Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add nullable support to enum
  • Loading branch information
wing328 committed Jul 25, 2019
commit dd5a94a0828f75f7bfb717ef3d4bfc1a0f93d0dc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class CodegenModel {
public Set<String> allMandatory = new TreeSet<String>(); // with parent's required properties

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 ExternalDocumentation externalDocumentation;

Expand Down Expand Up @@ -118,6 +118,7 @@ public String toString() {
.append("hasMoreModels", hasMoreModels)
.append("hasEnums", hasEnums)
.append("isEnum", isEnum)
.append("isNullable", isEnum)
.append("hasRequired", hasRequired)
.append("hasOptional", hasOptional)
.append("isArrayModel", isArrayModel)
Expand All @@ -138,86 +139,86 @@ public boolean equals(Object o) {
CodegenModel that = (CodegenModel) o;

return Objects.equals(parent, that.parent) &&
Objects.equals(parentSchema, that.parentSchema) &&
Objects.equals(interfaces, that.interfaces) &&
Objects.equals(allParents, that.allParents) &&
Objects.equals(parentModel, that.parentModel) &&
Objects.equals(interfaceModels, that.interfaceModels) &&
Objects.equals(name, that.name) &&
Objects.equals(classname, that.classname) &&
Objects.equals(title, that.title) &&
Objects.equals(description, that.description) &&
Objects.equals(classVarName, that.classVarName) &&
Objects.equals(modelJson, that.modelJson) &&
Objects.equals(dataType, that.dataType) &&
Objects.equals(xmlPrefix, that.xmlPrefix) &&
Objects.equals(xmlNamespace, that.xmlNamespace) &&
Objects.equals(xmlName, that.xmlName) &&
Objects.equals(classFilename, that.classFilename) &&
Objects.equals(unescapedDescription, that.unescapedDescription) &&
Objects.equals(discriminator, that.discriminator) &&
Objects.equals(defaultValue, that.defaultValue) &&
Objects.equals(vars, that.vars) &&
Objects.equals(requiredVars, that.requiredVars) &&
Objects.equals(optionalVars, that.optionalVars) &&
Objects.equals(allVars, that.allVars) &&
Objects.equals(allowableValues, that.allowableValues) &&
Objects.equals(mandatory, that.mandatory) &&
Objects.equals(allMandatory, that.allMandatory) &&
Objects.equals(imports, that.imports) &&
Objects.equals(hasVars, that.hasVars) &&
Objects.equals(emptyVars, that.emptyVars) &&
Objects.equals(hasMoreModels, that.hasMoreModels) &&
Objects.equals(hasEnums, that.hasEnums) &&
Objects.equals(isEnum, that.isEnum) &&
Objects.equals(externalDocumentation, that.externalDocumentation) &&
Objects.equals(hasOnlyReadOnly, that.hasOnlyReadOnly) &&
Objects.equals(hasChildren, that.hasChildren) &&
Objects.equals(parentVars, that.parentVars) &&
Objects.equals(vendorExtensions, that.vendorExtensions);
Objects.equals(parentSchema, that.parentSchema) &&
Objects.equals(interfaces, that.interfaces) &&
Objects.equals(allParents, that.allParents) &&
Objects.equals(parentModel, that.parentModel) &&
Objects.equals(interfaceModels, that.interfaceModels) &&
Objects.equals(name, that.name) &&
Objects.equals(classname, that.classname) &&
Objects.equals(title, that.title) &&
Objects.equals(description, that.description) &&
Objects.equals(classVarName, that.classVarName) &&
Objects.equals(modelJson, that.modelJson) &&
Objects.equals(dataType, that.dataType) &&
Objects.equals(xmlPrefix, that.xmlPrefix) &&
Objects.equals(xmlNamespace, that.xmlNamespace) &&
Objects.equals(xmlName, that.xmlName) &&
Objects.equals(classFilename, that.classFilename) &&
Objects.equals(unescapedDescription, that.unescapedDescription) &&
Objects.equals(discriminator, that.discriminator) &&
Objects.equals(defaultValue, that.defaultValue) &&
Objects.equals(vars, that.vars) &&
Objects.equals(requiredVars, that.requiredVars) &&
Objects.equals(optionalVars, that.optionalVars) &&
Objects.equals(allVars, that.allVars) &&
Objects.equals(allowableValues, that.allowableValues) &&
Objects.equals(mandatory, that.mandatory) &&
Objects.equals(allMandatory, that.allMandatory) &&
Objects.equals(imports, that.imports) &&
Objects.equals(hasVars, that.hasVars) &&
Objects.equals(emptyVars, that.emptyVars) &&
Objects.equals(hasMoreModels, that.hasMoreModels) &&
Objects.equals(hasEnums, that.hasEnums) &&
Objects.equals(isEnum, that.isEnum) &&
Objects.equals(externalDocumentation, that.externalDocumentation) &&
Objects.equals(hasOnlyReadOnly, that.hasOnlyReadOnly) &&
Objects.equals(hasChildren, that.hasChildren) &&
Objects.equals(parentVars, that.parentVars) &&
Objects.equals(vendorExtensions, that.vendorExtensions);
}

@Override
public int hashCode() {
return Objects.hash(
parent,
parentSchema,
interfaces,
allParents,
parentModel,
interfaceModels,
name,
classname,
title,
description,
classVarName,
modelJson,
dataType,
xmlPrefix,
xmlNamespace,
xmlName,
classFilename,
unescapedDescription,
discriminator,
defaultValue,
vars,
requiredVars,
optionalVars,
allVars,
allowableValues,
mandatory,
allMandatory,
imports,
hasVars,
emptyVars,
hasMoreModels,
hasEnums,
isEnum,
externalDocumentation,
vendorExtensions,
hasOnlyReadOnly,
hasChildren,
parentVars);
parent,
parentSchema,
interfaces,
allParents,
parentModel,
interfaceModels,
name,
classname,
title,
description,
classVarName,
modelJson,
dataType,
xmlPrefix,
xmlNamespace,
xmlName,
classFilename,
unescapedDescription,
discriminator,
defaultValue,
vars,
requiredVars,
optionalVars,
allVars,
allowableValues,
mandatory,
allMandatory,
imports,
hasVars,
emptyVars,
hasMoreModels,
hasEnums,
isEnum,
externalDocumentation,
vendorExtensions,
hasOnlyReadOnly,
hasChildren,
parentVars);
}

public String getParent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ private String getPrimitiveType(Schema schema) {
} else if (ModelUtils.isURISchema(schema)) {
return "URI";
} 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)
// then treat the format as a primitive type.
// This allows the typeMapping flag to add a new custom type which can then
Expand Down Expand Up @@ -1884,19 +1884,16 @@ public CodegenModel fromModel(String name, Schema schema) {
if (ModelUtils.isMapSchema(schema)) {
addAdditionPropertiesToCodeGenModel(m, schema);
m.isMapModel = true;
}
else if (ModelUtils.isIntegerSchema(schema)) { // integer type
} else if (ModelUtils.isIntegerSchema(schema)) { // integer type
m.isNumeric = Boolean.TRUE;
if (ModelUtils.isLongSchema(schema)) { // int64/long format
m.isLong = Boolean.TRUE;
} else { // int32 format
m.isInteger = Boolean.TRUE;
}
}
else if (ModelUtils.isStringSchema(schema)) {
} else if (ModelUtils.isStringSchema(schema)) {
m.isString = Boolean.TRUE;
}
else if (ModelUtils.isNumberSchema(schema)) {
} else if (ModelUtils.isNumberSchema(schema)) {
m.isNumeric = Boolean.TRUE;
if (ModelUtils.isFloatSchema(schema)) { // float
m.isFloat = Boolean.TRUE;
Expand All @@ -1907,6 +1904,10 @@ else if (ModelUtils.isNumberSchema(schema)) {
}
}

if (Boolean.TRUE.equals(schema.getNullable())) {
m.isNullable = Boolean.TRUE;
}

// passing null to allProperties and allRequired as there's no parent
addVars(m, unaliasPropertySchema(schema.getProperties()), schema.getRequired(), null, null);
}
Expand Down Expand Up @@ -2650,7 +2651,7 @@ public CodegenOperation fromOperation(String path,
if (requestBody != null) {
if (getContentType(requestBody) != null &&
(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
formParams = fromRequestBodyToFormParameters(requestBody, imports);
for (CodegenParameter cp : formParams) {
Expand Down Expand Up @@ -3018,7 +3019,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
}

Schema s;
if(parameter.getSchema() != null) {
if (parameter.getSchema() != null) {
s = parameter.getSchema();
} else if (parameter.getContent() != null) {
Content content = parameter.getContent();
Expand Down Expand Up @@ -4942,7 +4943,7 @@ protected void updateOption(String key, String defaultValue) {
}

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())) {
cliOptions.remove(i);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
public static final String SUPPORT_JAVA6 = "supportJava6";
public static final String DISABLE_HTML_ESCAPING = "disableHtmlEscaping";
public static final String BOOLEAN_GETTER_PREFIX = "booleanGetterPrefix";
public static final String USE_NULL_FOR_UNKNOWN_ENUM_VALUE = "useNullForUnknownEnumValue";

protected String dateLibrary = "threetenbp";
protected boolean supportAsync = false;
Expand Down Expand Up @@ -86,7 +85,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
protected boolean supportJava6 = false;
protected boolean disableHtmlEscaping = false;
protected String booleanGetterPrefix = "get";
protected boolean useNullForUnknownEnumValue = false;
protected String parentGroupId = "";
protected String parentArtifactId = "";
protected String parentVersion = "";
Expand Down Expand Up @@ -220,10 +218,6 @@ public void processOpts() {
this.setBooleanGetterPrefix(additionalProperties.get(BOOLEAN_GETTER_PREFIX).toString());
}
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)) {
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
Expand Down Expand Up @@ -1400,10 +1394,6 @@ public void setBooleanGetterPrefix(String booleanGetterPrefix) {
this.booleanGetterPrefix = booleanGetterPrefix;
}

public void setUseNullForUnknownEnumValue(boolean useNullForUnknownEnumValue) {
this.useNullForUnknownEnumValue = useNullForUnknownEnumValue;
}

@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
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}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
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}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
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}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
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}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
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}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public enum {{datatypeWithEnum}} {
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}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
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}}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public enum {{datatypeWithEnum}} {
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}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
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}}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
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}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
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}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
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}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public enum {{datatypeWithEnum}} {
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}}
}
}
Loading