From 8209b408f86d505c3f9b9a8d088df52e98f34245 Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Thu, 19 Nov 2026 16:33:28 +0300 Subject: [PATCH] Fix #25756: support array and object types in OAS 3.1 multi-type schemas Signed-off-by: Inf1mum --- .../codegen/OpenAPINormalizer.java | 150 ++++++++++++++---- .../codegen/OpenAPINormalizerTest.java | 68 ++++++++ .../src/test/resources/3_1/issue_25756.yaml | 56 +++++++ 3 files changed, 239 insertions(+), 35 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_1/issue_25756.yaml 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 d3e79dbf8c1c..4bda8aa5f526 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 @@ -2322,41 +2322,7 @@ protected Schema processNormalize31Spec(Schema schema, Set visitedSchema if (schema.getTypes().size() == 1) { String type = String.valueOf(schema.getTypes().iterator().next()); if (ModelUtils.isArraySchema(schema)) { - ArraySchema as = new ArraySchema(); - as.setDescription(schema.getDescription()); - as.setDefault(schema.getDefault()); - as.setTitle(schema.getTitle()); - if (schema.getExample() != null) { - as.setExample(schema.getExample()); - } - if (schema.getExamples() != null) { - as.setExamples(schema.getExamples()); - } - as.setMinItems(schema.getMinItems()); - as.setMaxItems(schema.getMaxItems()); - as.setExtensions(schema.getExtensions()); - as.setXml(schema.getXml()); - as.setNullable(schema.getNullable()); - as.setUniqueItems(schema.getUniqueItems()); - as.setDeprecated(schema.getDeprecated()); - as.setReadOnly(schema.getReadOnly()); - as.setWriteOnly(schema.getWriteOnly()); - if (schema.getItems() != null) { - // `items` is also a json schema - if (StringUtils.isNotEmpty(schema.getItems().get$ref())) { - Schema ref = new Schema(); - ref.set$ref(schema.getItems().get$ref()); - as.setItems(ref); - } else { // inline schema (e.g. model, string, etc) - Schema updatedItems = normalizeSchema(schema.getItems(), visitedSchemas); - as.setItems(updatedItems); - } - } else { - // when items is not defined, default to any type - as.setItems(new Schema()); - } - - return as; + return build31ArraySchema(schema, visitedSchemas); } else { // other primitive type such as string // set type (3.0 spec) directly schema.setType(type); @@ -2364,6 +2330,7 @@ protected Schema processNormalize31Spec(Schema schema, Set visitedSchema } else { // more than 1 item // convert to anyOf and keep all other attributes (e.g. nullable, description) // the same. No need to handle null as it should have been removed at this point. + boolean allTypesConverted = true; for (Object type : schema.getTypes()) { switch (String.valueOf(type)) { case "string": @@ -2378,16 +2345,129 @@ protected Schema processNormalize31Spec(Schema schema, Set visitedSchema case "boolean": schema.addAnyOfItem(new BooleanSchema()); break; + case "array": + schema.addAnyOfItem(build31ArraySchema(schema, visitedSchemas)); + schema.setItems(null); + break; + case "object": + schema.addAnyOfItem(build31ObjectSchema(schema, visitedSchemas)); + schema.setProperties(null); + schema.setAdditionalProperties(null); + break; default: + allTypesConverted = false; LOGGER.error("Type {} not yet supported in openapi-normalizer to process OpenAPI 3.1 spec with multiple types.", String.valueOf(type)); LOGGER.error("Please report the issue via https://github.com/OpenAPITools/openapi-generator/issues/new/."); } } + if (allTypesConverted) { + schema.setTypes(null); + } } return schema; } + /** + * Builds an OAS 3.0 style array schema out of an OAS 3.1 schema which defines + * the array type (as part of its type array), moving `items` and the array + * related constraints into the resulting schema. + * + * @param schema Schema to convert + * @param visitedSchemas Set of visited schemas + * @return the resulting array schema + */ + protected Schema build31ArraySchema(Schema schema, Set visitedSchemas) { + ArraySchema as = new ArraySchema(); + as.setDescription(schema.getDescription()); + as.setDefault(schema.getDefault()); + as.setTitle(schema.getTitle()); + if (schema.getExample() != null) { + as.setExample(schema.getExample()); + } + if (schema.getExamples() != null) { + as.setExamples(schema.getExamples()); + } + as.setMinItems(schema.getMinItems()); + as.setMaxItems(schema.getMaxItems()); + as.setExtensions(schema.getExtensions()); + as.setXml(schema.getXml()); + as.setNullable(schema.getNullable()); + as.setUniqueItems(schema.getUniqueItems()); + as.setDeprecated(schema.getDeprecated()); + as.setReadOnly(schema.getReadOnly()); + as.setWriteOnly(schema.getWriteOnly()); + if (schema.getItems() != null) { + // `items` is also a json schema + if (StringUtils.isNotEmpty(schema.getItems().get$ref())) { + Schema ref = new Schema(); + ref.set$ref(schema.getItems().get$ref()); + as.setItems(ref); + } else { // inline schema (e.g. model, string, etc) + Schema updatedItems = normalizeSchema(schema.getItems(), visitedSchemas); + as.setItems(updatedItems); + } + } else { + // when items is not defined, default to any type + as.setItems(new Schema()); + } + + return as; + } + + /** + * Builds an OAS 3.0 style object schema out of an OAS 3.1 schema which defines + * the object type (as part of its type array), moving `properties` and + * `additionalProperties` into the resulting schema. + * + * @param schema Schema to convert + * @param visitedSchemas Set of visited schemas + * @return the resulting object schema + */ + protected Schema build31ObjectSchema(Schema schema, Set visitedSchemas) { + ObjectSchema os = new ObjectSchema(); + os.setDescription(schema.getDescription()); + os.setDefault(schema.getDefault()); + os.setTitle(schema.getTitle()); + if (schema.getExample() != null) { + os.setExample(schema.getExample()); + } + if (schema.getExamples() != null) { + os.setExamples(schema.getExamples()); + } + os.setMinProperties(schema.getMinProperties()); + os.setMaxProperties(schema.getMaxProperties()); + os.setRequired(schema.getRequired()); + os.setExtensions(schema.getExtensions()); + os.setXml(schema.getXml()); + os.setNullable(schema.getNullable()); + os.setDeprecated(schema.getDeprecated()); + os.setReadOnly(schema.getReadOnly()); + os.setWriteOnly(schema.getWriteOnly()); + if (schema.getProperties() != null) { + Map properties = schema.getProperties(); + for (Map.Entry entry : properties.entrySet()) { + os.addProperties(entry.getKey(), normalizeSchema(entry.getValue(), visitedSchemas)); + } + } + if (schema.getAdditionalProperties() != null) { + if (schema.getAdditionalProperties() instanceof Schema) { + Schema additionalProperties = (Schema) schema.getAdditionalProperties(); + if (StringUtils.isNotEmpty(additionalProperties.get$ref())) { + Schema ref = new Schema(); + ref.set$ref(additionalProperties.get$ref()); + os.setAdditionalProperties(ref); + } else { + os.setAdditionalProperties(normalizeSchema(additionalProperties, visitedSchemas)); + } + } else { + os.setAdditionalProperties(schema.getAdditionalProperties()); + } + } + + return os; + } + /** * Normalizes OAS 3.1 binary content media schemas to the OAS 3.0 binary schema shape. * 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 8fc52c3f2f68..75d69665b2b6 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 @@ -744,6 +744,74 @@ public void testNormalize31Parameters() { assertNotNull(pathItem.getDelete().getParameters().get(0).getSchema().getTypes()); } + @Test + public void testNormalize31MultipleTypes() { + OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/issue_25756.yaml"); + + OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, Map.of("NORMALIZE_31SPEC", "true")); + openAPINormalizer.normalize(); + + Map companyProperties = openAPI.getComponents().getSchemas().get("Company").getProperties(); + + Schema tradingNames = companyProperties.get("tradingNames"); + assertNull(tradingNames.getTypes()); + assertEquals(tradingNames.getAnyOf().size(), 2); + assertNull(tradingNames.getItems()); + ArraySchema tradingNamesArray = null; + Schema tradingNamesString = null; + for (Object subSchema : tradingNames.getAnyOf()) { + if (subSchema instanceof ArraySchema) { + tradingNamesArray = (ArraySchema) subSchema; + } else if (subSchema instanceof StringSchema) { + tradingNamesString = (StringSchema) subSchema; + } + } + assertNotNull(tradingNamesArray); + assertNotNull(tradingNamesString); + assertEquals(tradingNamesArray.getDescription(), "A list for most countries, a single string for some."); + assertEquals(tradingNamesArray.getItems().getType(), "string"); + + Schema objOrArray = companyProperties.get("objOrArray"); + assertNull(objOrArray.getTypes()); + assertEquals(objOrArray.getAnyOf().size(), 2); + assertNull(objOrArray.getItems()); + ObjectSchema objOrArrayObject = null; + ArraySchema objOrArrayArray = null; + for (Object subSchema : objOrArray.getAnyOf()) { + if (subSchema instanceof ObjectSchema) { + objOrArrayObject = (ObjectSchema) subSchema; + } else if (subSchema instanceof ArraySchema) { + objOrArrayArray = (ArraySchema) subSchema; + } + } + assertNotNull(objOrArrayObject); + assertNotNull(objOrArrayArray); + assertEquals(objOrArrayArray.getDescription(), "Related case from the issue."); + assertEquals(objOrArrayArray.getItems().getType(), "string"); + + Schema nullableArrOrStr = companyProperties.get("nullableArrOrStr"); + assertTrue(nullableArrOrStr.getNullable()); + assertNull(nullableArrOrStr.getTypes()); + assertEquals(nullableArrOrStr.getAnyOf().size(), 2); + + Schema mapOrString = openAPI.getComponents().getSchemas().get("MapOrString"); + assertNull(mapOrString.getTypes()); + assertEquals(mapOrString.getAnyOf().size(), 2); + ObjectSchema mapOrStringObject = null; + Schema mapOrStringString = null; + for (Object subSchema : mapOrString.getAnyOf()) { + if (subSchema instanceof ObjectSchema) { + mapOrStringObject = (ObjectSchema) subSchema; + } else if (subSchema instanceof StringSchema) { + mapOrStringString = (StringSchema) subSchema; + } + } + assertNotNull(mapOrStringObject); + assertNotNull(mapOrStringString); + assertNotNull(mapOrStringObject.getAdditionalProperties()); + assertEquals(((Schema) mapOrStringObject.getAdditionalProperties()).getType(), "string"); + } + @Test public void testNormalize31ExclusiveMinMaxNumericOnly() { OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/exclusive-min-max.yaml"); diff --git a/modules/openapi-generator/src/test/resources/3_1/issue_25756.yaml b/modules/openapi-generator/src/test/resources/3_1/issue_25756.yaml new file mode 100644 index 000000000000..77719d651654 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_1/issue_25756.yaml @@ -0,0 +1,56 @@ +openapi: 3.1.0 +info: + title: Multi-type property repro (issue 25756) + version: 1.0.0 +paths: + /companies/{id}: + get: + operationId: getCompany + parameters: + - name: id + in: path + required: true + schema: + type: string + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Company' +components: + schemas: + Company: + type: object + properties: + name: + type: string + tradingNames: + description: A list for most countries, a single string for some. + type: + - array + - string + items: + type: string + objOrArray: + description: Related case from the issue. + type: + - object + - array + items: + type: string + nullableArrOrStr: + description: Nullable variant of the array-or-string case. + type: + - array + - string + - 'null' + items: + type: string + MapOrString: + type: + - object + - string + additionalProperties: + type: string