Skip to content

Commit 4f26f25

Browse files
committed
Fix some cubic findings
1 parent 18d2f81 commit 4f26f25

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

docs/customization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ Example:
651651
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/required-properties.yaml -o /tmp/java-okhttp/ --openapi-normalizer REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT=true
652652
```
653653
654-
- `REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING``: when set to true, oneOf is removed and is converted into mappings in a discriminator mapping.
654+
- `REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING`: when set to true, oneOf is removed and is converted into mappings in a discriminator mapping.
655655
656656
Example:
657657
```

modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)