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 @@ -1516,7 +1516,12 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
if (!propertySchemas.isEmpty()) {
return toObjectDefaultValue(cp, schema.getDefault(), propertySchemas);
}
return null;
// No object properties resolved: the composition wraps a non-object, e.g. an `allOf`
// to an enum or scalar (`allOf: [{$ref: '#/.../CurrencyCode'}]` + sibling `default`).
// There is nothing to build via toObjectDefaultValue, so defer to the base behavior,
// which emits the raw default for later enum var-name / scalar conversion. Returning
// null here dropped the default and regressed enum defaults (see #24384).
return super.toDefaultValue(schema);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,26 @@ public void toDefaultValueForComposedObjectWithDefaultTest() {
Assert.assertEquals(rendered, "new " + cp.datatypeWithEnum + "().one(\"one\").two(\"two\")");
}

@Test
public void toDefaultValueForComposedEnumWithDefaultTest() {
// A `$ref` to an enum schema combined with a sibling `default` is parsed as a composed (allOf)
// schema that wraps a non-object, so getComposedSchemaProperties resolves no properties. The raw
// enum default must still be preserved (for later enum var-name conversion, e.g. `CurrencyCode.EUR`)
// rather than dropped to null, which regressed the standard `allOf` + sibling-`default` idiom (see #24384).
codegen.setDateLibrary("java8");
codegen.setOpenAPI(new OpenAPI().components(new Components()
.addSchemas("CurrencyCode", new StringSchema()._enum(Arrays.asList("EUR", "USD")))));

Schema<?> composed = new ComposedSchema()
.addAllOfItem(new Schema<>().$ref("#/components/schemas/CurrencyCode"));
composed.setDefault("EUR");

CodegenProperty cp = codegen.fromProperty("currency", composed);
String rendered = codegen.toDefaultValue(cp, composed);

Assert.assertEquals(rendered, "EUR");
}

@Test
public void dateDefaultValueIsIsoDate() {
final OpenAPI openAPI = FLATTENED_SPEC.get("3_0/spring/date-time-parameter-types-for-testing");
Expand Down
Loading