[Java] Replace "useNullForUnknownEnumValue" option with the nullable attribute (#3455)

* add nullable support to enum

* update test spec with nullable enum

* update samples

* update samples
This commit is contained in:
William Cheng 2019-07-26 09:49:44 +08:00 committed by GitHub
parent fabe021fb0
commit 8253c28d6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
41 changed files with 128 additions and 152 deletions

View File

@ -27,7 +27,7 @@ fi
# if you've executed sbt assembly previously it will use that instead. # if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
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 $@" 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 $@"
echo "Removing files and folders under samples/client/petstore/java/jersey1/src/main" echo "Removing files and folders under samples/client/petstore/java/jersey1/src/main"
rm -rf samples/client/petstore/java/jersey1/src/main rm -rf samples/client/petstore/java/jersey1/src/main

View File

@ -1,7 +1,4 @@
{ {
"library": "jersey2", "library": "jersey2",
"artifactId": "petstore-jersey2", "artifactId": "petstore-jersey2"
"additionalProperties" : {
"useNullForUnknownEnumValue" : true
}
} }

View File

@ -61,7 +61,7 @@ public class CodegenModel {
public Set<String> allMandatory = new TreeSet<String>(); // with parent's required properties public Set<String> allMandatory = new TreeSet<String>(); // with parent's required properties
public Set<String> imports = new TreeSet<String>(); public Set<String> imports = new TreeSet<String>();
public boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, hasRequired, hasOptional, isArrayModel, hasChildren, isMapModel; public boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArrayModel, hasChildren, isMapModel;
public boolean hasOnlyReadOnly = true; // true if all properties are read-only public boolean hasOnlyReadOnly = true; // true if all properties are read-only
public ExternalDocumentation externalDocumentation; public ExternalDocumentation externalDocumentation;
@ -118,6 +118,7 @@ public class CodegenModel {
.append("hasMoreModels", hasMoreModels) .append("hasMoreModels", hasMoreModels)
.append("hasEnums", hasEnums) .append("hasEnums", hasEnums)
.append("isEnum", isEnum) .append("isEnum", isEnum)
.append("isNullable", isEnum)
.append("hasRequired", hasRequired) .append("hasRequired", hasRequired)
.append("hasOptional", hasOptional) .append("hasOptional", hasOptional)
.append("isArrayModel", isArrayModel) .append("isArrayModel", isArrayModel)
@ -138,86 +139,86 @@ public class CodegenModel {
CodegenModel that = (CodegenModel) o; CodegenModel that = (CodegenModel) o;
return Objects.equals(parent, that.parent) && return Objects.equals(parent, that.parent) &&
Objects.equals(parentSchema, that.parentSchema) && Objects.equals(parentSchema, that.parentSchema) &&
Objects.equals(interfaces, that.interfaces) && Objects.equals(interfaces, that.interfaces) &&
Objects.equals(allParents, that.allParents) && Objects.equals(allParents, that.allParents) &&
Objects.equals(parentModel, that.parentModel) && Objects.equals(parentModel, that.parentModel) &&
Objects.equals(interfaceModels, that.interfaceModels) && Objects.equals(interfaceModels, that.interfaceModels) &&
Objects.equals(name, that.name) && Objects.equals(name, that.name) &&
Objects.equals(classname, that.classname) && Objects.equals(classname, that.classname) &&
Objects.equals(title, that.title) && Objects.equals(title, that.title) &&
Objects.equals(description, that.description) && Objects.equals(description, that.description) &&
Objects.equals(classVarName, that.classVarName) && Objects.equals(classVarName, that.classVarName) &&
Objects.equals(modelJson, that.modelJson) && Objects.equals(modelJson, that.modelJson) &&
Objects.equals(dataType, that.dataType) && Objects.equals(dataType, that.dataType) &&
Objects.equals(xmlPrefix, that.xmlPrefix) && Objects.equals(xmlPrefix, that.xmlPrefix) &&
Objects.equals(xmlNamespace, that.xmlNamespace) && Objects.equals(xmlNamespace, that.xmlNamespace) &&
Objects.equals(xmlName, that.xmlName) && Objects.equals(xmlName, that.xmlName) &&
Objects.equals(classFilename, that.classFilename) && Objects.equals(classFilename, that.classFilename) &&
Objects.equals(unescapedDescription, that.unescapedDescription) && Objects.equals(unescapedDescription, that.unescapedDescription) &&
Objects.equals(discriminator, that.discriminator) && Objects.equals(discriminator, that.discriminator) &&
Objects.equals(defaultValue, that.defaultValue) && Objects.equals(defaultValue, that.defaultValue) &&
Objects.equals(vars, that.vars) && Objects.equals(vars, that.vars) &&
Objects.equals(requiredVars, that.requiredVars) && Objects.equals(requiredVars, that.requiredVars) &&
Objects.equals(optionalVars, that.optionalVars) && Objects.equals(optionalVars, that.optionalVars) &&
Objects.equals(allVars, that.allVars) && Objects.equals(allVars, that.allVars) &&
Objects.equals(allowableValues, that.allowableValues) && Objects.equals(allowableValues, that.allowableValues) &&
Objects.equals(mandatory, that.mandatory) && Objects.equals(mandatory, that.mandatory) &&
Objects.equals(allMandatory, that.allMandatory) && Objects.equals(allMandatory, that.allMandatory) &&
Objects.equals(imports, that.imports) && Objects.equals(imports, that.imports) &&
Objects.equals(hasVars, that.hasVars) && Objects.equals(hasVars, that.hasVars) &&
Objects.equals(emptyVars, that.emptyVars) && Objects.equals(emptyVars, that.emptyVars) &&
Objects.equals(hasMoreModels, that.hasMoreModels) && Objects.equals(hasMoreModels, that.hasMoreModels) &&
Objects.equals(hasEnums, that.hasEnums) && Objects.equals(hasEnums, that.hasEnums) &&
Objects.equals(isEnum, that.isEnum) && Objects.equals(isEnum, that.isEnum) &&
Objects.equals(externalDocumentation, that.externalDocumentation) && Objects.equals(externalDocumentation, that.externalDocumentation) &&
Objects.equals(hasOnlyReadOnly, that.hasOnlyReadOnly) && Objects.equals(hasOnlyReadOnly, that.hasOnlyReadOnly) &&
Objects.equals(hasChildren, that.hasChildren) && Objects.equals(hasChildren, that.hasChildren) &&
Objects.equals(parentVars, that.parentVars) && Objects.equals(parentVars, that.parentVars) &&
Objects.equals(vendorExtensions, that.vendorExtensions); Objects.equals(vendorExtensions, that.vendorExtensions);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash( return Objects.hash(
parent, parent,
parentSchema, parentSchema,
interfaces, interfaces,
allParents, allParents,
parentModel, parentModel,
interfaceModels, interfaceModels,
name, name,
classname, classname,
title, title,
description, description,
classVarName, classVarName,
modelJson, modelJson,
dataType, dataType,
xmlPrefix, xmlPrefix,
xmlNamespace, xmlNamespace,
xmlName, xmlName,
classFilename, classFilename,
unescapedDescription, unescapedDescription,
discriminator, discriminator,
defaultValue, defaultValue,
vars, vars,
requiredVars, requiredVars,
optionalVars, optionalVars,
allVars, allVars,
allowableValues, allowableValues,
mandatory, mandatory,
allMandatory, allMandatory,
imports, imports,
hasVars, hasVars,
emptyVars, emptyVars,
hasMoreModels, hasMoreModels,
hasEnums, hasEnums,
isEnum, isEnum,
externalDocumentation, externalDocumentation,
vendorExtensions, vendorExtensions,
hasOnlyReadOnly, hasOnlyReadOnly,
hasChildren, hasChildren,
parentVars); parentVars);
} }
public String getParent() { public String getParent() {

View File

@ -1572,7 +1572,7 @@ public class DefaultCodegen implements CodegenConfig {
} else if (ModelUtils.isURISchema(schema)) { } else if (ModelUtils.isURISchema(schema)) {
return "URI"; return "URI";
} else if (ModelUtils.isStringSchema(schema)) { } else if (ModelUtils.isStringSchema(schema)) {
if(typeMapping.containsKey(schema.getFormat())) { if (typeMapping.containsKey(schema.getFormat())) {
// If the format matches a typeMapping (supplied with the --typeMappings flag) // If the format matches a typeMapping (supplied with the --typeMappings flag)
// then treat the format as a primitive type. // then treat the format as a primitive type.
// This allows the typeMapping flag to add a new custom type which can then // This allows the typeMapping flag to add a new custom type which can then
@ -1884,19 +1884,16 @@ public class DefaultCodegen implements CodegenConfig {
if (ModelUtils.isMapSchema(schema)) { if (ModelUtils.isMapSchema(schema)) {
addAdditionPropertiesToCodeGenModel(m, schema); addAdditionPropertiesToCodeGenModel(m, schema);
m.isMapModel = true; m.isMapModel = true;
} } else if (ModelUtils.isIntegerSchema(schema)) { // integer type
else if (ModelUtils.isIntegerSchema(schema)) { // integer type
m.isNumeric = Boolean.TRUE; m.isNumeric = Boolean.TRUE;
if (ModelUtils.isLongSchema(schema)) { // int64/long format if (ModelUtils.isLongSchema(schema)) { // int64/long format
m.isLong = Boolean.TRUE; m.isLong = Boolean.TRUE;
} else { // int32 format } else { // int32 format
m.isInteger = Boolean.TRUE; m.isInteger = Boolean.TRUE;
} }
} } else if (ModelUtils.isStringSchema(schema)) {
else if (ModelUtils.isStringSchema(schema)) {
m.isString = Boolean.TRUE; m.isString = Boolean.TRUE;
} } else if (ModelUtils.isNumberSchema(schema)) {
else if (ModelUtils.isNumberSchema(schema)) {
m.isNumeric = Boolean.TRUE; m.isNumeric = Boolean.TRUE;
if (ModelUtils.isFloatSchema(schema)) { // float if (ModelUtils.isFloatSchema(schema)) { // float
m.isFloat = Boolean.TRUE; m.isFloat = Boolean.TRUE;
@ -1907,6 +1904,10 @@ public class DefaultCodegen implements CodegenConfig {
} }
} }
if (Boolean.TRUE.equals(schema.getNullable())) {
m.isNullable = Boolean.TRUE;
}
// passing null to allProperties and allRequired as there's no parent // passing null to allProperties and allRequired as there's no parent
addVars(m, unaliasPropertySchema(schema.getProperties()), schema.getRequired(), null, null); addVars(m, unaliasPropertySchema(schema.getProperties()), schema.getRequired(), null, null);
} }
@ -2650,7 +2651,7 @@ public class DefaultCodegen implements CodegenConfig {
if (requestBody != null) { if (requestBody != null) {
if (getContentType(requestBody) != null && if (getContentType(requestBody) != null &&
(getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") || (getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") ||
getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("multipart/form-data"))) { getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("multipart/form-data"))) {
// process form parameters // process form parameters
formParams = fromRequestBodyToFormParameters(requestBody, imports); formParams = fromRequestBodyToFormParameters(requestBody, imports);
for (CodegenParameter cp : formParams) { for (CodegenParameter cp : formParams) {
@ -3018,7 +3019,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
Schema s; Schema s;
if(parameter.getSchema() != null) { if (parameter.getSchema() != null) {
s = parameter.getSchema(); s = parameter.getSchema();
} else if (parameter.getContent() != null) { } else if (parameter.getContent() != null) {
Content content = parameter.getContent(); Content content = parameter.getContent();
@ -4942,7 +4943,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
protected void removeOption(String key) { protected void removeOption(String key) {
for(int i = 0; i < cliOptions.size(); i++) { for (int i = 0; i < cliOptions.size(); i++) {
if (key.equals(cliOptions.get(i).getOpt())) { if (key.equals(cliOptions.get(i).getOpt())) {
cliOptions.remove(i); cliOptions.remove(i);
break; break;

View File

@ -52,7 +52,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
public static final String SUPPORT_JAVA6 = "supportJava6"; public static final String SUPPORT_JAVA6 = "supportJava6";
public static final String DISABLE_HTML_ESCAPING = "disableHtmlEscaping"; public static final String DISABLE_HTML_ESCAPING = "disableHtmlEscaping";
public static final String BOOLEAN_GETTER_PREFIX = "booleanGetterPrefix"; public static final String BOOLEAN_GETTER_PREFIX = "booleanGetterPrefix";
public static final String USE_NULL_FOR_UNKNOWN_ENUM_VALUE = "useNullForUnknownEnumValue";
protected String dateLibrary = "threetenbp"; protected String dateLibrary = "threetenbp";
protected boolean supportAsync = false; protected boolean supportAsync = false;
@ -86,7 +85,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
protected boolean supportJava6 = false; protected boolean supportJava6 = false;
protected boolean disableHtmlEscaping = false; protected boolean disableHtmlEscaping = false;
protected String booleanGetterPrefix = "get"; protected String booleanGetterPrefix = "get";
protected boolean useNullForUnknownEnumValue = false;
protected String parentGroupId = ""; protected String parentGroupId = "";
protected String parentArtifactId = ""; protected String parentArtifactId = "";
protected String parentVersion = ""; protected String parentVersion = "";
@ -220,10 +218,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
this.setBooleanGetterPrefix(additionalProperties.get(BOOLEAN_GETTER_PREFIX).toString()); this.setBooleanGetterPrefix(additionalProperties.get(BOOLEAN_GETTER_PREFIX).toString());
} }
additionalProperties.put(BOOLEAN_GETTER_PREFIX, booleanGetterPrefix); additionalProperties.put(BOOLEAN_GETTER_PREFIX, booleanGetterPrefix);
if (additionalProperties.containsKey(USE_NULL_FOR_UNKNOWN_ENUM_VALUE)) {
this.setUseNullForUnknownEnumValue(Boolean.valueOf(additionalProperties.get(USE_NULL_FOR_UNKNOWN_ENUM_VALUE).toString()));
}
additionalProperties.put(USE_NULL_FOR_UNKNOWN_ENUM_VALUE, useNullForUnknownEnumValue);
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) { if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE)); this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
@ -1405,10 +1399,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
this.booleanGetterPrefix = booleanGetterPrefix; this.booleanGetterPrefix = booleanGetterPrefix;
} }
public void setUseNullForUnknownEnumValue(boolean useNullForUnknownEnumValue) {
this.useNullForUnknownEnumValue = useNullForUnknownEnumValue;
}
@Override @Override
public String escapeQuotationMark(String input) { public String escapeQuotationMark(String input) {
// remove " to avoid code injection // remove " to avoid code injection

View File

@ -53,7 +53,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
{{#gson}} {{#gson}}

View File

@ -44,7 +44,7 @@
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
{{#gson}} {{#gson}}

View File

@ -39,6 +39,6 @@
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/isNullable}}
} }
} }

View File

@ -37,6 +37,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/isNullable}}
} }
} }

View File

@ -28,6 +28,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -28,6 +28,6 @@ public enum {{datatypeWithEnum}} {
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -42,7 +42,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -28,6 +28,6 @@ public enum {{datatypeWithEnum}} {
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -42,7 +42,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -49,6 +49,6 @@
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -47,6 +47,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -45,7 +45,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
{{/jackson}} {{/jackson}}
} }

View File

@ -28,6 +28,6 @@ public enum {{datatypeWithEnum}} {
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -38,6 +38,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -39,6 +39,6 @@
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -37,6 +37,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -39,6 +39,6 @@
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -37,6 +37,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -31,6 +31,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
} }
} }

View File

@ -39,6 +39,6 @@
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/isNullable}}
} }
} }

