Merge pull request #2226 from wing328/model_more_boolean_flag

Add more boolean flags to CodegenProperty
This commit is contained in:
wing328 2016-02-24 22:06:19 +08:00
commit a65e6dbd0a
8 changed files with 168 additions and 7 deletions

View File

@ -66,6 +66,9 @@ public abstract class AbstractGenerator {
/** /**
* Get the template file path with template dir prepended, and use the * Get the template file path with template dir prepended, and use the
* library template if exists. * library template if exists.
*
* @param config Codegen config
* @param templateFile Template file
*/ */
public String getFullTemplateFile(CodegenConfig config, String templateFile) { public String getFullTemplateFile(CodegenConfig config, String templateFile) {
String library = config.getLibrary(); String library = config.getLibrary();

View File

@ -147,6 +147,8 @@ public interface CodegenConfig {
/** /**
* Library template (sub-template). * Library template (sub-template).
*
* @return libray template
*/ */
String getLibrary(); String getLibrary();
} }

View File

@ -7,10 +7,13 @@ import java.util.List;
public class CodegenParameter { public class CodegenParameter {
public Boolean isFormParam, isQueryParam, isPathParam, isHeaderParam, public Boolean isFormParam, isQueryParam, isPathParam, isHeaderParam,
isCookieParam, isBodyParam, isFile, notFile, hasMore, isContainer, isCookieParam, isBodyParam, hasMore, isContainer,
secondaryParam, isBinary, isCollectionFormatMulti; secondaryParam, isCollectionFormatMulti;
public String baseName, paramName, dataType, collectionFormat, description, baseType, defaultValue; public String baseName, paramName, dataType, collectionFormat, description, baseType, defaultValue;
public String jsonSchema; public String jsonSchema;
public Boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime;
public Boolean isListContainer, isMapContainer;
public Boolean isFile, notFile;
public boolean isEnum; public boolean isEnum;
public List<String> _enum; public List<String> _enum;
public Map<String, Object> allowableValues; public Map<String, Object> allowableValues;
@ -111,6 +114,17 @@ public class CodegenParameter {
} }
output.vendorExtensions = this.vendorExtensions; output.vendorExtensions = this.vendorExtensions;
output.isBinary = this.isBinary; output.isBinary = this.isBinary;
output.isByteArray = this.isByteArray;
output.isString = this.isString;
output.isInteger = this.isInteger;
output.isLong = this.isLong;
output.isDouble = this.isDouble;
output.isFloat = this.isFloat;
output.isBoolean = this.isBoolean;
output.isDate = this.isDate;
output.isDateTime = this.isDateTime;
output.isListContainer = this.isListContainer;
output.isMapContainer = this.isMapContainer;
return output; return output;
} }

View File

@ -33,6 +33,8 @@ public class CodegenProperty {
public Boolean exclusiveMaximum; public Boolean exclusiveMaximum;
public Boolean hasMore, required, secondaryParam; public Boolean hasMore, required, secondaryParam;
public Boolean isPrimitiveType, isContainer, isNotContainer; public Boolean isPrimitiveType, isContainer, isNotContainer;
public Boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime;
public Boolean isListContainer, isMapContainer;
public boolean isEnum; public boolean isEnum;
public Boolean isReadOnly = false; public Boolean isReadOnly = false;
public List<String> _enum; public List<String> _enum;
@ -81,6 +83,18 @@ public class CodegenProperty {
result = prime * result + ((setter == null) ? 0 : setter.hashCode()); result = prime * result + ((setter == null) ? 0 : setter.hashCode());
result = prime * result + ((unescapedDescription == null) ? 0 : unescapedDescription.hashCode()); result = prime * result + ((unescapedDescription == null) ? 0 : unescapedDescription.hashCode());
result = prime * result + ((vendorExtensions == null) ? 0 : vendorExtensions.hashCode()); result = prime * result + ((vendorExtensions == null) ? 0 : vendorExtensions.hashCode());
result = prime * result + ((isString == null) ? 0 : isString.hashCode());
result = prime * result + ((isInteger == null) ? 0 : isInteger.hashCode());
result = prime * result + ((isLong == null) ? 0 : isLong.hashCode());
result = prime * result + ((isFloat == null) ? 0 : isFloat.hashCode());
result = prime * result + ((isDouble == null) ? 0 : isDouble.hashCode());
result = prime * result + ((isByteArray == null) ? 0 : isByteArray.hashCode());
result = prime * result + ((isBinary == null) ? 0 : isBinary.hashCode());
result = prime * result + ((isBoolean == null) ? 0 : isBoolean.hashCode());
result = prime * result + ((isDate == null) ? 0 : isDate.hashCode());
result = prime * result + ((isDateTime == null) ? 0 : isDateTime.hashCode());
result = prime * result + ((isMapContainer == null) ? 0 : isMapContainer.hashCode());
result = prime * result + ((isListContainer == null) ? 0 : isListContainer.hashCode());
return result; return result;
} }
@ -189,6 +203,42 @@ public class CodegenProperty {
if (this.vendorExtensions != other.vendorExtensions && (this.vendorExtensions == null || !this.vendorExtensions.equals(other.vendorExtensions))) { if (this.vendorExtensions != other.vendorExtensions && (this.vendorExtensions == null || !this.vendorExtensions.equals(other.vendorExtensions))) {
return false; return false;
} }
if (this.isString != other.isString && (this.isString == null || !this.isString.equals(other.isString))) {
return false;
}
if (this.isInteger != other.isInteger && (this.isInteger == null || !this.isInteger.equals(other.isInteger))) {
return false;
}
if (this.isLong != other.isLong && (this.isLong == null || !this.isLong.equals(other.isLong))) {
return false;
}
if (this.isFloat != other.isFloat && (this.isFloat == null || !this.isFloat.equals(other.isFloat))) {
return false;
}
if (this.isDouble != other.isDouble && (this.isDouble == null || !this.isDouble.equals(other.isDouble))) {
return false;
}
if (this.isByteArray != other.isByteArray && (this.isByteArray == null || !this.isByteArray.equals(other.isByteArray))) {
return false;
}
if (this.isBoolean != other.isBoolean && (this.isBoolean == null || !this.isBoolean.equals(other.isBoolean))) {
return false;
}
if (this.isDate != other.isDate && (this.isDate == null || !this.isDate.equals(other.isDate))) {
return false;
}
if (this.isDateTime != other.isDateTime && (this.isDateTime == null || !this.isDateTime.equals(other.isDateTime))) {
return false;
}
if (this.isBinary != other.isBinary && (this.isBinary == null || !this.isBinary.equals(other.isBinary))) {
return false;
}
if (this.isListContainer != other.isListContainer && (this.isListContainer == null || !this.isListContainer.equals(other.isListContainer))) {
return false;
}
if (this.isMapContainer != other.isMapContainer && (this.isMapContainer == null || !this.isMapContainer.equals(other.isMapContainer))) {
return false;
}
return true; return true;
} }
} }

