fix issues with form parameter

This commit is contained in:
wing328 2018-03-23 18:25:08 +08:00
parent ce6efcf5bb
commit 7d07458b45
4 changed files with 248 additions and 54 deletions

View File

@ -149,11 +149,6 @@ public class CodegenParameter {
return output; return output;
} }
@Override
public String toString() {
return String.format("%s(%s)", baseName, dataType);
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
@ -344,5 +339,69 @@ public class CodegenParameter {
return result; return result;
} }
@java.lang.Override
public java.lang.String toString() {
return "CodegenParameter{" +
"isFormParam=" + isFormParam +
", isQueryParam=" + isQueryParam +
", isPathParam=" + isPathParam +
", isHeaderParam=" + isHeaderParam +
", isCookieParam=" + isCookieParam +
", isBodyParam=" + isBodyParam +
", hasMore=" + hasMore +
", isContainer=" + isContainer +
", secondaryParam=" + secondaryParam +
", isCollectionFormatMulti=" + isCollectionFormatMulti +
", isPrimitiveType=" + isPrimitiveType +
", baseName='" + baseName + '\'' +
", paramName='" + paramName + '\'' +
", dataType='" + dataType + '\'' +
", datatypeWithEnum='" + datatypeWithEnum + '\'' +
", dataFormat='" + dataFormat + '\'' +
", collectionFormat='" + collectionFormat + '\'' +
", description='" + description + '\'' +
", unescapedDescription='" + unescapedDescription + '\'' +
", baseType='" + baseType + '\'' +
", defaultValue='" + defaultValue + '\'' +
", enumName='" + enumName + '\'' +
", example='" + example + '\'' +
", jsonSchema='" + jsonSchema + '\'' +
", isString=" + isString +
", isNumeric=" + isNumeric +
", isInteger=" + isInteger +
", isLong=" + isLong +
", isNumber=" + isNumber +
", isFloat=" + isFloat +
", isDouble=" + isDouble +
", isByteArray=" + isByteArray +
", isBinary=" + isBinary +
", isBoolean=" + isBoolean +
", isDate=" + isDate +
", isDateTime=" + isDateTime +
", isUuid=" + isUuid +
", isListContainer=" + isListContainer +
", isMapContainer=" + isMapContainer +
", isFile=" + isFile +
", notFile=" + notFile +
", isEnum=" + isEnum +
", _enum=" + _enum +
", allowableValues=" + allowableValues +
", items=" + items +
", vendorExtensions=" + vendorExtensions +
", hasValidation=" + hasValidation +
", required=" + required +
", maximum='" + maximum + '\'' +
", exclusiveMaximum=" + exclusiveMaximum +
", minimum='" + minimum + '\'' +
", exclusiveMinimum=" + exclusiveMinimum +
", maxLength=" + maxLength +
", minLength=" + minLength +
", pattern='" + pattern + '\'' +
", maxItems=" + maxItems +
", minItems=" + minItems +
", uniqueItems=" + uniqueItems +
", multipleOf=" + multipleOf +
'}';
}
} }

View File