View File

@ -37,6 +37,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/isNullable}}
} }
} }

View File

@ -39,6 +39,6 @@
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/isNullable}}
} }
} }

View File

@ -37,6 +37,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/isNullable}}
} }
} }

View File

@ -37,6 +37,6 @@ if (String.valueOf(b.value).equals(text)) {
return b; return b;
} }
} }
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}} {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/isNullable}}
} }
} }

View File

@ -129,7 +129,6 @@ public class AbstractJavaCodegenTest {
Assert.assertEquals(codegen.getInvokerPackage(), "org.openapitools"); Assert.assertEquals(codegen.getInvokerPackage(), "org.openapitools");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "org.openapitools"); Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "org.openapitools");
Assert.assertEquals(codegen.additionalProperties().get(AbstractJavaCodegen.BOOLEAN_GETTER_PREFIX), "get"); Assert.assertEquals(codegen.additionalProperties().get(AbstractJavaCodegen.BOOLEAN_GETTER_PREFIX), "get");
Assert.assertEquals(codegen.additionalProperties().get(AbstractJavaCodegen.USE_NULL_FOR_UNKNOWN_ENUM_VALUE), Boolean.FALSE);
} }
@Test @Test
@ -140,7 +139,6 @@ public class AbstractJavaCodegenTest {
codegen.setApiPackage("xyz.yyyyy.zzzzzzz.api"); codegen.setApiPackage("xyz.yyyyy.zzzzzzz.api");
codegen.setInvokerPackage("xyz.yyyyy.zzzzzzz.invoker"); codegen.setInvokerPackage("xyz.yyyyy.zzzzzzz.invoker");
codegen.setBooleanGetterPrefix("is"); codegen.setBooleanGetterPrefix("is");
codegen.setUseNullForUnknownEnumValue(true);
codegen.processOpts(); codegen.processOpts();
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE); Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
@ -152,7 +150,6 @@ public class AbstractJavaCodegenTest {
Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.zzzzzzz.invoker"); Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.zzzzzzz.invoker");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xyz.yyyyy.zzzzzzz.invoker"); Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xyz.yyyyy.zzzzzzz.invoker");
Assert.assertEquals(codegen.additionalProperties().get(AbstractJavaCodegen.BOOLEAN_GETTER_PREFIX), "is"); Assert.assertEquals(codegen.additionalProperties().get(AbstractJavaCodegen.BOOLEAN_GETTER_PREFIX), "is");
Assert.assertEquals(codegen.additionalProperties().get(AbstractJavaCodegen.USE_NULL_FOR_UNKNOWN_ENUM_VALUE), Boolean.TRUE);
} }
@Test @Test
@ -163,7 +160,6 @@ public class AbstractJavaCodegenTest {
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.yyyyy.api.oooooo"); codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.yyyyy.api.oooooo");
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE, "xyz.yyyyy.invoker.oooooo"); codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE, "xyz.yyyyy.invoker.oooooo");
codegen.additionalProperties().put(AbstractJavaCodegen.BOOLEAN_GETTER_PREFIX, "getBoolean"); codegen.additionalProperties().put(AbstractJavaCodegen.BOOLEAN_GETTER_PREFIX, "getBoolean");
codegen.additionalProperties().put(AbstractJavaCodegen.USE_NULL_FOR_UNKNOWN_ENUM_VALUE, "true");
codegen.processOpts(); codegen.processOpts();
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE); Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
@ -175,7 +171,6 @@ public class AbstractJavaCodegenTest {
Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.invoker.oooooo"); Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.invoker.oooooo");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xyz.yyyyy.invoker.oooooo"); Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xyz.yyyyy.invoker.oooooo");
Assert.assertEquals(codegen.additionalProperties().get(AbstractJavaCodegen.BOOLEAN_GETTER_PREFIX), "getBoolean"); Assert.assertEquals(codegen.additionalProperties().get(AbstractJavaCodegen.BOOLEAN_GETTER_PREFIX), "getBoolean");
Assert.assertEquals(codegen.additionalProperties().get(AbstractJavaCodegen.USE_NULL_FOR_UNKNOWN_ENUM_VALUE), Boolean.TRUE);
} }
@Test @Test

