Skip to content

Commit

Permalink
Fix default empty collections when collection is optional (#18080)
Browse files Browse the repository at this point in the history
* Fix optional empty collection as default

* Fix test

* Fix test
  • Loading branch information
MelleD authored Mar 14, 2024
1 parent 6075b8a commit 96bf7ac
Show file tree
Hide file tree
Showing 495 changed files with 1,642 additions and 1,575 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1178,8 +1178,8 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
if (ModelUtils.isArraySchema(schema)) {
if (schema.getDefault() == null) {
// nullable, optional or containerDefaultToNull set to true
if (cp.isNullable || !cp.required || containerDefaultToNull) {
// nullable or containerDefaultToNull set to true
if (cp.isNullable || containerDefaultToNull) {
return null;
} else {
if (ModelUtils.isSet(schema)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,11 @@ public void toDefaultValueTest() {

ModelUtils.setGenerateAliasAsModel(false);
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", schema), schema);
Assert.assertEquals(defaultValue, null);
Assert.assertEquals(defaultValue, "new ArrayList<>()");

ModelUtils.setGenerateAliasAsModel(true);
defaultValue = codegen.toDefaultValue(codegen.fromProperty("", schema), schema);
Assert.assertEquals(defaultValue, null);
Assert.assertEquals(defaultValue, "new ArrayList<>()");

// Create a map schema with additionalProperties type set to array alias
schema = new MapSchema().additionalProperties(new Schema().$ref("#/components/schemas/NestedArray"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void converterInArrayTest() {
Assert.assertEquals(enumVar.dataType, "List<String>");
Assert.assertEquals(enumVar.datatypeWithEnum, "List<NameEnum>");
Assert.assertEquals(enumVar.name, "name");
Assert.assertEquals(enumVar.defaultValue, null);
Assert.assertEquals(enumVar.defaultValue, "new ArrayList<>()");
Assert.assertEquals(enumVar.baseType, "List");
Assert.assertTrue(enumVar.isEnum);

Expand Down Expand Up @@ -108,7 +108,7 @@ public void converterInArrayInArrayTest() {
Assert.assertEquals(enumVar.dataType, "List<List<String>>");
Assert.assertEquals(enumVar.datatypeWithEnum, "List<List<NameEnum>>");
Assert.assertEquals(enumVar.name, "name");
Assert.assertEquals(enumVar.defaultValue, null);
Assert.assertEquals(enumVar.defaultValue, "new ArrayList<>()");
Assert.assertEquals(enumVar.baseType, "List");
Assert.assertTrue(enumVar.isEnum);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void listPropertyTest() {
Assert.assertEquals(property.setter, "setUrls");
Assert.assertEquals(property.dataType, "List<String>");
Assert.assertEquals(property.name, "urls");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property.baseType, "List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -162,7 +162,7 @@ public void setPropertyTest() {
Assert.assertEquals(property.setter, "setUrls");
Assert.assertEquals(property.dataType, "Set<String>");
Assert.assertEquals(property.name, "urls");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.defaultValue, "new LinkedHashSet<>()");
Assert.assertEquals(property.baseType, "Set");
Assert.assertEquals(property.containerType, "set");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -248,7 +248,7 @@ public void list2DPropertyTest() {
Assert.assertEquals(property.setter, "setList2D");
Assert.assertEquals(property.dataType, "List<List<Pet>>");
Assert.assertEquals(property.name, "list2D");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property.baseType, "List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -333,7 +333,7 @@ public void complexListPropertyTest() {
Assert.assertEquals(property.setter, "setChildren");
Assert.assertEquals(property.dataType, "List<Children>");
Assert.assertEquals(property.name, "children");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property.baseType, "List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -396,7 +396,7 @@ public void complexArrayPropertyTest() {
Assert.assertEquals(property.setter, "setChildren");
Assert.assertEquals(property.dataType, "List<Children>");
Assert.assertEquals(property.name, "children");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property.baseType, "List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -429,7 +429,7 @@ public void complexSetPropertyTest() {
Assert.assertEquals(property.setter, "setChildren");
Assert.assertEquals(property.dataType, "Set<Children>");
Assert.assertEquals(property.name, "children");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.defaultValue, "new LinkedHashSet<>()");
Assert.assertEquals(property.baseType, "Set");
Assert.assertEquals(property.containerType, "set");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -466,7 +466,7 @@ public void arrayModelWithItemNameTest() {
Assert.assertEquals(property.setter, "setChildren");
Assert.assertEquals(property.dataType, "List<Child>");
Assert.assertEquals(property.name, "children");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property.baseType, "List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Expand Down Expand Up @@ -974,7 +974,7 @@ public void modelWithWrappedXmlTest() {
Assert.assertEquals(property2.setter, "setArray");
Assert.assertEquals(property2.dataType, "List<String>");
Assert.assertEquals(property2.name, "array");
Assert.assertEquals(property2.defaultValue, null);
Assert.assertEquals(property2.defaultValue, "new ArrayList<>()");
Assert.assertEquals(property2.baseType, "List");
Assert.assertTrue(property2.isContainer);
Assert.assertTrue(property2.isXmlWrapped);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4518,4 +4518,34 @@ public void testLombokAnnotations() throws IOException {
.assertMethod("equals")
;
}

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

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_1/petstore.yaml", null, new ParseOptions()).getOpenAPI();
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_CLOUD_LIBRARY);
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xyz.model");
codegen.additionalProperties().put(CodegenConstants.API_NAME_SUFFIX, "Controller");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.controller");
codegen.additionalProperties().put(CodegenConstants.MODEL_NAME_SUFFIX, "Dto");


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

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

JavaFileAssert.assertThat(files.get("PetDto.java"))
.fileContains("private List<@Valid TagDto> tags = new ArrayList<>();")
.fileContains("private List<String> photoUrls = new ArrayList<>();");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static ArrayStringEnumDefaultEnum fromValue(String value) {
private List<Integer> arrayIntegerDefault = new ArrayList<>(Arrays.asList(1, 3));

public static final String JSON_PROPERTY_ARRAY_STRING = "array_string";
private List<String> arrayString;
private List<String> arrayString = new ArrayList<>();

public static final String JSON_PROPERTY_ARRAY_STRING_NULLABLE = "array_string_nullable";
private JsonNullable<List<String>> arrayStringNullable = JsonNullable.<List<String>>undefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class Pet {
private List<String> photoUrls = new ArrayList<>();

public static final String JSON_PROPERTY_TAGS = "tags";
private List<Tag> tags;
private List<Tag> tags = new ArrayList<>();

/**
* pet status in the store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.5.0-SNAPSHOT")
public class TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter {
public static final String JSON_PROPERTY_VALUES = "values";
private List<String> values;
private List<String> values = new ArrayList<>();

public TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter() {
}
Expand Down
Loading

0 comments on commit 96bf7ac

Please sign in to comment.