@ -375,11 +375,6 @@ public class CodegenProperty implements Cloneable {
this.xmlNamespace = xmlNamespace; this.xmlNamespace = xmlNamespace;
} }
@Override
public String toString() {
return String.format("%s(%s)", baseName, datatype);
}
@Override @Override
public int hashCode() public int hashCode()
{ {
@ -679,7 +674,76 @@ public class CodegenProperty implements Cloneable {
} }
} }
@java.lang.Override
public java.lang.String toString() {
return "CodegenProperty{" +
"baseName='" + baseName + '\'' +
", complexType='" + complexType + '\'' +
", getter='" + getter + '\'' +
", setter='" + setter + '\'' +
", description='" + description + '\'' +
", datatype='" + datatype + '\'' +
", datatypeWithEnum='" + datatypeWithEnum + '\'' +
", dataFormat='" + dataFormat + '\'' +
", name='" + name + '\'' +
", min='" + min + '\'' +
", max='" + max + '\'' +
", defaultValue='" + defaultValue + '\'' +
", defaultValueWithParam='" + defaultValueWithParam + '\'' +
", baseType='" + baseType + '\'' +
", containerType='" + containerType + '\'' +
", title='" + title + '\'' +
", unescapedDescription='" + unescapedDescription + '\'' +
", maxLength=" + maxLength +
", minLength=" + minLength +
", pattern='" + pattern + '\'' +
", example='" + example + '\'' +
", jsonSchema='" + jsonSchema + '\'' +
", minimum='" + minimum + '\'' +
", maximum='" + maximum + '\'' +
", exclusiveMinimum=" + exclusiveMinimum +
", exclusiveMaximum=" + exclusiveMaximum +
", hasMore=" + hasMore +
", required=" + required +
", secondaryParam=" + secondaryParam +
", hasMoreNonReadOnly=" + hasMoreNonReadOnly +
", isPrimitiveType=" + isPrimitiveType +
", isContainer=" + isContainer +
", isNotContainer=" + isNotContainer +
", isString=" + isString +
", isNumeric=" + isNumeric +
", isInteger=" + isInteger +
", isLong=" + isLong +
", isNumber=" + isNumber +
", isFloat=" + isFloat +
", isDouble=" + isDouble +
", isByteArray=" + isByteArray +
", isBinary=" + isBinary +
", isFile=" + isFile +
", isBoolean=" + isBoolean +
", isDate=" + isDate +
", isDateTime=" + isDateTime +
", isUuid=" + isUuid +
", isListContainer=" + isListContainer +
", isMapContainer=" + isMapContainer +
", isEnum=" + isEnum +
", isReadOnly=" + isReadOnly +
", _enum=" + _enum +
", allowableValues=" + allowableValues +
", items=" + items +
", vendorExtensions=" + vendorExtensions +
", hasValidation=" + hasValidation +
", isInherited=" + isInherited +
", discriminatorValue='" + discriminatorValue + '\'' +
", nameInCamelCase='" + nameInCamelCase + '\'' +
", enumName='" + enumName + '\'' +
", maxItems=" + maxItems +
", minItems=" + minItems +
", isXmlAttribute=" + isXmlAttribute +
", xmlPrefix='" + xmlPrefix + '\'' +
", xmlName='" + xmlName + '\'' +
", xmlNamespace='" + xmlNamespace + '\'' +
", isXmlWrapped=" + isXmlWrapped +
'}';
}
} }

View File

