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 @@ -1558,7 +1558,17 @@ private String toObjectDefaultValue(CodegenProperty cp, Object defaultValue, Map
}

String defaultPropertyExpression = null;
if(ModelUtils.isLongSchema(propertySchema)) {
if(ModelUtils.isEnumSchema(ModelUtils.getReferencedSchema(this.openAPI, propertySchema))) {
// Enum-typed property: render the enum constant (e.g. `OutputFormat.OrderEnum.SIMILARITY`)
// rather than a raw quoted string, which would not compile (see #24298).
CodegenProperty enumProperty = fromProperty(key, propertySchema);
String enumType = enumProperty.isEnum
// an inline enum is generated as a nested class of the containing object type
? cp.datatypeWithEnum + "." + enumProperty.datatypeWithEnum
// a `$ref` to a named enum is a top-level type
: enumProperty.datatypeWithEnum;
defaultPropertyExpression = enumType + "." + toEnumVarName(value.asText(), enumProperty.dataType);
} else if(ModelUtils.isLongSchema(propertySchema)) {
defaultPropertyExpression = value.asText()+"l";
} else if(ModelUtils.isIntegerSchema(propertySchema)) {
defaultPropertyExpression = value.asText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,28 @@ public void toDefaultValueForComposedObjectWithDefaultTest() {
Assert.assertEquals(rendered, "new " + cp.datatypeWithEnum + "().one(\"one\").two(\"two\")");
}

@Test
public void toDefaultValueForObjectWithEnumPropertyDefaultTest() {
// An object default that contains an enum property must render the enum constant
// (e.g. `OutputFormat.OrderEnum.SIMILARITY`) rather than a raw quoted string, which
// would not compile (see #24298).
ObjectSchema outputFormat = new ObjectSchema();
outputFormat.addProperty("order", new StringSchema()._enum(java.util.Arrays.asList("IMPORTANCE", "SIMILARITY")));
outputFormat.addProperty("limit", new IntegerSchema());
Map<String, Object> defaultValue = new LinkedHashMap<>();
defaultValue.put("order", "SIMILARITY");
defaultValue.put("limit", 10);
outputFormat.setDefault(defaultValue);

codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("OutputFormat", outputFormat)));

CodegenProperty cp = codegen.fromProperty("format", new Schema<>().$ref("#/components/schemas/OutputFormat"));
String rendered = codegen.toDefaultValue(cp, outputFormat);

Assert.assertEquals(rendered, "new " + cp.datatypeWithEnum + "().order("
+ cp.datatypeWithEnum + ".OrderEnum.SIMILARITY).limit(10)");
}

@Test
public void dateDefaultValueIsIsoDate() {
final OpenAPI openAPI = FLATTENED_SPEC.get("3_0/spring/date-time-parameter-types-for-testing");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,25 @@ public void testJavaClientDefaultValues_issueNoNumber() {
.asString().endsWith("= new ArrayList<>();");
}

@Test
public void testObjectDefaultWithEnumProperty_issue24298() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName(JAVA_GENERATOR)
.setInputSpec("src/test/resources/bugs/issue_24298.yaml")
.setOutputDir(output.toString().replace("\\", "/"));

Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

// The `format` field's default is an OutputFormat whose `order` property is an inline enum. It must
// be rendered as the enum constant, not a raw quoted string, otherwise the code does not compile (#24298).
JavaFileAssert.assertThat(files.get("Formatter.java"))
.assertProperty("format")
.asString()
.contains("new OutputFormat().order(OutputFormat.OrderEnum.SIMILARITY).limit(10)");
}

@Test
public void testWebClientJsonCreatorWithNullable_issue12790() {
final Path output = newTempFolder();
Expand Down
25 changes: 25 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_24298.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
openapi: "3.1.0"
info:
title: Enum Default Issue Demo
version: 1.0.0
paths: {}
components:
schemas:
OutputFormat:
type: object
properties:
order:
type: string
enum:
- IMPORTANCE
- SIMILARITY
limit:
type: integer
default:
order: SIMILARITY
limit: 10
Formatter:
type: object
properties:
format:
$ref: '#/components/schemas/OutputFormat'
Loading