Skip to content

Fix #25756: support array/object in the OAS 3.1 multi-type normalizer for Java - #25761

Open
Inf1mum wants to merge 1 commit into
OpenAPITools:masterfrom
Inf1mum:fix/25756-java-multi-type-attr
Open

Inf1mum wants to merge 1 commit into
OpenAPITools:masterfrom
Inf1mum:fix/25756-java-multi-type-attr

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #25756

For type: [array, string] (2+ non-null types) the OAS 3.1 normalizer handled only scalar types; array/object hit the "not yet supported" fallback and left the schema half-converted (types kept alongside a partial anyOf). Downstream, getType() returned the first remaining type, InlineModelResolver took the array branch without extracting the property, and getSchemaType() composed a phantom anyOf<string> name — java emitted AnyOfString<String> with an import of a never-generated class (compile failure).

OpenAPINormalizer.processNormalize31Spec now converts array (items + array constraints moved in) and object (properties/additionalProperties/required moved in) multi-type schemas via new build31ArraySchema/build31ObjectSchema helpers; types is cleared after full conversion so a real wrapper model is extracted, and unknown types keep the old error path.

Reproduced the issue output first; after the fix okhttp-gson/native/spring-http-interface generate proper CompanyTradingNames/CompanyObjOrArray wrapper models with runtime adapters, and the generated clients compile. New normalizer test + fixture; OpenAPINormalizerTest 73/73, DefaultCodegenTest 185/185, CompositionNormalizationTest 13/13 green; okhttp-gson-3.1 sample regen shows only pre-existing stale-sample drift.


Summary by cubic

Fixes #25756. The OAS 3.1 multi-type normalizer now converts array and object types instead of leaving the schema half-converted, which previously caused Java generators to emit a phantom AnyOfString<String> model, import a never-generated class, and fail compilation.

  • Adds build31ArraySchema/build31ObjectSchema helpers that move items, properties, additionalProperties, and related constraints into the converted anyOf branches.
  • Clears types after a full conversion so a real wrapper model is extracted; unknown types keep the old error path.
  • Adds a normalizer test and fixture covering array/string, object/array, object/string, and nullable multi-type schemas.

Written for commit 8209b40. Summary will update on new commits.

View guided diff

…ti-type schemas

Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

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.

2 issues found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. 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. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java">

<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java:787">
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.</violation>
</file>

<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java:2352">
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).</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

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>

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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][JAVA] Bug generating attribute with multiple types in OAS 3.1

1 participant