@@ -1591,7 +1591,7 @@ protected Schema processSimplifyOneOf(Schema schema) {
15911591 * </ul>
15921592 */
15931593 protected Schema processReplaceOneOfByMapping (Schema schema ) {
1594- if (!getRule (REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING )) {
1594+ if (!getRule (REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING ) || schema . getOneOf () == null ) {
15951595 return schema ;
15961596 }
15971597 Discriminator discriminator = schema .getDiscriminator ();
@@ -1606,17 +1606,24 @@ protected Schema processReplaceOneOfByMapping(Schema schema) {
16061606 Map <String , String > mappings = new TreeMap <>();
16071607 discriminator .setMapping (mappings );
16081608 List <Schema > oneOfs = schema .getOneOf ();
1609- for (Schema oneOf : oneOfs ) {
1609+ for (Schema oneOf : oneOfs ) {
16101610 String refSchema = oneOf .get$ref ();
16111611 if (refSchema != null ) {
16121612 boolean hasProperty = findProperty (schema , discriminator .getPropertyName (), false , new HashSet <>()) != null ;
16131613 String name = getDiscriminatorValue (refSchema , discriminator .getPropertyName (), hasProperty );
16141614 mappings .put (name , refSchema );
16151615 }
16161616 }
1617+ // remove oneOf and only keep the new discriminator mapping
1618+ schema .setOneOf (null );
1619+ } else if (discriminator .getPropertyName () == null ) {
1620+ LOGGER .warn ("Missing property name in discriminator" );
1621+ } 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 ());
1623+ } else {
1624+ // remove oneOf and only keep the discriminator mapping
1625+ schema .setOneOf (null );
16171626 }
1618- // remove oneOf and only keep the discriminator mapping
1619- schema .setOneOf (null );
16201627 }
16211628
16221629 return schema ;
@@ -1690,8 +1697,11 @@ private Schema findProperty(Schema schema, String propertyName, boolean toDelete
16901697 Schema property = properties .get (propertyName );
16911698 if (property != null ) {
16921699 if (toDelete ) {
1693- if (schema .getProperties ().remove (propertyName ) != null && schema .getProperties ().isEmpty ()) {
1694- schema .setProperties (null );
1700+ if (schema .getProperties ().remove (propertyName ) != null ) {
1701+ LOGGER .info ("property " + propertyName + " has been removed in REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING normalization" );
1702+ if (schema .getProperties ().isEmpty ()) {
1703+ schema .setProperties (null );
1704+ }
16951705 }
16961706 }
16971707 return property ;
0 commit comments