@@ -78,6 +78,10 @@ public class OpenAPINormalizer {
7878 // are removed as most generators cannot handle such case at the moment
7979 final String REMOVE_ANYOF_ONEOF_AND_KEEP_PROPERTIES_ONLY = "REMOVE_ANYOF_ONEOF_AND_KEEP_PROPERTIES_ONLY" ;
8080
81+ // when set to true, oneOf is removed and is converted into mappings in a discriminator mapping
82+ final String REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING = "REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING" ;
83+
84+
8185 // when set to true, oneOf/anyOf with either string or enum string as sub schemas will be simplified
8286 // to just string
8387 final String SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING = "SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING" ;
@@ -214,6 +218,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
214218 ruleNames .add (SIMPLIFY_ONEOF_ANYOF_ENUM );
215219 ruleNames .add (REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT );
216220 ruleNames .add (SORT_MODEL_PROPERTIES );
221+ ruleNames .add (REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING );
217222
218223 // rules that are default to true
219224 rules .put (SIMPLIFY_ONEOF_ANYOF , true );
@@ -1053,6 +1058,8 @@ protected Schema normalizeOneOf(Schema schema, Set<Schema> visitedSchemas) {
10531058 // simplify first as the schema may no longer be a oneOf after processing the rule below
10541059 schema = processSimplifyOneOf (schema );
10551060
1061+ schema = processReplaceOneOfByMapping (schema );
1062+
10561063 // if it's still a oneOf, loop through the sub-schemas
10571064 if (schema .getOneOf () != null ) {
10581065 for (int i = 0 ; i < schema .getOneOf ().size (); i ++) {
@@ -1569,6 +1576,33 @@ protected Schema processSimplifyOneOf(Schema schema) {
15691576 return schema ;
15701577 }
15711578
1579+
1580+ protected Schema processReplaceOneOfByMapping (Schema schema ) {
1581+ if (!getRule (REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING )) {
1582+ return schema ;
1583+ }
1584+
1585+ if (schema .getDiscriminator () != null ) {
1586+ Discriminator discriminator = schema .getDiscriminator ();
1587+ if (discriminator .getMapping () == null ) {
1588+ Map <String , String > mapping = new TreeMap <>();
1589+ discriminator .setMapping (mapping );
1590+ for (Object oneOfObject : schema .getOneOf ()) {
1591+ Schema oneOf = (Schema ) oneOfObject ;
1592+ String ref = oneOf .get$ref ();
1593+ if (ref != null ) {
1594+ String name = ref .contains ("/" ) ? ref .substring (ref .lastIndexOf ('/' ) + 1 ) : ref ;
1595+ mapping .put (name , oneOf .get$ref ());
1596+ }
1597+ }
1598+ }
1599+
1600+ schema .setOneOf (null );
1601+ }
1602+ return schema ;
1603+ }
1604+
1605+
15721606 /**
15731607 * Set nullable to true in array/set if needed.
15741608 *
0 commit comments