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 @@ -1380,6 +1380,22 @@ public String toArrayDefaultValue(CodegenProperty cp, Schema schema) {

@Override
public String toDefaultValue(CodegenProperty cp, Schema schema) {
// When generateAliasAsModel is enabled, a property that $refs an array/map alias is typed
// as the generated alias model (e.g. "ItemArray extends ArrayList<Item>"), not the inlined
// collection. Dereferencing below would otherwise yield a collection default such as
// "new ArrayList<>()", which is not assignable to the alias type and does not compile.
// Use the alias model's own default instead.
// See https://github.com/OpenAPITools/openapi-generator/issues/23988
if (schema.get$ref() != null && !cp.isArray && !cp.isMap) {
Schema referencedSchema = ModelUtils.getReferencedSchema(this.openAPI, schema);
if (ModelUtils.isArraySchema(referencedSchema)
|| (ModelUtils.isMapSchema(referencedSchema) && !ModelUtils.isComposedSchema(referencedSchema))) {
if (cp.isNullable || containerDefaultToNull) {
return null;
}
return "new " + cp.datatypeWithEnum + "()";
}
}
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
if (ModelUtils.isArraySchema(schema)) {
if (defaultToEmptyContainer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6582,6 +6582,39 @@ public void shouldAddNullableImportForArrayTypeModels() throws IOException {
.hasImports("org.springframework.lang.Nullable");
}

@Test
public void shouldUseAliasModelDefaultValueForAliasAsModelProperties_issue23988() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/spring/issue_23988.yaml");
final SpringCodegen codegen = new SpringCodegen();
codegen.setOpenAPI(openAPI);
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
codegen.additionalProperties().put(CodegenConstants.GENERATE_ALIAS_AS_MODEL, "true");

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
generator.setGenerateMetadata(false);
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");

Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

// A property that $refs an array/map alias is typed as the generated alias model, so its
// default value must instantiate the alias model rather than the inlined collection type
// (which is not assignable to the alias and does not compile).
JavaFileAssert.assertThat(files.get("MyObject.java"))
.fileContains("private ItemArray itemArray = new ItemArray();")
.fileContains("private ItemMap itemMap = new ItemMap();");
}

@Test
public void testClientRegistrationIdAnnotation() throws IOException {
final SpringCodegen codegen = new SpringCodegen();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
openapi: 3.0.1
info:
title: Test
version: 0.0.1
paths: {}
components:
schemas:
Item:
type: object
properties:
property:
type: string
ItemArray:
type: array
items:
$ref: '#/components/schemas/Item'
ItemMap:
type: object
additionalProperties:
$ref: '#/components/schemas/Item'
MyObject:
type: object
properties:
itemArray:
$ref: '#/components/schemas/ItemArray'
itemMap:
$ref: '#/components/schemas/ItemMap'
Loading