From ffaeff56fd71fe37f241eaab15f23cc79053bb69 Mon Sep 17 00:00:00 2001 From: RamiRond <38854387+RomiRand@users.noreply.github.com> Date: Fri, 22 May 2026 19:41:29 +0200 Subject: [PATCH] fix: normalize allOf schemas --- ...in-model-prefix-type-mapping-echo-api.yaml | 2 +- .../kotlin-model-prefix-type-mapping.yaml | 2 +- docs/customization.md | 6 +- .../codegen/OpenAPINormalizer.java | 58 +++++++++++++++---- .../codegen/utils/ModelUtils.java | 2 +- .../codegen/OpenAPINormalizerTest.java | 53 ++++++++++++----- .../codegen/utils/ModelUtilsTest.java | 24 ++++---- .../3_0/simplifyOneOfAnyOf_test.yaml | 14 +++++ 8 files changed, 118 insertions(+), 43 deletions(-) diff --git a/bin/configs/kotlin-model-prefix-type-mapping-echo-api.yaml b/bin/configs/kotlin-model-prefix-type-mapping-echo-api.yaml index e765f0def5b5..6a4be3d7793b 100644 --- a/bin/configs/kotlin-model-prefix-type-mapping-echo-api.yaml +++ b/bin/configs/kotlin-model-prefix-type-mapping-echo-api.yaml @@ -12,4 +12,4 @@ additionalProperties: enumPropertyNaming: UPPERCASE serializationLibrary: gson openapiNormalizer: - SIMPLIFY_ONEOF_ANYOF: false + SIMPLIFY_ONEOF_ANYOF_ALLOF: false diff --git a/bin/configs/kotlin-model-prefix-type-mapping.yaml b/bin/configs/kotlin-model-prefix-type-mapping.yaml index 15b0d3d885e0..db3fa6034e2e 100644 --- a/bin/configs/kotlin-model-prefix-type-mapping.yaml +++ b/bin/configs/kotlin-model-prefix-type-mapping.yaml @@ -13,4 +13,4 @@ additionalProperties: serializationLibrary: gson generateOneOfAnyOfWrappers: true openapiNormalizer: - SIMPLIFY_ONEOF_ANYOF: false + SIMPLIFY_ONEOF_ANYOF_ALLOF: false diff --git a/docs/customization.md b/docs/customization.md index e43a72893471..e8d0f1bfb5f4 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -536,7 +536,7 @@ Another useful option is `inlineSchemaOptions`, which allows you to customize ho OpenAPI Normalizer transforms the input OpenAPI doc/spec (which may not perfectly conform to the specification) to make it workable with OpenAPI Generator. A few rules are switched on by default since 7.0.0 release: -- SIMPLIFY_ONEOF_ANYOF +- SIMPLIFY_ONEOF_ANYOF_ALLOF - SIMPLIFY_BOOLEAN_ENUM - SIMPLIFY_ONEOF_ANYOF_ENUM - REFACTOR_ALLOF_WITH_PROPERTIES_ONLY @@ -581,11 +581,11 @@ Example: java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/simplifyBooleanEnum_test.yaml -o /tmp/java-okhttp/ --openapi-normalizer SIMPLIFY_BOOLEAN_ENUM=true ``` -- `SIMPLIFY_ONEOF_ANYOF`: when set to `true`, simplify oneOf/anyOf by 1) removing null (sub-schema) or enum of null (sub-schema) and setting nullable to true instead, and 2) simplifying oneOf/anyOf with a single sub-schema to just the sub-schema itself. +- `SIMPLIFY_ONEOF_ANYOF_ALLOF`: when set to `true`, simplify oneOf/anyOf/allOf by 1) removing null (sub-schema) or enum of null (sub-schema) and setting nullable to true instead, and 2) simplifying oneOf/anyOf/allOf with a single sub-schema to just the sub-schema itself. Example: ``` -java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml -o /tmp/java-okhttp/ --openapi-normalizer SIMPLIFY_ONEOF_ANYOF=true +java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml -o /tmp/java-okhttp/ --openapi-normalizer SIMPLIFY_ONEOF_ANYOF_ALLOF=true ``` - `KEEP_ONLY_FIRST_TAG_IN_OPERATION`: when set to `true`, only keep the first tag in operation if there are more than one tag defined. diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java index 68750b1bdefc..0221de94eec9 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java @@ -41,7 +41,7 @@ import java.util.stream.Collectors; import static org.openapitools.codegen.CodegenConstants.*; -import static org.openapitools.codegen.utils.ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema; +import static org.openapitools.codegen.utils.ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema; import static org.openapitools.codegen.utils.StringUtils.getUniqueString; public class OpenAPINormalizer { @@ -75,7 +75,7 @@ public class OpenAPINormalizer { // when set to true, only keep the first tag in operation if there are more than one tag defined. final String KEEP_ONLY_FIRST_TAG_IN_OPERATION = "KEEP_ONLY_FIRST_TAG_IN_OPERATION"; - // when set to true, complex composed schemas (a mix of oneOf/anyOf/anyOf and properties) with + // when set to true, complex composed schemas (a mix of oneOf/anyOf and properties) with // oneOf/anyOf containing only `required` and no properties (these are properties inter-dependency rules) // are removed as most generators cannot handle such case at the moment final String REMOVE_ANYOF_ONEOF_AND_KEEP_PROPERTIES_ONLY = "REMOVE_ANYOF_ONEOF_AND_KEEP_PROPERTIES_ONLY"; @@ -88,10 +88,10 @@ public class OpenAPINormalizer { // to just string final String SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING = "SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING"; - // when set to true, oneOf/anyOf schema with only one sub-schema is simplified to just the sub-schema + // when set to true, oneOf/anyOf/allOf schema with only one sub-schema is simplified to just the sub-schema // and if sub-schema contains "null", remove it and set nullable to true instead // and if sub-schema contains enum of "null", remove it and set nullable to true instead - final String SIMPLIFY_ONEOF_ANYOF = "SIMPLIFY_ONEOF_ANYOF"; + final String SIMPLIFY_ONEOF_ANYOF_ALLOF = "SIMPLIFY_ONEOF_ANYOF_ALLOF"; // when set to true, boolean enum will be converted to just boolean final String SIMPLIFY_BOOLEAN_ENUM = "SIMPLIFY_BOOLEAN_ENUM"; @@ -205,7 +205,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map inputRules) { ruleNames.add(REF_AS_PARENT_IN_ALLOF); ruleNames.add(REMOVE_ANYOF_ONEOF_AND_KEEP_PROPERTIES_ONLY); ruleNames.add(SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING); - ruleNames.add(SIMPLIFY_ONEOF_ANYOF); + ruleNames.add(SIMPLIFY_ONEOF_ANYOF_ALLOF); ruleNames.add(SIMPLIFY_BOOLEAN_ENUM); ruleNames.add(KEEP_ONLY_FIRST_TAG_IN_OPERATION); ruleNames.add(SET_TAGS_FOR_ALL_OPERATIONS); @@ -227,7 +227,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map inputRules) { ruleNames.add(REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING); // rules that are default to true - rules.put(SIMPLIFY_ONEOF_ANYOF, true); + rules.put(SIMPLIFY_ONEOF_ANYOF_ALLOF, true); rules.put(SIMPLIFY_BOOLEAN_ENUM, true); rules.put(SIMPLIFY_ONEOF_ANYOF_ENUM, true); rules.put(REFACTOR_ALLOF_WITH_PROPERTIES_ONLY, true); @@ -929,7 +929,7 @@ public Schema normalizeSchema(Schema schema, Set visitedSchemas) { if (ModelUtils.isArraySchema(schema)) { // array Schema result = normalizeArraySchema(schema); - normalizeSchema(result.getItems(), visitedSchemas); + result.setItems(normalizeSchema(result.getItems(), visitedSchemas)); return result; } else if (ModelUtils.isOneOf(schema)) { // oneOf return normalizeOneOf(schema, visitedSchemas); @@ -1203,6 +1203,7 @@ protected Schema normalizeAllOf(Schema schema, Set visitedSchemas) { // process rules here processUseAllOfRefAsParent(schema); + schema = processSimplifyAllOf(schema); return schema; } @@ -1306,6 +1307,39 @@ protected Schema normalizeComplexComposedSchema(Schema schema, Set visit // ===================== a list of rules ===================== // all rules (functions ) start with the word "process" + + /** + * If the schema is allOf and the sub-schemas is null, set `nullable: true` + * instead. + * If there's only one sub-schema, simply return the sub-schema directly. + * + * @param schema Schema + * @return Schema + */ + protected Schema processSimplifyAllOf(Schema schema) { + if (!getRule(SIMPLIFY_ONEOF_ANYOF_ALLOF)) { + return schema; + } + + List allOfSchemas = schema.getAllOf(); + if (allOfSchemas != null) { + // allOf containing $refs is intentional composition (inheritance, or $ref+sibling wrapping + // from normalizeReferenceSchema) — never simplify these, as the referenced schema may have + // no type/description yet still be a real model (e.g. after oneOf removal by discriminator mapping) + if (allOfSchemas.stream().anyMatch(s -> StringUtils.isNotEmpty(s.get$ref()))) { + return schema; + } + schema = simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, schema, allOfSchemas); + if (ModelUtils.isIntegerSchema(schema) || ModelUtils.isNumberSchema(schema) || ModelUtils.isStringSchema(schema) || ModelUtils.isBooleanSchema(schema)) { + if (schema.getSpecVersion().equals(SpecVersion.V30)) { + schema.setAllOf(null); + } + } + } + + return schema; + } + /** * Child schemas in `allOf` is considered a parent if it's a `$ref` (instead of inline schema). * @@ -1703,7 +1737,7 @@ protected Schema createSimplifiedEnumSchema(Schema originalSchema, Map subSchemas) { + public static Schema simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(OpenAPI openAPI, Schema schema, List subSchemas) { if (subSchemas.removeIf(subSchema -> isNullTypeSchema(openAPI, subSchema))) { schema.setNullable(true); } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java index 87e96066c0bc..c7211e1cd18a 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java @@ -262,7 +262,7 @@ public void testSimplifyOneOfAnyOfEnum() throws Exception { @Test public void testOpenAPINormalizerSimplifyOneOfAnyOf() { - // to test the rule SIMPLIFY_ONEOF_ANYOF + // to test the rule SIMPLIFY_ONEOF_ANYOF_ALLOF OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml"); Schema schema = openAPI.getComponents().getSchemas().get("AnyOfTest"); @@ -303,7 +303,7 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf() { assertEquals(schema19.getAnyOf().size(), 1); Map options = new HashMap<>(); - options.put("SIMPLIFY_ONEOF_ANYOF", "true"); + options.put("SIMPLIFY_ONEOF_ANYOF_ALLOF", "true"); OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options); openAPINormalizer.normalize(); @@ -353,6 +353,33 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf() { assertEquals(schema20.getEnum().size(), 2); } + @Test + public void testOpenAPINormalizerSimplifyAllOf() { + OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml"); + + Schema schema = openAPI.getComponents().getSchemas().get("AllOfTest"); + assertEquals(schema.getAllOf().size(), 4); + assertNull(schema.getNullable()); + + Schema schema3 = openAPI.getComponents().getSchemas().get("SingleAllOfTest"); + assertEquals(schema3.getAllOf().size(), 1); + + Map options = new HashMap<>(); + options.put("SIMPLIFY_ONEOF_ANYOF_ALLOF", "true"); + OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options); + openAPINormalizer.normalize(); + + Schema schema2 = openAPI.getComponents().getSchemas().get("AllOfTest"); + assertNull(schema2.getAllOf()); + assertTrue(schema2 instanceof StringSchema); + assertTrue(schema2.getNullable()); + + Schema schema4 = openAPI.getComponents().getSchemas().get("SingleAllOfTest"); + assertEquals(schema4.getAllOf(), null); + assertEquals(schema4.getType(), "string"); + assertEquals(schema4.getEnum().size(), 2); + } + @Test public void testOpenAPINormalizerSimplifyOneOfWithSingleRef() { OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml"); @@ -361,7 +388,7 @@ public void testOpenAPINormalizerSimplifyOneOfWithSingleRef() { assertEquals(((Schema) oneOfWithSingleRef.getProperties().get("number")).getOneOf().size(), 1); Map options = new HashMap<>(); - options.put("SIMPLIFY_ONEOF_ANYOF", "true"); + options.put("SIMPLIFY_ONEOF_ANYOF_ALLOF", "true"); OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options); openAPINormalizer.normalize(); @@ -494,7 +521,7 @@ public void testAddUnsignedToIntegerWithInvalidMaxValue() { @Test public void testOpenAPINormalizerConvertEnumNullToNullable() { - // to test the rule SIMPLIFY_ONEOF_ANYOF, which now also covers CONVERT_ENUM_NULL_TO_NULLABLE (removed) + // to test the rule SIMPLIFY_ONEOF_ANYOF_ALLOF, which now also covers CONVERT_ENUM_NULL_TO_NULLABLE (removed) OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/convertEnumNullToNullable_test.yaml"); Schema schema = openAPI.getComponents().getSchemas().get("AnyOfTest"); @@ -502,7 +529,7 @@ public void testOpenAPINormalizerConvertEnumNullToNullable() { assertNull(schema.getNullable()); Map options = new HashMap<>(); - options.put("SIMPLIFY_ONEOF_ANYOF", "true"); + options.put("SIMPLIFY_ONEOF_ANYOF_ALLOF", "true"); OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options); openAPINormalizer.normalize(); @@ -513,7 +540,7 @@ public void testOpenAPINormalizerConvertEnumNullToNullable() { @Test public void testOpenAPINormalizerDefaultRules() { - // to test the rule SIMPLIFY_ONEOF_ANYOF, which now also covers CONVERT_ENUM_NULL_TO_NULLABLE (removed) + // to test the rule SIMPLIFY_ONEOF_ANYOF_ALLOF, which now also covers CONVERT_ENUM_NULL_TO_NULLABLE (removed) OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/convertEnumNullToNullable_test.yaml"); Schema schema = openAPI.getComponents().getSchemas().get("AnyOfTest"); @@ -521,8 +548,8 @@ public void testOpenAPINormalizerDefaultRules() { assertNull(schema.getNullable()); Map options = new HashMap<>(); - // SIMPLIFY_ONEOF_ANYOF is switched on by default as part of v7.0.0 release - //options.put("SIMPLIFY_ONEOF_ANYOF", "true"); + // SIMPLIFY_ONEOF_ANYOF_ALLOF is switched on by default as part of v7.0.0 release + //options.put("SIMPLIFY_ONEOF_ANYOF_ALLOF", "true"); OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options); openAPINormalizer.normalize(); @@ -533,7 +560,7 @@ public void testOpenAPINormalizerDefaultRules() { @Test public void testOpenAPINormalizerDisableAll() { - // to test the rule SIMPLIFY_ONEOF_ANYOF, which now also covers CONVERT_ENUM_NULL_TO_NULLABLE (removed) + // to test the rule SIMPLIFY_ONEOF_ANYOF_ALLOF, which now also covers CONVERT_ENUM_NULL_TO_NULLABLE (removed) OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/convertEnumNullToNullable_test.yaml"); // before test @@ -1321,7 +1348,7 @@ public void testSetPrimitiveTypesToNullable() { @Test public void testOpenAPINormalizerSimplifyOneOfAnyOf31SpecForIssue18184() { - // to test the rule SIMPLIFY_ONEOF_ANYOF in 3.1 spec + // to test the rule SIMPLIFY_ONEOF_ANYOF_ALLOF in 3.1 spec OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/issue_18184.yaml"); // test spec contains anyOf with a ref to enum and another scheme type is null @@ -1449,7 +1476,7 @@ public void testOpenAPINormalizerProcessingArraySchema31NullabilitySpec() { @Test public void testOpenAPINormalizerSimplifyOneOfAnyOf31Spec() { - // to test the rule SIMPLIFY_ONEOF_ANYOF with 3.1 spec + // to test the rule SIMPLIFY_ONEOF_ANYOF_ALLOF with 3.1 spec OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/simplifyOneOfAnyOf_test.yaml"); Schema schema = openAPI.getComponents().getSchemas().get("AnyOfTest"); @@ -1498,7 +1525,7 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf31Spec() { // start the normalization Map options = new HashMap<>(); - options.put("SIMPLIFY_ONEOF_ANYOF", "true"); + options.put("SIMPLIFY_ONEOF_ANYOF_ALLOF", "true"); OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options); openAPINormalizer.normalize(); @@ -1573,7 +1600,7 @@ public void testOpenAPINormalizerSimplifyOneOfWithSingleRef31Spec() { assertEquals(((Schema) oneOfWithSingleRef.getProperties().get("number")).getOneOf().size(), 1); Map options = new HashMap<>(); - options.put("SIMPLIFY_ONEOF_ANYOF", "true"); + options.put("SIMPLIFY_ONEOF_ANYOF_ALLOF", "true"); OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options); openAPINormalizer.normalize(); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java index 906ace829bcb..476730aec8ab 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java @@ -487,7 +487,7 @@ public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema() { Schema anyOfWithSeveralSubSchemasButSingleNonNull = ModelUtils.getSchema(openAPI, "AnyOfTest"); subSchemas = anyOfWithSeveralSubSchemasButSingleNonNull.getAnyOf(); - schema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, anyOfWithSeveralSubSchemasButSingleNonNull, subSchemas); + schema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, anyOfWithSeveralSubSchemasButSingleNonNull, subSchemas); assertNull(schema.getOneOf()); assertNull(schema.getAnyOf()); assertTrue(schema.getNullable()); @@ -495,7 +495,7 @@ public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema() { Schema anyOfWithSingleNonNullSubSchema = ModelUtils.getSchema(openAPI, "Parent"); subSchemas = ((Schema) anyOfWithSingleNonNullSubSchema.getProperties().get("number")).getAnyOf(); - schema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, anyOfWithSingleNonNullSubSchema, subSchemas); + schema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, anyOfWithSingleNonNullSubSchema, subSchemas); assertNull(schema.getOneOf()); assertNull(schema.getAnyOf()); assertNull(schema.getNullable()); @@ -503,7 +503,7 @@ public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema() { Schema oneOfWithSeveralSubSchemasButSingleNonNull = ModelUtils.getSchema(openAPI, "OneOfTest"); subSchemas = oneOfWithSeveralSubSchemasButSingleNonNull.getOneOf(); - schema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, oneOfWithSeveralSubSchemasButSingleNonNull, subSchemas); + schema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, oneOfWithSeveralSubSchemasButSingleNonNull, subSchemas); assertNull(schema.getOneOf()); assertNull(schema.getAnyOf()); assertTrue(schema.getNullable()); @@ -511,7 +511,7 @@ public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema() { Schema oneOfWithSingleNonNullSubSchema = ModelUtils.getSchema(openAPI, "ParentWithOneOfProperty"); subSchemas = ((Schema) oneOfWithSingleNonNullSubSchema.getProperties().get("number")).getOneOf(); - schema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, oneOfWithSingleNonNullSubSchema, subSchemas); + schema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, oneOfWithSingleNonNullSubSchema, subSchemas); assertNull(schema.getOneOf()); assertNull(schema.getAnyOf()); assertNull(schema.getNullable()); @@ -519,7 +519,7 @@ public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema() { Schema oneOfWithSeveralSubSchemas = ModelUtils.getSchema(openAPI, "ParentWithPluralOneOfProperty"); subSchemas = ((Schema) oneOfWithSeveralSubSchemas.getProperties().get("number")).getOneOf(); - schema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, oneOfWithSeveralSubSchemas, subSchemas); + schema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, oneOfWithSeveralSubSchemas, subSchemas); assertNull(schema.getOneOf()); assertNotNull(oneOfWithSeveralSubSchemas.getProperties().get("number")); assertNull(schema.getAnyOf()); @@ -536,7 +536,7 @@ public void simplifyOneOfWithOnlyOneNonNullSubSchemaKeepsReadOnlyWriteOnlyAttrib Schema oneOfWithNullAndRefSubSchema = ModelUtils.getSchema(openAPI, "OneOfParentRefTest"); Schema numberPropertySchema = ((Schema) oneOfWithNullAndRefSubSchema.getProperties().get("number")); subSchemas = numberPropertySchema.getOneOf(); - schema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, numberPropertySchema, subSchemas); + schema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, numberPropertySchema, subSchemas); assertNull(schema.getOneOf()); assertNull(schema.getAnyOf()); assertTrue(schema.getNullable()); @@ -546,7 +546,7 @@ public void simplifyOneOfWithOnlyOneNonNullSubSchemaKeepsReadOnlyWriteOnlyAttrib Schema number2PropertySchema = ((Schema) oneOfWithNullAndRefSubSchema.getProperties().get("number2")); subSchemas = number2PropertySchema.getOneOf(); - schema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, number2PropertySchema, subSchemas); + schema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, number2PropertySchema, subSchemas); assertNull(schema.getOneOf()); assertNull(schema.getAnyOf()); assertTrue(schema.getNullable()); @@ -564,7 +564,7 @@ public void simplifyAnyOfWithOnlyOneNonNullSubSchemaKeepsReadOnlyWriteOnlyAttrib Schema anyOfWithNullAndRefSubSchema = ModelUtils.getSchema(openAPI, "AnyOfParentRefTest"); Schema numberPropertySchema = ((Schema) anyOfWithNullAndRefSubSchema.getProperties().get("number")); subSchemas = numberPropertySchema.getAnyOf(); - schema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, numberPropertySchema, subSchemas); + schema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, numberPropertySchema, subSchemas); assertNull(schema.getOneOf()); assertNull(schema.getAnyOf()); assertTrue(schema.getNullable()); @@ -574,7 +574,7 @@ public void simplifyAnyOfWithOnlyOneNonNullSubSchemaKeepsReadOnlyWriteOnlyAttrib Schema number2PropertySchema = ((Schema) anyOfWithNullAndRefSubSchema.getProperties().get("number2")); subSchemas = number2PropertySchema.getAnyOf(); - schema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, number2PropertySchema, subSchemas); + schema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, number2PropertySchema, subSchemas); assertNull(schema.getOneOf()); assertNull(schema.getAnyOf()); assertTrue(schema.getNullable()); @@ -592,7 +592,7 @@ public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchemaKeepsParentDescription( new StringSchema(), new Schema<>().type("null") ))); - Schema anyOfSchema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, anyOfParent, anyOfParent.getAnyOf()); + Schema anyOfSchema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, anyOfParent, anyOfParent.getAnyOf()); assertEquals(anyOfSchema.getDescription(), "Access token"); Schema oneOfParent = new Schema().description("Expires at"); @@ -600,7 +600,7 @@ public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchemaKeepsParentDescription( new IntegerSchema(), new Schema<>().type("null") ))); - Schema oneOfSchema = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema(openAPI, oneOfParent, oneOfParent.getOneOf()); + Schema oneOfSchema = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema(openAPI, oneOfParent, oneOfParent.getOneOf()); assertEquals(oneOfSchema.getDescription(), "Expires at"); Schema anyOfParentWithChildDescription = new Schema().description("Parent description"); @@ -608,7 +608,7 @@ public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchemaKeepsParentDescription( new StringSchema().description("Child description"), new Schema<>().type("null") ))); - Schema anyOfSchemaWithChildDescription = ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema( + Schema anyOfSchemaWithChildDescription = ModelUtils.simplifyOneOfAnyOfAllOfWithOnlyOneNonNullSubSchema( openAPI, anyOfParentWithChildDescription, anyOfParentWithChildDescription.getAnyOf()); diff --git a/modules/openapi-generator/src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml b/modules/openapi-generator/src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml index ee1c5e2ce445..9dd1607e0548 100644 --- a/modules/openapi-generator/src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml @@ -45,6 +45,13 @@ components: oneOf: - type: string - type: 'null' + AllOfTest: + description: to test allOf + allOf: + - type: string + - type: 'null' + - type: null + - $ref: null OneOfNullableTest: description: to test oneOf nullable oneOf: @@ -58,6 +65,13 @@ components: enum: - A - B + SingleAllOfTest: + description: to test allOf (enum string only) + allOf: + - type: string + enum: + - A + - B Parent: type: object properties: