mirror of
https://github.com/poem-web/poem.git
synced 2026-01-24 20:08:49 +00:00
feat(poem-openapi): support externally tagged unions with primitive types
This commit is contained in:
@@ -168,11 +168,6 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {
|
||||
|
||||
if args.externally_tagged {
|
||||
create_schemas.push(quote! {
|
||||
{
|
||||
fn __check_is_object_type<T: #crate_name::types::IsObjectType>() {}
|
||||
__check_is_object_type::<#object_ty>();
|
||||
}
|
||||
|
||||
let schema = #crate_name::registry::MetaSchema {
|
||||
description: #description,
|
||||
all_of: ::std::vec![
|
||||
|
||||
@@ -844,3 +844,90 @@ fn with_externally_tagged_mapping() {
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_externally_tagged_primitives() {
|
||||
#[derive(Union, Debug, PartialEq)]
|
||||
#[oai(externally_tagged)]
|
||||
enum MyObj {
|
||||
A(String),
|
||||
B(bool),
|
||||
}
|
||||
|
||||
let schema = get_meta::<MyObj>();
|
||||
|
||||
assert_eq!(
|
||||
schema,
|
||||
MetaSchema {
|
||||
rust_typename: Some("union::with_externally_tagged_primitives::MyObj"),
|
||||
ty: "object",
|
||||
any_of: vec![
|
||||
MetaSchemaRef::Reference("MyObj_A".to_string()),
|
||||
MetaSchemaRef::Reference("MyObj_B".to_string()),
|
||||
],
|
||||
..MetaSchema::ANY
|
||||
}
|
||||
);
|
||||
|
||||
let schema_myobj_a = get_meta_by_name::<MyObj>("MyObj_A");
|
||||
assert_eq!(
|
||||
schema_myobj_a,
|
||||
MetaSchema {
|
||||
all_of: vec![MetaSchemaRef::Inline(Box::new(MetaSchema {
|
||||
required: vec!["A"],
|
||||
properties: vec![(
|
||||
"A",
|
||||
MetaSchemaRef::Inline(Box::new(MetaSchema::new("string")))
|
||||
)],
|
||||
..MetaSchema::new("object")
|
||||
})),],
|
||||
..MetaSchema::ANY
|
||||
}
|
||||
);
|
||||
|
||||
let schema_myobj_b = get_meta_by_name::<MyObj>("MyObj_B");
|
||||
assert_eq!(
|
||||
schema_myobj_b,
|
||||
MetaSchema {
|
||||
all_of: vec![MetaSchemaRef::Inline(Box::new(MetaSchema {
|
||||
required: vec!["B"],
|
||||
properties: vec![(
|
||||
"B",
|
||||
MetaSchemaRef::Inline(Box::new(MetaSchema::new("boolean")))
|
||||
)],
|
||||
..MetaSchema::new("object")
|
||||
})),],
|
||||
..MetaSchema::ANY
|
||||
}
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
MyObj::parse_from_json(Some(json!({
|
||||
"A":"hello",
|
||||
})))
|
||||
.unwrap(),
|
||||
MyObj::A("hello".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
MyObj::B(true).to_json(),
|
||||
Some(json!({
|
||||
"B": true,
|
||||
}))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
MyObj::parse_from_json(Some(json!({
|
||||
"A": "hello",
|
||||
})))
|
||||
.unwrap(),
|
||||
MyObj::A("hello".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
MyObj::B(true).to_json(),
|
||||
Some(json!({
|
||||
"B": true,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user