@ -1515,6 +1515,7 @@ public class DefaultCodegen implements CodegenConfig {
LOGGER.error("Unexpected missing property for name " + name); LOGGER.error("Unexpected missing property for name " + name);
return null; return null;
} }
LOGGER.info("debugging fromProperty for " + name + " : " + p);
CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY); CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY);
property.name = toVarName(name); property.name = toVarName(name);
@ -1549,12 +1550,12 @@ public class DefaultCodegen implements CodegenConfig {
String type = getSchemaType(p); String type = getSchemaType(p);
//LOGGER.info("from property = " + p); //LOGGER.info("from property = " + p);
if (p instanceof IntegerSchema) { if (p instanceof IntegerSchema || SchemaTypeUtil.INTEGER_TYPE.equals(p.getType())) {
property.isNumeric = Boolean.TRUE; property.isNumeric = Boolean.TRUE;
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(p.getFormat())) { if (SchemaTypeUtil.INTEGER64_FORMAT.equals(p.getFormat())) {
property.isInteger = Boolean.TRUE;
} else {
property.isLong = Boolean.TRUE; property.isLong = Boolean.TRUE;
} else {
property.isInteger = Boolean.TRUE;
} }
if (p.getMinimum() != null) { if (p.getMinimum() != null) {
@ -1599,7 +1600,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
} }
if (p instanceof StringSchema) { if (p instanceof StringSchema || SchemaTypeUtil.STRING_TYPE.equals(p.getType())) {
property.maxLength = p.getMaxLength(); property.maxLength = p.getMaxLength();
property.minLength = p.getMinLength(); property.minLength = p.getMinLength();
property.pattern = toRegularExpression(p.getPattern()); property.pattern = toRegularExpression(p.getPattern());
@ -1621,26 +1622,26 @@ public class DefaultCodegen implements CodegenConfig {
} }
} }
if (p instanceof BooleanSchema) { if (p instanceof BooleanSchema || SchemaTypeUtil.BOOLEAN_TYPE.equals(p.getType())) {
property.isBoolean = true; property.isBoolean = true;
property.getter = toBooleanGetter(name); property.getter = toBooleanGetter(name);
} }
if (p instanceof BinarySchema) { if (p instanceof BinarySchema || SchemaTypeUtil.BINARY_FORMAT.equals(p.getFormat())) {
property.isBinary = true; property.isBinary = true;
} }
if (p instanceof FileSchema) { if (p instanceof FileSchema) { //TODO revise file and binary
property.isFile = true; property.isFile = true;
} }
if (p instanceof UUIDSchema) { if (p instanceof UUIDSchema || SchemaTypeUtil.UUID_FORMAT.equals(p.getFormat())) {
// keep isString to true to make it backward compatible // keep isString to true to make it backward compatible
property.isString =true; property.isString =true;
property.isUuid = true; property.isUuid = true;
} }
if (p instanceof ByteArraySchema) { if (p instanceof ByteArraySchema || SchemaTypeUtil.BYTE_FORMAT.equals(p.getFormat())) {
property.isByteArray = true; property.isByteArray = true;
} }
if (p instanceof NumberSchema) { if (p instanceof NumberSchema || SchemaTypeUtil.NUMBER_TYPE.equals(p.getType())) {
property.isNumeric = Boolean.TRUE; property.isNumeric = Boolean.TRUE;
if (SchemaTypeUtil.FLOAT_FORMAT.equals(p.getFormat())) { // float if (SchemaTypeUtil.FLOAT_FORMAT.equals(p.getFormat())) { // float
property.isFloat = Boolean.TRUE; property.isFloat = Boolean.TRUE;
@ -1665,7 +1666,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
} }
if (p instanceof DateSchema) { if (p instanceof DateSchema || SchemaTypeUtil.DATE_FORMAT.equals(p.getFormat())) {
property.isDate = true; property.isDate = true;
if (p.getEnum() != null) { if (p.getEnum() != null) {
List<String> _enum = p.getEnum(); List<String> _enum = p.getEnum();
@ -1682,7 +1683,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
} }
if (p instanceof DateTimeSchema) { if (p instanceof DateTimeSchema || SchemaTypeUtil.DATE_TIME_FORMAT.equals(p.getFormat())) {
property.isDateTime = true; property.isDateTime = true;
if (p.getEnum() != null) { if (p.getEnum() != null) {
List<String> _enum = p.getEnum(); List<String> _enum = p.getEnum();
@ -1756,7 +1757,7 @@ public class DefaultCodegen implements CodegenConfig {
setNonArrayMapProperty(property, type); setNonArrayMapProperty(property, type);
} }
//LOGGER.info("from property end = " + property); LOGGER.info("debugging from property : " + property);
return property; return property;
} }
@ -2037,7 +2038,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
} }
// TODO need to revise the logic below // TODO need to revise the logic below
//op.examples = new ExampleGenerator(definitions).generate(methodResponse.getExamples(), operation.getProduces(), responseProperty); //op.examples = new ExampleGenerator(schemas).generate(responseSchema.getExample(), new ArrayList<String>(getProducesInfo(operation)), responseSchema);
op.defaultResponse = toDefaultValue(responseSchema); op.defaultResponse = toDefaultValue(responseSchema);
op.returnType = cm.datatype; op.returnType = cm.datatype;
op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType); op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType);
@ -2101,8 +2102,8 @@ public class DefaultCodegen implements CodegenConfig {
bodyParam = fromRequestBody(requestBody, schemas, imports); bodyParam = fromRequestBody(requestBody, schemas, imports);
bodyParams.add(bodyParam); bodyParams.add(bodyParam);
if (schemas != null) { if (schemas != null) {
// TODO need to replace "op.consumes" // TODO fix NPE
//op.requestBodyExamples = new ExampleGenerator(schemas).generate(null, op.consumes, bodyParam.dataType); //op.requestBodyExamples = new ExampleGenerator(schemas).generate(null, new ArrayList<String>(getConsumesInfo(operation)), bodyParam.dataType);
} }
allParams.add(bodyParam); allParams.add(bodyParam);
} }
@ -2110,6 +2111,8 @@ public class DefaultCodegen implements CodegenConfig {
if (parameters != null) { if (parameters != null) {
for (Parameter param : parameters) { for (Parameter param : parameters) {
LOGGER.info("parameter debugging " + param.getName() + " => " + param);
if (StringUtils.isNotBlank(param.get$ref())) { if (StringUtils.isNotBlank(param.get$ref())) {
param = getParameterFromRef(param.get$ref(), openAPI); param = getParameterFromRef(param.get$ref(), openAPI);
} }
@ -2408,12 +2411,19 @@ public class DefaultCodegen implements CodegenConfig {
parameterSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to missing type definition."); parameterSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to missing type definition.");
} }
codegenParameter.required = Boolean.TRUE;
CodegenProperty codegenProperty = fromProperty(parameter.getName(), parameterSchema); CodegenProperty codegenProperty = fromProperty(parameter.getName(), parameterSchema);
LOGGER.info("fromProperty debug required: " + parameter.getRequired());
// TODO revise below which seems not working
//if (parameterSchema.getRequired() != null && !parameterSchema.getRequired().isEmpty() && parameterSchema.getRequired().contains(codegenProperty.baseName)) {
codegenProperty.required = Boolean.TRUE.equals(parameter.getRequired()) ? true : false;
//}
//codegenProperty.required = true;
// set boolean flag (e.g. isString) // set boolean flag (e.g. isString)
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty); setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
codegenParameter.required = codegenProperty.required;
codegenParameter.dataType = codegenProperty.datatype; codegenParameter.dataType = codegenProperty.datatype;
codegenParameter.dataFormat = codegenProperty.dataFormat; codegenParameter.dataFormat = codegenProperty.dataFormat;
if (codegenProperty.isEnum) { if (codegenProperty.isEnum) {
@ -3718,11 +3728,11 @@ public class DefaultCodegen implements CodegenConfig {
scopes.add(scope); scopes.add(scope);
} }
LOGGER.info("setOauth2Info setting scopes properly"); //LOGGER.info("setOauth2Info setting scopes properly");
codegenSecurity.scopes = scopes; codegenSecurity.scopes = scopes;
} }
LOGGER.info("setOauth2Info scope: " + flow.getScopes()); //LOGGER.info("setOauth2Info scope: " + flow.getScopes());
} }
private List<Schema> getInterfaces(ComposedSchema composed) { private List<Schema> getInterfaces(ComposedSchema composed) {
@ -3889,26 +3899,10 @@ public class DefaultCodegen implements CodegenConfig {
Map<String, Schema> properties = schema.getProperties(); Map<String, Schema> properties = schema.getProperties();
for (Map.Entry<String, Schema> entry : properties.entrySet()) { for (Map.Entry<String, Schema> entry : properties.entrySet()) {
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
String name = entry.getKey(); // key => property anme
Schema propertySchema = entry.getValue(); // value => property schema
CodegenProperty codegenProperty = fromProperty(name, propertySchema); codegenParameter = fromFormProperty(entry.getKey(), entry.getValue(), imports);
codegenParameter.isFormParam = Boolean.TRUE;
codegenParameter.baseName = codegenProperty.baseName;
codegenParameter.paramName = toParamName((codegenParameter.baseName));
codegenParameter.baseType = codegenProperty.baseType;
codegenParameter.dataType = codegenProperty.datatype;
codegenParameter.description = codegenProperty.description;
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
setParameterExampleValue(codegenParameter);
if (codegenProperty.getVendorExtensions() != null && !codegenProperty.getVendorExtensions().isEmpty()) {
codegenParameter.vendorExtensions = codegenProperty.getVendorExtensions();
}
if (schema.getRequired() != null && !schema.getRequired().isEmpty() && schema.getRequired().contains(codegenProperty.baseName)) {
codegenParameter.required = Boolean.TRUE;
}
//TODO collectionFormat for form parameter not yet supported
//codegenParameter.collectionFormat = getCollectionFormat(propertySchema);
parameters.add(codegenParameter); parameters.add(codegenParameter);
} }
} }
@ -3920,6 +3914,82 @@ public class DefaultCodegen implements CodegenConfig {
return parameters; return parameters;
} }
public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set<String> imports) {
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
LOGGER.info("Debugging fromRequestBodyToFormParameters: " + name);
CodegenProperty codegenProperty = fromProperty(name, propertySchema);
codegenParameter.isFormParam = Boolean.TRUE;
codegenParameter.baseName = codegenProperty.baseName;
codegenParameter.paramName = toParamName((codegenParameter.baseName));
codegenParameter.baseType = codegenProperty.baseType;
codegenParameter.dataType = codegenProperty.datatype;
codegenParameter.dataFormat = codegenProperty.dataFormat;
codegenParameter.description = escapeText(codegenProperty.description);
codegenParameter.unescapedDescription = codegenProperty.getDescription();
codegenParameter.jsonSchema = Json.pretty(propertySchema);
if (codegenProperty.getVendorExtensions() != null && !codegenProperty.getVendorExtensions().isEmpty()) {
codegenParameter.vendorExtensions = codegenProperty.getVendorExtensions();
}
if (propertySchema.getRequired() != null && !propertySchema.getRequired().isEmpty() && propertySchema.getRequired().contains(codegenProperty.baseName)) {
codegenParameter.required = Boolean.TRUE;
}
// non-array/map
updateCodegenPropertyEnum(codegenProperty);
codegenParameter.isEnum = codegenProperty.isEnum;
codegenParameter._enum = codegenProperty._enum;
codegenParameter.allowableValues = codegenProperty.allowableValues;
if (codegenProperty.items != null && codegenProperty.items.isEnum) {
codegenParameter.datatypeWithEnum = codegenProperty.datatypeWithEnum;
codegenParameter.enumName = codegenProperty.enumName;
codegenParameter.items = codegenProperty.items;
}
// import
if (codegenProperty.complexType != null) {
imports.add(codegenProperty.complexType);
}
// validation
// handle maximum, minimum properly for int/long by removing the trailing ".0"
if (propertySchema instanceof IntegerSchema) {
codegenParameter.maximum = propertySchema.getMaximum() == null ? null : String.valueOf(propertySchema.getMaximum().longValue());
codegenParameter.minimum = propertySchema.getMinimum() == null ? null : String.valueOf(propertySchema.getMinimum().longValue());
} else {
codegenParameter.maximum = propertySchema.getMaximum() == null ? null : String.valueOf(propertySchema.getMaximum());
codegenParameter.minimum = propertySchema.getMinimum() == null ? null : String.valueOf(propertySchema.getMinimum());
}
codegenParameter.exclusiveMaximum = propertySchema.getExclusiveMaximum() == null ? false : propertySchema.getExclusiveMaximum();
codegenParameter.exclusiveMinimum = propertySchema.getExclusiveMinimum() == null ? false : propertySchema.getExclusiveMinimum();
codegenParameter.maxLength = propertySchema.getMaxLength();
codegenParameter.minLength = propertySchema.getMinLength();
codegenParameter.pattern = toRegularExpression(propertySchema.getPattern());
codegenParameter.maxItems = propertySchema.getMaxItems();
codegenParameter.minItems = propertySchema.getMinItems();
codegenParameter.uniqueItems = propertySchema.getUniqueItems() == null ? false : propertySchema.getUniqueItems();
codegenParameter.multipleOf = propertySchema.getMultipleOf();
// exclusive* are noop without corresponding min/max
if (codegenParameter.maximum != null || codegenParameter.minimum != null ||
codegenParameter.maxLength != null || codegenParameter.minLength != null ||
codegenParameter.maxItems != null || codegenParameter.minItems != null ||
codegenParameter.pattern != null) {
codegenParameter.hasValidation = true;
}
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
setParameterExampleValue(codegenParameter);
//TODO collectionFormat for form parameter not yet supported
//codegenParameter.collectionFormat = getCollectionFormat(propertySchema);
return codegenParameter;
}
public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> schemas, Set<String> imports) { public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> schemas, Set<String> imports) {
if (body == null) { if (body == null) {
LOGGER.error("body in fromRequestBody cannot be null!"); LOGGER.error("body in fromRequestBody cannot be null!");
@ -3956,7 +4026,7 @@ public class DefaultCodegen implements CodegenConfig {
codegenParameter.baseType = codegenProperty.baseType; codegenParameter.baseType = codegenProperty.baseType;
codegenParameter.dataType = codegenProperty.datatype; codegenParameter.dataType = codegenProperty.datatype;
codegenParameter.description = codegenProperty.description; codegenParameter.description = codegenProperty.description;
LOGGER.info("Seting description to body parameter: " + codegenProperty.description); LOGGER.info("Setting description to body parameter: " + codegenProperty.description);
if (codegenProperty.complexType != null) { if (codegenProperty.complexType != null) {
imports.add(codegenProperty.complexType); imports.add(codegenProperty.complexType);
@ -4019,4 +4089,5 @@ public class DefaultCodegen implements CodegenConfig {
} }
return false; return false;
} }
} }

View File

@ -587,7 +587,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
type = p.dataType; type = p.dataType;
} }
if ("String".equalsIgnoreCase(type)) { if ("String".equalsIgnoreCase(type) || p.isString) {
if (example == null) { if (example == null) {
example = p.paramName + "_example"; example = p.paramName + "_example";
} }
@ -604,7 +604,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
if (example == null) { if (example == null) {
example = "True"; example = "True";
} }
} else if ("\\SplFileObject".equalsIgnoreCase(type)) { } else if ("\\SplFileObject".equalsIgnoreCase(type) || p.isFile) {
if (example == null) { if (example == null) {
example = "/path/to/file"; example = "/path/to/file";
} }