Skip to content

Commit

Permalink
Merge pull request #2226 from wing328/model_more_boolean_flag
Browse files Browse the repository at this point in the history
Add more boolean flags to CodegenProperty
  • Loading branch information
wing328 committed Feb 24, 2016
2 parents bd32a6a + 3cfb110 commit a65e6db
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public Reader getTemplateReader(String name) {
/**
* Get the template file path with template dir prepended, and use the
* library template if exists.
*
* @param config Codegen config
* @param templateFile Template file
*/
public String getFullTemplateFile(CodegenConfig config, String templateFile) {
String library = config.getLibrary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public interface CodegenConfig {

/**
* Library template (sub-template).
*
* @return libray template
*/
String getLibrary();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

public class CodegenParameter {
public Boolean isFormParam, isQueryParam, isPathParam, isHeaderParam,
isCookieParam, isBodyParam, isFile, notFile, hasMore, isContainer,
secondaryParam, isBinary, isCollectionFormatMulti;
isCookieParam, isBodyParam, hasMore, isContainer,
secondaryParam, isCollectionFormatMulti;
public String baseName, paramName, dataType, collectionFormat, description, baseType, defaultValue;
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 List<String> _enum;
public Map<String, Object> allowableValues;
Expand Down Expand Up @@ -111,6 +114,17 @@ public CodegenParameter copy() {
}
output.vendorExtensions = this.vendorExtensions;
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class CodegenProperty {
public Boolean exclusiveMaximum;
public Boolean hasMore, required, secondaryParam;
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 isReadOnly = false;
public List<String> _enum;
Expand Down Expand Up @@ -81,6 +83,18 @@ public int hashCode()
result = prime * result + ((setter == null) ? 0 : setter.hashCode());
result = prime * result + ((unescapedDescription == null) ? 0 : unescapedDescription.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;
}

Expand Down Expand Up @@ -189,6 +203,42 @@ public boolean equals(Object obj) {
if (this.vendorExtensions != other.vendorExtensions && (this.vendorExtensions == null || !this.vendorExtensions.equals(other.vendorExtensions))) {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ public CodegenProperty fromProperty(String name, Property p) {
property.maxLength = sp.getMaxLength();
property.minLength = sp.getMinLength();
property.pattern = sp.getPattern();
property.isString = true;
if (sp.getEnum() != null) {
List<String> _enum = sp.getEnum();
property._enum = _enum;
Expand All @@ -1034,6 +1035,7 @@ public CodegenProperty fromProperty(String name, Property p) {

if (p instanceof IntegerProperty) {
IntegerProperty sp = (IntegerProperty) p;
property.isInteger = true;
if (sp.getEnum() != null) {
List<Integer> _enum = sp.getEnum();
property._enum = new ArrayList<String>();
Expand All @@ -1051,6 +1053,7 @@ public CodegenProperty fromProperty(String name, Property p) {

if (p instanceof LongProperty) {
LongProperty sp = (LongProperty) p;
property.isLong = true;
if (sp.getEnum() != null) {
List<Long> _enum = sp.getEnum();
property._enum = new ArrayList<String>();
Expand All @@ -1066,8 +1069,21 @@ public CodegenProperty fromProperty(String name, Property p) {
}
}

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) {
DoubleProperty sp = (DoubleProperty) p;
property.isDouble = true;
if (sp.getEnum() != null) {
List<Double> _enum = sp.getEnum();
property._enum = new ArrayList<String>();
Expand All @@ -1085,6 +1101,7 @@ public CodegenProperty fromProperty(String name, Property p) {

if (p instanceof FloatProperty) {
FloatProperty sp = (FloatProperty) p;
property.isFloat = true;
if (sp.getEnum() != null) {
List<Float> _enum = sp.getEnum();
property._enum = new ArrayList<String>();
Expand All @@ -1102,6 +1119,7 @@ public CodegenProperty fromProperty(String name, Property p) {

if (p instanceof DateProperty) {
DateProperty sp = (DateProperty) p;
property.isDate = true;
if (sp.getEnum() != null) {
List<String> _enum = sp.getEnum();
property._enum = new ArrayList<String>();
Expand All @@ -1119,6 +1137,7 @@ public CodegenProperty fromProperty(String name, Property p) {

if (p instanceof DateTimeProperty) {
DateTimeProperty sp = (DateTimeProperty) p;
property.isDateTime = true;
if (sp.getEnum() != null) {
List<String> _enum = sp.getEnum();
property._enum = new ArrayList<String>();
Expand Down Expand Up @@ -1146,6 +1165,7 @@ public CodegenProperty fromProperty(String name, Property p) {

if (p instanceof ArrayProperty) {
property.isContainer = true;
property.isListContainer = true;
property.containerType = "array";
ArrayProperty ap = (ArrayProperty) p;
CodegenProperty cp = fromProperty(property.name, ap.getItems());
Expand All @@ -1168,6 +1188,7 @@ public CodegenProperty fromProperty(String name, Property p) {
}
} else if (p instanceof MapProperty) {
property.isContainer = true;
property.isMapContainer = true;
property.containerType = "map";
MapProperty ap = (MapProperty) p;
CodegenProperty cp = fromProperty("inner", ap.getAdditionalProperties());
Expand Down Expand Up @@ -1575,7 +1596,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
if (null == type) {
LOGGER.warn("Type is NULL for Serializable Parameter: " + param);
}
if ("array".equals(type)) {
if ("array".equals(type)) { // for array parameter
Property inner = qp.getItems();
if (inner == null) {
LOGGER.warn("warning! No inner type supplied for array parameter \"" + qp.getName() + "\", using String");
Expand All @@ -1589,8 +1610,9 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
CodegenProperty pr = fromProperty("inner", inner);
p.baseType = pr.datatype;
p.isContainer = true;
p.isListContainer = true;
imports.add(pr.baseType);
} else if ("object".equals(type)) {
} else if ("object".equals(type)) { // for map parameter
Property inner = qp.getItems();
if (inner == null) {
LOGGER.warn("warning! No inner type supplied for map parameter \"" + qp.getName() + "\", using String");
Expand All @@ -1600,19 +1622,26 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
collectionFormat = qp.getCollectionFormat();
CodegenProperty pr = fromProperty("inner", inner);
p.baseType = pr.datatype;
p.isContainer = true;
p.isMapContainer = true;
imports.add(pr.baseType);
} else {
Map<PropertyId, Object> args = new HashMap<PropertyId, Object>();
String format = qp.getFormat();
args.put(PropertyId.ENUM, qp.getEnum());
property = PropertyBuilder.build(type, format, args);
}

if (property == null) {
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.setRequired(param.getRequired());
CodegenProperty model = fromProperty(qp.getName(), property);

// set boolean flag (e.g. isString)
setParameterBooleanFlagWithCodegenProperty(p, model);

p.dataType = model.datatype;
p.isEnum = model.isEnum;
p._enum = model._enum;
Expand Down Expand Up @@ -1663,6 +1692,9 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
p.dataType = cp.datatype;
p.isBinary = cp.datatype.toLowerCase().startsWith("byte");
}

// set boolean flag (e.g. isString)
setParameterBooleanFlagWithCodegenProperty(p, cp);
}
} else if (model instanceof ArrayModel) {
// to use the built-in model parsing, we unwrap the ArrayModel
Expand All @@ -1678,6 +1710,10 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
imports.add(cp.baseType);
p.dataType = cp.datatype;
p.isContainer = true;
p.isListContainer = true;

// set boolean flag (e.g. isString)
setParameterBooleanFlagWithCodegenProperty(p, cp);
} else {
Model sub = bp.getSchema();
if (sub instanceof RefModel) {
Expand Down Expand Up @@ -2157,6 +2193,8 @@ public void setLibrary(String library) {

/**
* Library template (sub-template).
*
* @return Library template
*/
public String getLibrary() {
return library;
Expand Down Expand Up @@ -2221,10 +2259,10 @@ public String sanitizeName(String name) {
}

/**
* only write if the file doesn't exist
* Only write if the file doesn't exist
*
* @param outputFolder
* @param supportingFile
* @param outputFolder Output folder
* @param supportingFile Supporting file
*/
public void writeOptional(String outputFolder, SupportingFile supportingFile) {
String folder = "";
Expand All @@ -2243,4 +2281,46 @@ public void writeOptional(String outputFolder, SupportingFile 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ public String toDefaultValueWithParam(String name, Property p) {

/**
* Normalize type by wrapping primitive types with single quotes.
*
* @param type Primitive type
* @return Normalized type
*/
public String normalizeType(String type) {
return type.replaceAll("\\b(Boolean|Integer|Number|String|Date)\\b", "'$1'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ public void setPackageVersion(String packageVersion) {
*
* (PEP 0008) Python packages should also have short, all-lowercase names,
* although the use of underscores is discouraged.
*
* @param packageName Package name
* @return Python package name that conforms to PEP 0008
*/
@SuppressWarnings("static-method")
public String generatePackageName(String packageName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ public String getHelp() {

/**
* 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")
public String generateModuleName(String gemName) {
Expand All @@ -219,6 +222,9 @@ public String generateModuleName(String gemName) {

/**
* 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")
public String generateGemName(String moduleName) {
Expand Down

0 comments on commit a65e6db

Please sign in to comment.