Skip to content

Commit 8253c28

Browse files
authored
[Java] Replace "useNullForUnknownEnumValue" option with the nullable attribute (#3455)
* add nullable support to enum * update test spec with nullable enum * update samples * update samples
1 parent fabe021 commit 8253c28

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+128
-152
lines changed

bin/java-petstore-jersey1.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fi
2727

2828
# if you've executed sbt assembly previously it will use that instead.
2929
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
30-
ags="generate --artifact-id petstore-java-client-jersey1 -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -o samples/client/petstore/java/jersey1 --additional-properties hideGenerationTimestamp=true --library=jersey1 --additional-properties useNullForUnknownEnumValue=true $@"
30+
ags="generate --artifact-id petstore-java-client-jersey1 -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -o samples/client/petstore/java/jersey1 --additional-properties hideGenerationTimestamp=true --library=jersey1 $@"
3131

3232
echo "Removing files and folders under samples/client/petstore/java/jersey1/src/main"
3333
rm -rf samples/client/petstore/java/jersey1/src/main

bin/java-petstore-jersey2.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
{
22
"library": "jersey2",
3-
"artifactId": "petstore-jersey2",
4-
"additionalProperties" : {
5-
"useNullForUnknownEnumValue" : true
6-
}
3+
"artifactId": "petstore-jersey2"
74
}

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

Lines changed: 77 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class CodegenModel {
6161
public Set<String> allMandatory = new TreeSet<String>(); // with parent's required properties
6262

6363
public Set<String> imports = new TreeSet<String>();
64-
public boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, hasRequired, hasOptional, isArrayModel, hasChildren, isMapModel;
64+
public boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArrayModel, hasChildren, isMapModel;
6565
public boolean hasOnlyReadOnly = true; // true if all properties are read-only
6666
public ExternalDocumentation externalDocumentation;
6767

@@ -118,6 +118,7 @@ public String toString() {
118118
.append("hasMoreModels", hasMoreModels)
119119
.append("hasEnums", hasEnums)
120120
.append("isEnum", isEnum)
121+
.append("isNullable", isEnum)
121122
.append("hasRequired", hasRequired)
122123
.append("hasOptional", hasOptional)
123124
.append("isArrayModel", isArrayModel)
@@ -138,86 +139,86 @@ public boolean equals(Object o) {
138139
CodegenModel that = (CodegenModel) o;
139140

140141
return Objects.equals(parent, that.parent) &&
141-
Objects.equals(parentSchema, that.parentSchema) &&
142-
Objects.equals(interfaces, that.interfaces) &&
143-
Objects.equals(allParents, that.allParents) &&
144-
Objects.equals(parentModel, that.parentModel) &&
145-
Objects.equals(interfaceModels, that.interfaceModels) &&
146-
Objects.equals(name, that.name) &&
147-
Objects.equals(classname, that.classname) &&
148-
Objects.equals(title, that.title) &&
149-
Objects.equals(description, that.description) &&
150-
Objects.equals(classVarName, that.classVarName) &&
151-
Objects.equals(modelJson, that.modelJson) &&
152-
Objects.equals(dataType, that.dataType) &&
153-
Objects.equals(xmlPrefix, that.xmlPrefix) &&
154-
Objects.equals(xmlNamespace, that.xmlNamespace) &&
155-
Objects.equals(xmlName, that.xmlName) &&
156-
Objects.equals(classFilename, that.classFilename) &&
157-
Objects.equals(unescapedDescription, that.unescapedDescription) &&
158-
Objects.equals(discriminator, that.discriminator) &&
159-
Objects.equals(defaultValue, that.defaultValue) &&
160-
Objects.equals(vars, that.vars) &&
161-
Objects.equals(requiredVars, that.requiredVars) &&
162-
Objects.equals(optionalVars, that.optionalVars) &&
163-
Objects.equals(allVars, that.allVars) &&
164-
Objects.equals(allowableValues, that.allowableValues) &&
165-
Objects.equals(mandatory, that.mandatory) &&
166-
Objects.equals(allMandatory, that.allMandatory) &&
167-
Objects.equals(imports, that.imports) &&
168-
Objects.equals(hasVars, that.hasVars) &&
169-
Objects.equals(emptyVars, that.emptyVars) &&
170-
Objects.equals(hasMoreModels, that.hasMoreModels) &&
171-
Objects.equals(hasEnums, that.hasEnums) &&
172-
Objects.equals(isEnum, that.isEnum) &&
173-
Objects.equals(externalDocumentation, that.externalDocumentation) &&
174-
Objects.equals(hasOnlyReadOnly, that.hasOnlyReadOnly) &&
175-
Objects.equals(hasChildren, that.hasChildren) &&
176-
Objects.equals(parentVars, that.parentVars) &&
177-
Objects.equals(vendorExtensions, that.vendorExtensions);
142+
Objects.equals(parentSchema, that.parentSchema) &&
143+
Objects.equals(interfaces, that.interfaces) &&
144+
Objects.equals(allParents, that.allParents) &&
145+
Objects.equals(parentModel, that.parentModel) &&
146+
Objects.equals(interfaceModels, that.interfaceModels) &&
147+
Objects.equals(name, that.name) &&
148+
Objects.equals(classname, that.classname) &&
149+
Objects.equals(title, that.title) &&
150+
Objects.equals(description, that.description) &&
151+
Objects.equals(classVarName, that.classVarName) &&
152+
Objects.equals(modelJson, that.modelJson) &&
153+
Objects.equals(dataType, that.dataType) &&
154+
Objects.equals(xmlPrefix, that.xmlPrefix) &&
155+
Objects.equals(xmlNamespace, that.xmlNamespace) &&
156+
Objects.equals(xmlName, that.xmlName) &&
157+
Objects.equals(classFilename, that.classFilename) &&
158+
Objects.equals(unescapedDescription, that.unescapedDescription) &&
159+
Objects.equals(discriminator, that.discriminator) &&
160+
Objects.equals(defaultValue, that.defaultValue) &&
161+
Objects.equals(vars, that.vars) &&
162+
Objects.equals(requiredVars, that.requiredVars) &&
163+
Objects.equals(optionalVars, that.optionalVars) &&
164+
Objects.equals(allVars, that.allVars) &&
165+
Objects.equals(allowableValues, that.allowableValues) &&
166+
Objects.equals(mandatory, that.mandatory) &&
167+
Objects.equals(allMandatory, that.allMandatory) &&
168+
Objects.equals(imports, that.imports) &&
169+
Objects.equals(hasVars, that.hasVars) &&
170+
Objects.equals(emptyVars, that.emptyVars) &&
171+
Objects.equals(hasMoreModels, that.hasMoreModels) &&
172+
Objects.equals(hasEnums, that.hasEnums) &&
173+
Objects.equals(isEnum, that.isEnum) &&
174+
Objects.equals(externalDocumentation, that.externalDocumentation) &&
175+
Objects.equals(hasOnlyReadOnly, that.hasOnlyReadOnly) &&
176+
Objects.equals(hasChildren, that.hasChildren) &&
177+
Objects.equals(parentVars, that.parentVars) &&
178+
Objects.equals(vendorExtensions, that.vendorExtensions);
178179
}
179180

180181
@Override
181182
public int hashCode() {
182183
return Objects.hash(
183-
parent,
184-
parentSchema,
185-
interfaces,
186-
allParents,
187-
parentModel,
188-
interfaceModels,
189-
name,
190-
classname,
191-
title,
192-
description,
193-
classVarName,
194-
modelJson,
195-
dataType,
196-
xmlPrefix,
197-
xmlNamespace,
198-
xmlName,
199-
classFilename,
200-
unescapedDescription,
201-
discriminator,
202-
defaultValue,
203-
vars,
204-
requiredVars,
205-
optionalVars,
206-
allVars,
207-
allowableValues,
208-
mandatory,
209-
allMandatory,
210-
imports,
211-
hasVars,
212-
emptyVars,
213-
hasMoreModels,
214-
hasEnums,
215-
isEnum,
216-
externalDocumentation,
217-
vendorExtensions,
218-
hasOnlyReadOnly,
219-
hasChildren,
220-
parentVars);
184+
parent,
185+
parentSchema,
186+
interfaces,
187+
allParents,
188+
parentModel,
189+
interfaceModels,
190+
name,
191+
classname,
192+
title,
193+
description,
194+
classVarName,
195+
modelJson,
196+
dataType,
197+
xmlPrefix,
198+
xmlNamespace,
199+
xmlName,
200+
classFilename,
201+
unescapedDescription,
202+
discriminator,
203+
defaultValue,
204+
vars,
205+
requiredVars,
206+
optionalVars,
207+
allVars,
208+
allowableValues,
209+
mandatory,
210+
allMandatory,
211+
imports,
212+
hasVars,
213+
emptyVars,
214+
hasMoreModels,
215+
hasEnums,
216+
isEnum,
217+
externalDocumentation,
218+
vendorExtensions,
219+
hasOnlyReadOnly,
220+
hasChildren,
221+
parentVars);
221222
}
222223

223224
public String getParent() {

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ private String getPrimitiveType(Schema schema) {
15721572
} else if (ModelUtils.isURISchema(schema)) {
15731573
return "URI";
15741574
} else if (ModelUtils.isStringSchema(schema)) {
1575-
if(typeMapping.containsKey(schema.getFormat())) {
1575+
if (typeMapping.containsKey(schema.getFormat())) {
15761576
// If the format matches a typeMapping (supplied with the --typeMappings flag)
15771577
// then treat the format as a primitive type.
15781578
// This allows the typeMapping flag to add a new custom type which can then
@@ -1884,19 +1884,16 @@ public CodegenModel fromModel(String name, Schema schema) {
18841884
if (ModelUtils.isMapSchema(schema)) {
18851885
addAdditionPropertiesToCodeGenModel(m, schema);
18861886
m.isMapModel = true;
1887-
}
1888-
else if (ModelUtils.isIntegerSchema(schema)) { // integer type
1887+
} else if (ModelUtils.isIntegerSchema(schema)) { // integer type
18891888
m.isNumeric = Boolean.TRUE;
18901889
if (ModelUtils.isLongSchema(schema)) { // int64/long format
18911890
m.isLong = Boolean.TRUE;
18921891
} else { // int32 format
18931892
m.isInteger = Boolean.TRUE;
18941893
}
1895-
}
1896-
else if (ModelUtils.isStringSchema(schema)) {
1894+
} else if (ModelUtils.isStringSchema(schema)) {
18971895
m.isString = Boolean.TRUE;
1898-
}
1899-
else if (ModelUtils.isNumberSchema(schema)) {
1896+
} else if (ModelUtils.isNumberSchema(schema)) {
19001897
m.isNumeric = Boolean.TRUE;
19011898
if (ModelUtils.isFloatSchema(schema)) { // float
19021899
m.isFloat = Boolean.TRUE;
@@ -1907,6 +1904,10 @@ else if (ModelUtils.isNumberSchema(schema)) {
19071904
}
19081905
}
19091906

1907+
if (Boolean.TRUE.equals(schema.getNullable())) {
1908+
m.isNullable = Boolean.TRUE;
1909+
}
1910+
19101911
// passing null to allProperties and allRequired as there's no parent
19111912
addVars(m, unaliasPropertySchema(schema.getProperties()), schema.getRequired(), null, null);
19121913
}
@@ -2650,7 +2651,7 @@ public CodegenOperation fromOperation(String path,
26502651
if (requestBody != null) {
26512652
if (getContentType(requestBody) != null &&
26522653
(getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") ||
2653-
getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("multipart/form-data"))) {
2654+
getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("multipart/form-data"))) {
26542655
// process form parameters
26552656
formParams = fromRequestBodyToFormParameters(requestBody, imports);
26562657
for (CodegenParameter cp : formParams) {
@@ -3018,7 +3019,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
30183019
}
30193020

30203021
Schema s;
3021-
if(parameter.getSchema() != null) {
3022+
if (parameter.getSchema() != null) {
30223023
s = parameter.getSchema();
30233024
} else if (parameter.getContent() != null) {
30243025
Content content = parameter.getContent();
@@ -4942,7 +4943,7 @@ protected void updateOption(String key, String defaultValue) {
49424943
}
49434944

49444945
protected void removeOption(String key) {
4945-
for(int i = 0; i < cliOptions.size(); i++) {
4946+
for (int i = 0; i < cliOptions.size(); i++) {
49464947
if (key.equals(cliOptions.get(i).getOpt())) {
49474948
cliOptions.remove(i);
49484949
break;

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
5252
public static final String SUPPORT_JAVA6 = "supportJava6";
5353
public static final String DISABLE_HTML_ESCAPING = "disableHtmlEscaping";
5454
public static final String BOOLEAN_GETTER_PREFIX = "booleanGetterPrefix";
55-
public static final String USE_NULL_FOR_UNKNOWN_ENUM_VALUE = "useNullForUnknownEnumValue";
5655

5756
protected String dateLibrary = "threetenbp";
5857
protected boolean supportAsync = false;
@@ -86,7 +85,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
8685
protected boolean supportJava6 = false;
8786
protected boolean disableHtmlEscaping = false;
8887
protected String booleanGetterPrefix = "get";
89-
protected boolean useNullForUnknownEnumValue = false;
9088
protected String parentGroupId = "";
9189
protected String parentArtifactId = "";
9290
protected String parentVersion = "";
@@ -220,10 +218,6 @@ public void processOpts() {
220218
this.setBooleanGetterPrefix(additionalProperties.get(BOOLEAN_GETTER_PREFIX).toString());
221219
}
222220
additionalProperties.put(BOOLEAN_GETTER_PREFIX, booleanGetterPrefix);
223-
if (additionalProperties.containsKey(USE_NULL_FOR_UNKNOWN_ENUM_VALUE)) {
224-
this.setUseNullForUnknownEnumValue(Boolean.valueOf(additionalProperties.get(USE_NULL_FOR_UNKNOWN_ENUM_VALUE).toString()));
225-
}
226-
additionalProperties.put(USE_NULL_FOR_UNKNOWN_ENUM_VALUE, useNullForUnknownEnumValue);
227221

228222
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
229223
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
@@ -1405,10 +1399,6 @@ public void setBooleanGetterPrefix(String booleanGetterPrefix) {
14051399
this.booleanGetterPrefix = booleanGetterPrefix;
14061400
}
14071401

1408-
public void setUseNullForUnknownEnumValue(boolean useNullForUnknownEnumValue) {
1409-
this.useNullForUnknownEnumValue = useNullForUnknownEnumValue;
1410-
}
1411-
14121402
@Override
14131403
public String escapeQuotationMark(String input) {
14141404
// remove " to avoid code injection

modules/openapi-generator/src/main/resources/Java/modelEnum.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
5353
return b;
5454
}
5555
}
56-
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}}
56+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
5757
}
5858
{{#gson}}
5959

modules/openapi-generator/src/main/resources/Java/modelInnerEnum.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
return b;
4545
}
4646
}
47-
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}}
47+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
4848
}
4949
{{#gson}}
5050

modules/openapi-generator/src/main/resources/JavaInflector/enumClass.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@
3939
return b;
4040
}
4141
}
42-
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}}
42+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/isNullable}}
4343
}
4444
}

modules/openapi-generator/src/main/resources/JavaInflector/enumOuterClass.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
3737
return b;
3838
}
3939
}
40-
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}}
40+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/isNullable}}
4141
}
4242
}

