Skip to content

Commit 3dcb59e

Browse files
committed
update java generators to respect default value
1 parent 4974932 commit 3dcb59e

9 files changed

Lines changed: 26 additions & 10 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
336336

337337
@Setter @Getter boolean arrayDefaultToEmpty, arrayNullableDefaultToEmpty, arrayOptionalNullableDefaultToEmpty, arrayOptionalDefaultToEmpty;
338338
@Setter @Getter boolean mapDefaultToEmpty, mapNullableDefaultToEmpty, mapOptionalNullableDefaultToEmpty, mapOptionalDefaultToEmpty;
339+
@Setter @Getter protected boolean defaultToEmptyContainer;
339340
final String DEFAULT_TO_EMPTY_CONTAINER = "defaultToEmptyContainer";
340341
final List EMPTY_LIST = new ArrayList();
341342

@@ -4235,7 +4236,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
42354236
}
42364237

42374238
// override defaultValue if it's not set and defaultToEmptyContainer is set
4238-
if (p.getDefault() == null && additionalProperties.containsKey("defaultToEmptyContainer")) {
4239+
if (p.getDefault() == null && defaultToEmptyContainer) {
42394240
updateDefaultToEmptyContainer(property, p);
42404241
}
42414242

@@ -4328,6 +4329,8 @@ void parseDefaultToEmptyContainer(String input) {
43284329
LOGGER.error("Skipped invalid container type `{}` in the rule `{}`.", containerType, input);
43294330
}
43304331
}
4332+
4333+
defaultToEmptyContainer = true;
43314334
}
43324335

43334336
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,10 @@ public String toArrayDefaultValue(CodegenProperty cp, Schema schema) {
13061306
public String toDefaultValue(CodegenProperty cp, Schema schema) {
13071307
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
13081308
if (ModelUtils.isArraySchema(schema)) {
1309-
if (schema.getDefault() == null) {
1309+
if (defaultToEmptyContainer) {
1310+
// if default to empty container option is set, respect the default values provided in the spec
1311+
return toArrayDefaultValue(cp, schema);
1312+
} else if (schema.getDefault() == null) {
13101313
// nullable or containerDefaultToNull set to true
13111314
if (cp.isNullable || containerDefaultToNull) {
13121315
return null;
@@ -1323,6 +1326,16 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
13231326
return null;
13241327
}
13251328

1329+
if (defaultToEmptyContainer) {
1330+
// respect the default values provided in the spec when the option is enabled
1331+
if (schema.getDefault() != null) {
1332+
return String.format(Locale.ROOT, "new %s<>()",
1333+
instantiationTypes().getOrDefault("map", "HashMap"));
1334+
} else {
1335+
return null;
1336+
}
1337+
}
1338+
13261339
// nullable or containerDefaultToNull set to true
13271340
if (cp.isNullable || containerDefaultToNull) {
13281341
return null;

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/AllOfModelArrayAnyOfAllOfLinkListColumn1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class AllOfModelArrayAnyOfAllOfLinkListColumn1 {
5656
public static final String SERIALIZED_NAME_VALUE = "value";
5757
@SerializedName(SERIALIZED_NAME_VALUE)
5858
@javax.annotation.Nonnull
59-
private List<AllOfModelArrayAnyOfAllOfLinkListColumn1Value> value = new ArrayList<>();
59+
private List<AllOfModelArrayAnyOfAllOfLinkListColumn1Value> value;
6060

6161
public AllOfModelArrayAnyOfAllOfLinkListColumn1() {
6262
}

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/NewPet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public class NewPet {
8888
public static final String SERIALIZED_NAME_PHOTO_URLS = "photoUrls";
8989
@SerializedName(SERIALIZED_NAME_PHOTO_URLS)
9090
@javax.annotation.Nonnull
91-
private List<String> photoUrls = new ArrayList<>();
91+
private List<String> photoUrls;
9292

9393
public static final String SERIALIZED_NAME_TAGS = "tags";
9494
@SerializedName(SERIALIZED_NAME_TAGS)

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/Pet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class Pet {
7272
public static final String SERIALIZED_NAME_PHOTO_URLS = "photoUrls";
7373
@SerializedName(SERIALIZED_NAME_PHOTO_URLS)
7474
@javax.annotation.Nonnull
75-
private List<String> photoUrls = new ArrayList<>();
75+
private List<String> photoUrls;
7676

7777
public static final String SERIALIZED_NAME_TAGS = "tags";
7878
@SerializedName(SERIALIZED_NAME_TAGS)

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/PetComposition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class PetComposition {
7272
public static final String SERIALIZED_NAME_PHOTO_URLS = "photoUrls";
7373
@SerializedName(SERIALIZED_NAME_PHOTO_URLS)
7474
@javax.annotation.Nonnull
75-
private List<String> photoUrls = new ArrayList<>();
75+
private List<String> photoUrls;
7676

7777
public static final String SERIALIZED_NAME_TAGS = "tags";
7878
@SerializedName(SERIALIZED_NAME_TAGS)

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/PetRef.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class PetRef {
7272
public static final String SERIALIZED_NAME_PHOTO_URLS = "photoUrls";
7373
@SerializedName(SERIALIZED_NAME_PHOTO_URLS)
7474
@javax.annotation.Nonnull
75-
private List<String> photoUrls = new ArrayList<>();
75+
private List<String> photoUrls;
7676

7777
public static final String SERIALIZED_NAME_TAGS = "tags";
7878
@SerializedName(SERIALIZED_NAME_TAGS)

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/PetUsingAllOf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class PetUsingAllOf {
7272
public static final String SERIALIZED_NAME_PHOTO_URLS = "photoUrls";
7373
@SerializedName(SERIALIZED_NAME_PHOTO_URLS)
7474
@javax.annotation.Nonnull
75-
private List<String> photoUrls = new ArrayList<>();
75+
private List<String> photoUrls;
7676

7777
public static final String SERIALIZED_NAME_TAGS = "tags";
7878
@SerializedName(SERIALIZED_NAME_TAGS)

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/PetWithRequiredTags.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ public class PetWithRequiredTags {
7272
public static final String SERIALIZED_NAME_PHOTO_URLS = "photoUrls";
7373
@SerializedName(SERIALIZED_NAME_PHOTO_URLS)
7474
@javax.annotation.Nonnull
75-
private List<String> photoUrls = new ArrayList<>();
75+
private List<String> photoUrls;
7676

7777
public static final String SERIALIZED_NAME_TAGS = "tags";
7878
@SerializedName(SERIALIZED_NAME_TAGS)
7979
@javax.annotation.Nonnull
80-
private List<Tag> tags = new ArrayList<>();
80+
private List<Tag> tags;
8181

8282
/**
8383
* pet status in the store

0 commit comments

Comments
 (0)