[Java] Render object default for composed ($ref + default) schemas (#23795)
Fixes #23795
Problem
When a property is declared as a $ref to an object schema with a sibling default (or, equivalently, an explicit allOf), the swagger-parser represents it as a composed schema — the object's properties live in the allOf members rather than directly on the schema.
AbstractJavaCodegen.toDefaultValue(...) handled this in the isComposedSchema(...) branch by falling through to super.toDefaultValue(schema), which emits the raw default value as Java:
private Nested test = {"one":"one","two":"two"}; // does not compile
The semantically-equivalent inline-object spec already rendered correctly:
private DtoTest test = new DtoTest().one("one").two("two");
Change
- Extracted the existing object-default rendering into a private
toObjectDefaultValue(cp, defaultValue, propertySchemas)helper (behaviour-preserving for the plainisObjectSchemapath). - In the composed-schema branch, resolve the effective property schemas from the
allOfmembers (dereferencing$refs) via a newgetComposedSchemaProperties(...)helper, then render the default through the same fluent-builder logic. - When no object properties can be resolved, return
nullinstead of emitting uncompilable output — so the previously broken cases now at worst omit the default rather than producing code that fails to compile.
For the reporter's spec this now generates:
private Nested test = new Nested().one("one").two("two");
Tests
Added AbstractJavaCodegenTest.toDefaultValueForComposedObjectWithDefaultTest, which builds a composed (allOf → $ref) schema with an object default and asserts the rendered fluent-builder expression.
Tests run: 66, Failures: 0, Errors: 0, Skipped: 0
-- in org.openapitools.codegen.java.AbstractJavaCodegenTest
Verification done: ran the modules/openapi-generator module test class AbstractJavaCodegenTest (JDK 21) — all 66 tests pass, including the pre-existing toDefaultValueTest that exercises the refactored object-default path, confirming no behaviour change there.
I did not run the full ./bin/generate-samples.sh regeneration: this code path only triggers for the composed-$ref-plus-default pattern, which previously produced uncompilable output, so no committed sample relies on the old behaviour and a regeneration is not expected to change any sample. Happy to regenerate any specific generator samples if a maintainer would like that included.
PR checklist
- Read the contribution guidelines.
- Ran full
./mvnw clean package+ sample regeneration (see note above — targeted module tests run instead). - Filed against
master.
Summary by cubic
Fix Java generator to render object defaults for composed ($ref + default or allOf) schemas as fluent builder expressions instead of raw JSON, preventing uncompilable code. Addresses #23795.
- Bug Fixes
- Resolve properties from
allOf(dereferencing$ref) and render defaults via the object builder path; now generatesnew Nested().one("one").two("two"). - Extracted
toObjectDefaultValue(...)andgetComposedSchemaProperties(...); plain object behavior unchanged. - If no properties are resolved, omit the default (return
null) instead of emitting invalid Java. - Added
AbstractJavaCodegenTest.toDefaultValueForComposedObjectWithDefaultTest; existing tests remain green.
- Resolve properties from