View File

@ -133,11 +133,6 @@ public class JavaJAXRSCXFExtServerCodegenTest extends JavaJaxrsBaseTest {
return useMultipartFeature; return useMultipartFeature;
} }
// AbstractJavaCodegen.USE_NULL_FOR_UNKNOWN_ENUM_VALUE
public boolean isUseNullForUnknownEnumValue() {
return useNullForUnknownEnumValue;
}
// SpringFeatures.USE_SPRING_ANNOTATION_CONFIG // SpringFeatures.USE_SPRING_ANNOTATION_CONFIG
public boolean isUseSpringAnnotationConfig() { public boolean isUseSpringAnnotationConfig() {
return useSpringAnnotationConfig; return useSpringAnnotationConfig;
@ -253,7 +248,6 @@ public class JavaJAXRSCXFExtServerCodegenTest extends JavaJaxrsBaseTest {
additionalProperties.put(AbstractJavaCodegen.JAVA8_MODE, "true"); additionalProperties.put(AbstractJavaCodegen.JAVA8_MODE, "true");
additionalProperties.put(AbstractJavaCodegen.SUPPORT_ASYNC, "true"); additionalProperties.put(AbstractJavaCodegen.SUPPORT_ASYNC, "true");
additionalProperties.put(AbstractJavaCodegen.SUPPORT_JAVA6, "false"); additionalProperties.put(AbstractJavaCodegen.SUPPORT_JAVA6, "false");
additionalProperties.put(AbstractJavaCodegen.USE_NULL_FOR_UNKNOWN_ENUM_VALUE, "true");
additionalProperties.put(AbstractJavaCodegen.WITH_XML, "true"); additionalProperties.put(AbstractJavaCodegen.WITH_XML, "true");
// Options processed by AbstractJavaJAXRSServerCodegen // Options processed by AbstractJavaJAXRSServerCodegen
additionalProperties.put(CodegenConstants.IMPL_FOLDER, "myimpl"); additionalProperties.put(CodegenConstants.IMPL_FOLDER, "myimpl");
@ -328,7 +322,6 @@ public class JavaJAXRSCXFExtServerCodegenTest extends JavaJaxrsBaseTest {
assertEquals(testerCodegen.isFullJavaUtil(), true); assertEquals(testerCodegen.isFullJavaUtil(), true);
assertEquals(testerCodegen.isJava8Mode(), true); assertEquals(testerCodegen.isJava8Mode(), true);
assertEquals(testerCodegen.isSupportAsync(), true); assertEquals(testerCodegen.isSupportAsync(), true);
assertEquals(testerCodegen.isUseNullForUnknownEnumValue(), true);
assertEquals(testerCodegen.isWithXml(), true); assertEquals(testerCodegen.isWithXml(), true);
// Options processed by AbstractJavaJAXRSServerCodegen // Options processed by AbstractJavaJAXRSServerCodegen
assertEquals(testerCodegen.getImplFolder(), "myimpl"); assertEquals(testerCodegen.getImplFolder(), "myimpl");
@ -581,7 +574,6 @@ public class JavaJAXRSCXFExtServerCodegenTest extends JavaJaxrsBaseTest {
assertNull(additionalProperties.get(AbstractJavaCodegen.JAVA8_MODE)); assertNull(additionalProperties.get(AbstractJavaCodegen.JAVA8_MODE));
assertNull(additionalProperties.get(AbstractJavaCodegen.SUPPORT_ASYNC)); assertNull(additionalProperties.get(AbstractJavaCodegen.SUPPORT_ASYNC));
assertEquals(additionalProperties.get(AbstractJavaCodegen.SUPPORT_JAVA6), Boolean.FALSE); assertEquals(additionalProperties.get(AbstractJavaCodegen.SUPPORT_JAVA6), Boolean.FALSE);
assertEquals(additionalProperties.get(AbstractJavaCodegen.USE_NULL_FOR_UNKNOWN_ENUM_VALUE), false);
assertEquals(additionalProperties.get(AbstractJavaCodegen.WITH_XML), false); assertEquals(additionalProperties.get(AbstractJavaCodegen.WITH_XML), false);
// Options processed by AbstractJavaJAXRSServerCodegen // Options processed by AbstractJavaJAXRSServerCodegen
assertNull(additionalProperties.get(CodegenConstants.IMPL_FOLDER)); assertNull(additionalProperties.get(CodegenConstants.IMPL_FOLDER));

View File

@ -1580,6 +1580,7 @@ components:
- fish - fish
- crab - crab
OuterEnum: OuterEnum:
nullable: true
type: string type: string
enum: enum:
- placed - placed

View File

@ -59,7 +59,7 @@ public class EnumArrays {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }
@ -97,7 +97,7 @@ public class EnumArrays {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }

View File

@ -53,7 +53,7 @@ public enum EnumClass {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }

View File

@ -60,7 +60,7 @@ public class EnumTest {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }
@ -100,7 +100,7 @@ public class EnumTest {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }
@ -138,7 +138,7 @@ public class EnumTest {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }
@ -176,7 +176,7 @@ public class EnumTest {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }

View File

@ -63,7 +63,7 @@ public class MapTest {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }

View File

@ -72,7 +72,7 @@ public class Order {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }

View File

@ -53,7 +53,7 @@ public enum OuterEnum {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }

View File

@ -78,7 +78,7 @@ public class Pet {
return b; return b;
} }
} }
return null; throw new IllegalArgumentException("Unexpected value '" + value + "'");
} }
} }

View File

@ -72,8 +72,7 @@ class EnumTest(object):
self.enum_integer = enum_integer self.enum_integer = enum_integer
if enum_number is not None: if enum_number is not None:
self.enum_number = enum_number self.enum_number = enum_number
if outer_enum is not None: self.outer_enum = outer_enum
self.outer_enum = outer_enum
if outer_enum_integer is not None: if outer_enum_integer is not None:
self.outer_enum_integer = outer_enum_integer self.outer_enum_integer = outer_enum_integer
if outer_enum_default_value is not None: if outer_enum_default_value is not None:

View File

@ -55,7 +55,7 @@ public enum OuterEnum {
return b; return b;
} }
} }
throw new IllegalArgumentException("Unexpected value '" + value + "'"); return null;
} }
} }