View File

@ -1020,6 +1020,7 @@ public class DefaultCodegen {
property.maxLength = sp.getMaxLength(); property.maxLength = sp.getMaxLength();
property.minLength = sp.getMinLength(); property.minLength = sp.getMinLength();
property.pattern = sp.getPattern(); property.pattern = sp.getPattern();
property.isString = true;
if (sp.getEnum() != null) { if (sp.getEnum() != null) {
List<String> _enum = sp.getEnum(); List<String> _enum = sp.getEnum();
property._enum = _enum; property._enum = _enum;
@ -1034,6 +1035,7 @@ public class DefaultCodegen {
if (p instanceof IntegerProperty) { if (p instanceof IntegerProperty) {
IntegerProperty sp = (IntegerProperty) p; IntegerProperty sp = (IntegerProperty) p;
property.isInteger = true;
if (sp.getEnum() != null) { if (sp.getEnum() != null) {
List<Integer> _enum = sp.getEnum(); List<Integer> _enum = sp.getEnum();
property._enum = new ArrayList<String>(); property._enum = new ArrayList<String>();
@ -1051,6 +1053,7 @@ public class DefaultCodegen {
if (p instanceof LongProperty) { if (p instanceof LongProperty) {
LongProperty sp = (LongProperty) p; LongProperty sp = (LongProperty) p;
property.isLong = true;
if (sp.getEnum() != null) { if (sp.getEnum() != null) {
List<Long> _enum = sp.getEnum(); List<Long> _enum = sp.getEnum();
property._enum = new ArrayList<String>(); property._enum = new ArrayList<String>();
@ -1066,8 +1069,21 @@ public class DefaultCodegen {
} }
} }
if (p instanceof BooleanProperty) {
property.isBoolean = true;
}
if (p instanceof BinaryProperty) {
property.isBinary = true;
}
if (p instanceof ByteArrayProperty) {
property.isByteArray = true;
}
if (p instanceof DoubleProperty) { if (p instanceof DoubleProperty) {
DoubleProperty sp = (DoubleProperty) p; DoubleProperty sp = (DoubleProperty) p;
property.isDouble = true;
if (sp.getEnum() != null) { if (sp.getEnum() != null) {
List<Double> _enum = sp.getEnum(); List<Double> _enum = sp.getEnum();
property._enum = new ArrayList<String>(); property._enum = new ArrayList<String>();
@ -1085,6 +1101,7 @@ public class DefaultCodegen {
if (p instanceof FloatProperty) { if (p instanceof FloatProperty) {
FloatProperty sp = (FloatProperty) p; FloatProperty sp = (FloatProperty) p;
property.isFloat = true;
if (sp.getEnum() != null) { if (sp.getEnum() != null) {
List<Float> _enum = sp.getEnum(); List<Float> _enum = sp.getEnum();
property._enum = new ArrayList<String>(); property._enum = new ArrayList<String>();
@ -1102,6 +1119,7 @@ public class DefaultCodegen {
if (p instanceof DateProperty) { if (p instanceof DateProperty) {
DateProperty sp = (DateProperty) p; DateProperty sp = (DateProperty) p;
property.isDate = true;
if (sp.getEnum() != null) { if (sp.getEnum() != null) {
List<String> _enum = sp.getEnum(); List<String> _enum = sp.getEnum();
property._enum = new ArrayList<String>(); property._enum = new ArrayList<String>();
@ -1119,6 +1137,7 @@ public class DefaultCodegen {
if (p instanceof DateTimeProperty) { if (p instanceof DateTimeProperty) {
DateTimeProperty sp = (DateTimeProperty) p; DateTimeProperty sp = (DateTimeProperty) p;
property.isDateTime = true;
if (sp.getEnum() != null) { if (sp.getEnum() != null) {
List<String> _enum = sp.getEnum(); List<String> _enum = sp.getEnum();
property._enum = new ArrayList<String>(); property._enum = new ArrayList<String>();
@ -1146,6 +1165,7 @@ public class DefaultCodegen {
if (p instanceof ArrayProperty) { if (p instanceof ArrayProperty) {
property.isContainer = true; property.isContainer = true;
property.isListContainer = true;
property.containerType = "array"; property.containerType = "array";
ArrayProperty ap = (ArrayProperty) p; ArrayProperty ap = (ArrayProperty) p;
CodegenProperty cp = fromProperty(property.name, ap.getItems()); CodegenProperty cp = fromProperty(property.name, ap.getItems());
@ -1168,6 +1188,7 @@ public class DefaultCodegen {
} }
} else if (p instanceof MapProperty) { } else if (p instanceof MapProperty) {
property.isContainer = true; property.isContainer = true;
property.isMapContainer = true;
property.containerType = "map"; property.containerType = "map";
MapProperty ap = (MapProperty) p; MapProperty ap = (MapProperty) p;
CodegenProperty cp = fromProperty("inner", ap.getAdditionalProperties()); CodegenProperty cp = fromProperty("inner", ap.getAdditionalProperties());
@ -1575,7 +1596,7 @@ public class DefaultCodegen {
if (null == type) { if (null == type) {
LOGGER.warn("Type is NULL for Serializable Parameter: " + param); LOGGER.warn("Type is NULL for Serializable Parameter: " + param);
} }
if ("array".equals(type)) { if ("array".equals(type)) { // for array parameter
Property inner = qp.getItems(); Property inner = qp.getItems();
if (inner == null) { if (inner == null) {
LOGGER.warn("warning! No inner type supplied for array parameter \"" + qp.getName() + "\", using String"); LOGGER.warn("warning! No inner type supplied for array parameter \"" + qp.getName() + "\", using String");
@ -1589,8 +1610,9 @@ public class DefaultCodegen {
CodegenProperty pr = fromProperty("inner", inner); CodegenProperty pr = fromProperty("inner", inner);
p.baseType = pr.datatype; p.baseType = pr.datatype;
p.isContainer = true; p.isContainer = true;
p.isListContainer = true;
imports.add(pr.baseType); imports.add(pr.baseType);
} else if ("object".equals(type)) { } else if ("object".equals(type)) { // for map parameter
Property inner = qp.getItems(); Property inner = qp.getItems();
if (inner == null) { if (inner == null) {
LOGGER.warn("warning! No inner type supplied for map parameter \"" + qp.getName() + "\", using String"); LOGGER.warn("warning! No inner type supplied for map parameter \"" + qp.getName() + "\", using String");
@ -1600,6 +1622,8 @@ public class DefaultCodegen {
collectionFormat = qp.getCollectionFormat(); collectionFormat = qp.getCollectionFormat();
CodegenProperty pr = fromProperty("inner", inner); CodegenProperty pr = fromProperty("inner", inner);
p.baseType = pr.datatype; p.baseType = pr.datatype;
p.isContainer = true;
p.isMapContainer = true;
imports.add(pr.baseType); imports.add(pr.baseType);
} else { } else {
Map<PropertyId, Object> args = new HashMap<PropertyId, Object>(); Map<PropertyId, Object> args = new HashMap<PropertyId, Object>();
@ -1607,12 +1631,17 @@ public class DefaultCodegen {
args.put(PropertyId.ENUM, qp.getEnum()); args.put(PropertyId.ENUM, qp.getEnum());
property = PropertyBuilder.build(type, format, args); property = PropertyBuilder.build(type, format, args);
} }
if (property == null) { if (property == null) {
LOGGER.warn("warning! Property type \"" + type + "\" not found for parameter \"" + param.getName() + "\", using String"); LOGGER.warn("warning! Property type \"" + type + "\" not found for parameter \"" + param.getName() + "\", using String");
property = new StringProperty().description("//TODO automatically added by swagger-codegen. Type was " + type + " but not supported"); property = new StringProperty().description("//TODO automatically added by swagger-codegen. Type was " + type + " but not supported");
} }
property.setRequired(param.getRequired()); property.setRequired(param.getRequired());
CodegenProperty model = fromProperty(qp.getName(), property); CodegenProperty model = fromProperty(qp.getName(), property);
// set boolean flag (e.g. isString)
setParameterBooleanFlagWithCodegenProperty(p, model);
p.dataType = model.datatype; p.dataType = model.datatype;
p.isEnum = model.isEnum; p.isEnum = model.isEnum;
p._enum = model._enum; p._enum = model._enum;
@ -1663,6 +1692,9 @@ public class DefaultCodegen {
p.dataType = cp.datatype; p.dataType = cp.datatype;
p.isBinary = cp.datatype.toLowerCase().startsWith("byte"); p.isBinary = cp.datatype.toLowerCase().startsWith("byte");
} }
// set boolean flag (e.g. isString)
setParameterBooleanFlagWithCodegenProperty(p, cp);
} }
} else if (model instanceof ArrayModel) { } else if (model instanceof ArrayModel) {
// to use the built-in model parsing, we unwrap the ArrayModel // to use the built-in model parsing, we unwrap the ArrayModel
@ -1678,6 +1710,10 @@ public class DefaultCodegen {
imports.add(cp.baseType); imports.add(cp.baseType);
p.dataType = cp.datatype; p.dataType = cp.datatype;
p.isContainer = true; p.isContainer = true;
p.isListContainer = true;
// set boolean flag (e.g. isString)
setParameterBooleanFlagWithCodegenProperty(p, cp);
} else { } else {
Model sub = bp.getSchema(); Model sub = bp.getSchema();
if (sub instanceof RefModel) { if (sub instanceof RefModel) {
@ -2157,6 +2193,8 @@ public class DefaultCodegen {
/** /**
* Library template (sub-template). * Library template (sub-template).
*
* @return Library template
*/ */
public String getLibrary() { public String getLibrary() {
return library; return library;
@ -2221,10 +2259,10 @@ public class DefaultCodegen {
} }
/** /**
* only write if the file doesn't exist * Only write if the file doesn't exist
* *
* @param outputFolder * @param outputFolder Output folder
* @param supportingFile * @param supportingFile Supporting file
*/ */
public void writeOptional(String outputFolder, SupportingFile supportingFile) { public void writeOptional(String outputFolder, SupportingFile supportingFile) {
String folder = ""; String folder = "";
@ -2243,4 +2281,46 @@ public class DefaultCodegen {
supportingFiles.add(supportingFile); supportingFiles.add(supportingFile);
} }
} }
/**
* Set CodegenParameter boolean flag using CodegenProperty.
*
* @param parameter Codegen Parameter
* @param property Codegen property
*/
public void setParameterBooleanFlagWithCodegenProperty(CodegenParameter parameter, CodegenProperty property) {
if (parameter == null) {
LOGGER.error("Codegen Parameter cannnot be null.");
return;
}
if (property == null) {
LOGGER.error("Codegen Property cannot be null.");
return;
}
if (Boolean.TRUE.equals(property.isString)) {
parameter.isString = true;
} else if (Boolean.TRUE.equals(property.isBoolean)) {
parameter.isBoolean = true;
} else if (Boolean.TRUE.equals(property.isLong)) {
parameter.isLong = true;
} else if (Boolean.TRUE.equals(property.isInteger)) {
parameter.isInteger = true;
} else if (Boolean.TRUE.equals(property.isDouble)) {
parameter.isDouble = true;
} else if (Boolean.TRUE.equals(property.isFloat)) {
parameter.isFloat = true;
} else if (Boolean.TRUE.equals(property.isByteArray)) {
parameter.isByteArray = true;
} else if (Boolean.TRUE.equals(property.isBinary)) {
parameter.isByteArray = true;
} else if (Boolean.TRUE.equals(property.isDate)) {
parameter.isDate = true;
} else if (Boolean.TRUE.equals(property.isDateTime)) {
parameter.isDateTime = true;
} else {
LOGGER.debug("Property type is not primitive: " + property.datatype);
}
}
} }

View File

@ -385,6 +385,9 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
/** /**
* Normalize type by wrapping primitive types with single quotes. * Normalize type by wrapping primitive types with single quotes.
*
* @param type Primitive type
* @return Normalized type
*/ */
public String normalizeType(String type) { public String normalizeType(String type) {
return type.replaceAll("\\b(Boolean|Integer|Number|String|Date)\\b", "'$1'"); return type.replaceAll("\\b(Boolean|Integer|Number|String|Date)\\b", "'$1'");

View File

@ -293,6 +293,9 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
* *
* (PEP 0008) Python packages should also have short, all-lowercase names, * (PEP 0008) Python packages should also have short, all-lowercase names,
* although the use of underscores is discouraged. * although the use of underscores is discouraged.
*
* @param packageName Package name
* @return Python package name that conforms to PEP 0008
*/ */
@SuppressWarnings("static-method") @SuppressWarnings("static-method")
public String generatePackageName(String packageName) { public String generatePackageName(String packageName) {

View File

@ -211,6 +211,9 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
/** /**
* Generate Ruby module name from the gem name, e.g. use "SwaggerClient" for "swagger_client". * Generate Ruby module name from the gem name, e.g. use "SwaggerClient" for "swagger_client".
*
* @param gemName Ruby gem name
* @return Ruby module naame
*/ */
@SuppressWarnings("static-method") @SuppressWarnings("static-method")
public String generateModuleName(String gemName) { public String generateModuleName(String gemName) {
@ -219,6 +222,9 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
/** /**
* Generate Ruby gem name from the module name, e.g. use "swagger_client" for "SwaggerClient". * Generate Ruby gem name from the module name, e.g. use "swagger_client" for "SwaggerClient".
*
* @param moduleName Ruby module naame
* @return Ruby gem name
*/ */
@SuppressWarnings("static-method") @SuppressWarnings("static-method")
public String generateGemName(String moduleName) { public String generateGemName(String moduleName) {