@@ -1603,25 +1603,31 @@ protected Schema processReplaceOneOfByMapping(Schema schema) {
16031603 return schema ;
16041604 }
16051605 if (discriminator .getMapping () == null && discriminator .getPropertyName () != null ) {
1606+ List <Schema > oneOfs = schema .getOneOf ();
1607+ if (oneOfs .stream ().anyMatch (oneOf -> oneOf .get$ref () == null )) {
1608+ LOGGER .warn ("oneOf should only contain $ref for REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING normalization" );
1609+ return schema ;
1610+ }
16061611 Map <String , String > mappings = new TreeMap <>();
1612+ // is the discriminator qttribute qlready in this schema?
1613+ // if yes, it will be deleted in references oneOf to avoid duplicates
1614+ boolean hasProperty = findProperty (schema , discriminator .getPropertyName (), false , new HashSet <>()) != null ;
16071615 discriminator .setMapping (mappings );
1608- List <Schema > oneOfs = schema .getOneOf ();
16091616 for (Schema oneOf : oneOfs ) {
16101617 String refSchema = oneOf .get$ref ();
1611- if (refSchema != null ) {
1612- boolean hasProperty = findProperty (schema , discriminator .getPropertyName (), false , new HashSet <>()) != null ;
1613- String name = getDiscriminatorValue (refSchema , discriminator .getPropertyName (), hasProperty );
1614- mappings .put (name , refSchema );
1615- }
1618+ String name = getDiscriminatorValue (refSchema , discriminator .getPropertyName (), hasProperty , new HashSet <>(List .of (schema )));
1619+ mappings .put (name , refSchema );
1620+
16161621 }
16171622 // remove oneOf and only keep the new discriminator mapping
16181623 schema .setOneOf (null );
16191624 } else if (discriminator .getPropertyName () == null ) {
16201625 LOGGER .warn ("Missing property name in discriminator" );
16211626 } else if (discriminator .getMapping () != null && discriminator .getMapping ().size () != schema .getOneOf ().size ()) {
1622- LOGGER .warn ("Discriminator Mapping size " + discriminator .getMapping ().size () + " mismatch with oneOf size " + schema .getOneOf ().size ());
1627+ LOGGER .warn ("Discriminator mapping size " + discriminator .getMapping ().size () + " mismatch with oneOf size " + schema .getOneOf ().size ());
16231628 } else {
16241629 // remove oneOf and only keep the discriminator mapping
1630+ LOGGER .info ("Removing oneOf, discriminator mapping takes precedences on OneOfs" );
16251631 schema .setOneOf (null );
16261632 }
16271633 }
@@ -1656,10 +1662,10 @@ private boolean isInlineSchema(Schema schema) {
16561662 *
16571663 * @return the name
16581664 */
1659- protected String getDiscriminatorValue (String refSchema , String discriminatorPropertyName , boolean propertyAlreadyPresent ) {
1665+ protected String getDiscriminatorValue (String refSchema , String discriminatorPropertyName , boolean propertyAlreadyPresent , Set < Schema > visitedSchemas ) {
16601666 String schemaName = ModelUtils .getSimpleRef (refSchema );
16611667 Schema schema = ModelUtils .getSchema (openAPI , schemaName );
1662- Schema property = findProperty (schema , discriminatorPropertyName , propertyAlreadyPresent , new HashSet <>() );
1668+ Schema property = findProperty (schema , discriminatorPropertyName , propertyAlreadyPresent , visitedSchemas );
16631669 if (schema != null && schema .getExtensions () != null ) {
16641670 Object discriminatorValue = schema .getExtensions ().get ("x-discriminator-value" );
16651671 if (discriminatorValue != null ) {
@@ -1668,6 +1674,7 @@ protected String getDiscriminatorValue(String refSchema, String discriminatorPro
16681674 }
16691675
16701676 // find the discriminator value as a unique enum value
1677+ property = ModelUtils .getReferencedSchema (openAPI , property );
16711678 if (property != null ) {
16721679 List enums = property .getEnum ();
16731680 if (enums != null && enums .size () == 1 ) {
@@ -1687,14 +1694,15 @@ protected String getDiscriminatorValue(String refSchema, String discriminatorPro
16871694 * @param visitedSchemas avoid infinite recursion
16881695 * @return found property or null if not found.
16891696 */
1690- private Schema findProperty (Schema schema , String propertyName , boolean toDelete , HashSet <Object > visitedSchemas ) {
1697+ private Schema findProperty (Schema schema , String propertyName , boolean toDelete , Set <Schema > visitedSchemas ) {
1698+ schema = ModelUtils .getReferencedSchema (openAPI , schema );
16911699 if (propertyName == null || schema == null || visitedSchemas .contains (schema )) {
16921700 return null ;
16931701 }
16941702 visitedSchemas .add (schema );
16951703 Map <String , Schema > properties = schema .getProperties ();
16961704 if (properties != null ) {
1697- Schema property = properties .get (propertyName );
1705+ Schema property = ModelUtils . getReferencedSchema ( openAPI , properties .get (propertyName ) );
16981706 if (property != null ) {
16991707 if (toDelete ) {
17001708 if (schema .getProperties ().remove (propertyName ) != null ) {
@@ -1784,6 +1792,7 @@ private boolean hasParent(Schema parent, Schema child, String reference, Set<Sch
17841792 if (child .get$ref () != null && child .get$ref ().equals (reference )) {
17851793 return true ;
17861794 }
1795+ child = ModelUtils .getReferencedSchema (openAPI , child );
17871796 List <Schema > allOf = child .getAllOf ();
17881797 if (allOf != null ) {
17891798 for (Schema schema : allOf ) {
0 commit comments