Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2322,48 +2322,15 @@ protected Schema processNormalize31Spec(Schema schema, Set<Schema> 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);
}
} 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":
Expand All @@ -2378,16 +2345,129 @@ protected Schema processNormalize31Spec(Schema schema, Set<Schema> visitedSchema
case "boolean":
schema.addAnyOfItem(new BooleanSchema());
break;
case "array":
schema.addAnyOfItem(build31ArraySchema(schema, visitedSchemas));
schema.setItems(null);
break;
case "object":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Object-first unions that declare properties are still not normalized by this change. Because normalizeSchema handles non-empty properties before processNormalize31Spec, type: [object, array] remains a JsonSchema instead of becoming an anyOf, so Java generation can still treat it as an object-only model and lose the array alternative; normalize explicit multi-type schemas before the properties-only path (while preserving the existing implied-object behavior).

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java, line 2352:

<comment>Object-first unions that declare properties are still not normalized by this change. Because `normalizeSchema` handles non-empty properties before `processNormalize31Spec`, `type: [object, array]` remains a JsonSchema instead of becoming an `anyOf`, so Java generation can still treat it as an object-only model and lose the array alternative; normalize explicit multi-type schemas before the properties-only path (while preserving the existing implied-object behavior).</comment>

<file context>
@@ -2378,16 +2345,129 @@ protected Schema processNormalize31Spec(Schema schema, Set<Schema> visitedSchema
+                        schema.addAnyOfItem(build31ArraySchema(schema, visitedSchemas));
+                        schema.setItems(null);
+                        break;
+                    case "object":
+                        schema.addAnyOfItem(build31ObjectSchema(schema, visitedSchemas));
+                        schema.setProperties(null);
</file context>

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<Schema> 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<Schema> 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<String, Schema> properties = schema.getProperties();
for (Map.Entry<String, Schema> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Schema> 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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This regression test does not cover object properties or required fields, despite the fix moving those fields into the object anyOf branch. Add such fields to the fixture and assert they survive normalization; otherwise the test can pass while the claimed object conversion is broken.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java, line 787:

<comment>This regression test does not cover object `properties` or `required` fields, despite the fix moving those fields into the object `anyOf` branch. Add such fields to the fixture and assert they survive normalization; otherwise the test can pass while the claimed object conversion is broken.</comment>

<file context>
@@ -744,6 +744,74 @@ public void testNormalize31Parameters() {
+                objOrArrayArray = (ArraySchema) subSchema;
+            }
+        }
+        assertNotNull(objOrArrayObject);
+        assertNotNull(objOrArrayArray);
+        assertEquals(objOrArrayArray.getDescription(), "Related case from the issue.");
</file context>

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");
Expand Down
56 changes: 56 additions & 0 deletions modules/openapi-generator/src/test/resources/3_1/issue_25756.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading