add boolean flag to codegen parameter

This commit is contained in:
wing328
2016-02-24 17:00:33 +08:00
parent 961ee39760
commit 7fb41048dd
2 changed files with 75 additions and 4 deletions

View File

@@ -7,10 +7,13 @@ import java.util.List;
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;
@@ -111,6 +114,17 @@ public class CodegenParameter {
}
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;
}

View File

@@ -1596,7 +1596,7 @@ public class DefaultCodegen {
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");
@@ -1610,8 +1610,9 @@ public class DefaultCodegen {
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");
@@ -1621,6 +1622,8 @@ public class DefaultCodegen {
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>();
@@ -1628,12 +1631,17 @@ public class DefaultCodegen {
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;
@@ -1684,6 +1692,9 @@ public class DefaultCodegen {
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
@@ -1699,6 +1710,10 @@ public class DefaultCodegen {
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) {
@@ -2264,4 +2279,46 @@ public class DefaultCodegen {
supportingFiles.add(supportingFile);
}
}
/**
* Set CodegenParameter boolean flag using CodegenProperty.
*
* @param p Codegen Parameter
* @param cp 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);
}
}
}