diff --git a/utoipa/src/openapi/schema.rs b/utoipa/src/openapi/schema.rs index c8e5ccbc..53d3c76a 100644 --- a/utoipa/src/openapi/schema.rs +++ b/utoipa/src/openapi/schema.rs @@ -284,6 +284,12 @@ pub struct Discriminator { /// Defines a discriminator property name which must be found within all composite /// objects. pub property_name: String, + + /// An object to hold mappings between payload values and schema names or references. + /// This field can only be populated manually. There is no macro support and no + /// validation. + #[serde(skip_serializing_if = "BTreeMap::is_empty", default)] + pub mapping: BTreeMap, } impl Discriminator { @@ -299,6 +305,7 @@ impl Discriminator { pub fn new>(property_name: I) -> Self { Self { property_name: property_name.into(), + mapping: BTreeMap::new(), } } } @@ -2037,4 +2044,34 @@ mod tests { assert_eq!(json_str, json_de_str); } + + #[test] + fn serialize_discriminator_with_mapping() { + let mut discriminator = Discriminator::new("type"); + discriminator.mapping = [("int".to_string(), "#/components/schemas/MyInt".to_string())] + .into_iter() + .collect::>(); + let one_of = OneOfBuilder::new() + .item(Ref::from_schema_name("MyInt")) + .discriminator(Some(discriminator)) + .build(); + let json_value = serde_json::to_value(one_of).unwrap(); + + assert_json_eq!( + json_value, + json!({ + "oneOf": [ + { + "$ref": "#/components/schemas/MyInt" + } + ], + "discriminator": { + "propertyName": "type", + "mapping": { + "int": "#/components/schemas/MyInt" + } + } + }) + ); + } }