modules/openapi-generator/src/main/resources/JavaJaxRS/cxf-cdi/enumClass.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
2828
return b;
2929
}
3030
}
31-
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}}
31+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
3232
}
3333
}

modules/openapi-generator/src/main/resources/JavaJaxRS/cxf-ext/enumClass.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ public enum {{datatypeWithEnum}} {
2828
return b;
2929
}
3030
}
31-
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}}
31+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
3232
}
3333
}

modules/openapi-generator/src/main/resources/JavaJaxRS/cxf-ext/enumOuterClass.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
4242
return b;
4343
}
4444
}
45-
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}}
45+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
4646
}
4747

4848
}

modules/openapi-generator/src/main/resources/JavaJaxRS/cxf/enumClass.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ public enum {{datatypeWithEnum}} {
2828
return b;
2929
}
3030
}
31-
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}}
31+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
3232
}
3333
}

modules/openapi-generator/src/main/resources/JavaJaxRS/cxf/enumOuterClass.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
4242
return b;
4343
}
4444
}
45-
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}}
45+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
4646
}
4747

4848
}

modules/openapi-generator/src/main/resources/JavaJaxRS/enumClass.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@
4949
return b;
5050
}
5151
}
52-
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}}
52+
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
5353
}
5454
}

0 commit comments

Comments
 (0)