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 @@ -1598,16 +1598,16 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
}

if ("array".equals(property.containerType)) {
model.imports.add("ArrayList");
model.imports.add(instantiationTypes.get("array"));
model.imports.add("Arrays");
} else if ("set".equals(property.containerType)) {
model.imports.add("LinkedHashSet");
model.imports.add(instantiationTypes.get("set"));
if (!openApiNullable || !property.isNullable) { // cannot be wrapped to nullable
model.imports.add("JsonDeserialize");
property.vendorExtensions.put("x-setter-extra-annotation", "@JsonDeserialize(as = LinkedHashSet.class)");
property.vendorExtensions.put("x-setter-extra-annotation", "@JsonDeserialize(as = " + instantiationTypes.get("set") + ".class)");
}
} else if ("map".equals(property.containerType)) {
model.imports.add("HashMap");
model.imports.add(instantiationTypes.get("map"));
}

if (!BooleanUtils.toBoolean(model.isEnum)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,40 @@ public void maplikeDefaultValueForModelWithStringToModelMapping() {
Assert.assertEquals(defaultValue, "null", "Expected string-ref map aliased model to default to null since nullable is not set to tru");
}

@Test
public void uniqueItemsSet() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/setSchema.yaml");
codegen.additionalProperties().put(CodegenConstants.GENERATE_ALIAS_AS_MODEL, true);
codegen.setOpenAPI(openAPI);

Schema schema = openAPI.getComponents().getSchemas().get("Response");
CodegenModel cm = codegen.fromModel("Response", schema);
Assert.assertFalse(cm.getImports().contains("List"), "expect List import");
Assert.assertFalse(cm.getImports().contains("ArrayList"), "expect ArrayList import");
Assert.assertTrue(cm.getImports().contains("Set"), "expect no Set import");
Assert.assertTrue(cm.getImports().contains("LinkedHashSet"), "expect no LinkedHashSet import");
}

@Test
public void uniqueItemsArray() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
// overrides to keep uniqueItems=true arrays as arrays in generated code
codegen.typeMapping().put("set", codegen.typeMapping().get("array"));
codegen.instantiationTypes().put("set", codegen.instantiationTypes().get("array"));

final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/setSchema.yaml");
codegen.additionalProperties().put(CodegenConstants.GENERATE_ALIAS_AS_MODEL, true);
codegen.setOpenAPI(openAPI);

Schema schema = openAPI.getComponents().getSchemas().get("Response");
CodegenModel cm = codegen.fromModel("Response", schema);
Assert.assertTrue(cm.getImports().contains("List"), "expect List import");
Assert.assertTrue(cm.getImports().contains("ArrayList"), "expect ArrayList import");
Assert.assertFalse(cm.getImports().contains("Set"), "expect no Set import");
Assert.assertFalse(cm.getImports().contains("LinkedHashSet"), "expect no LinkedHashSet import");
}

@Test
public void srcMainFolderShouldNotBeOperatingSystemSpecificPaths() {
// it's not responsibility of the generator to fix OS-specific paths. This is left to template manager.
Expand Down
26 changes: 26 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/setSchema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: 3.0.0
paths:
/examples:
get:
tags:
- Examples
summary: Get a list of transactions
operationId: getFilteredTransactions
responses:
200:
description: A list of deleted consumers
content:
application/json:
schema:
$ref: '#/components/schemas/Response'

components:
schemas:
Response:
type: object
properties:
something:
type: array
uniqueItems: true
items:
type: string