From 7d07458b4514f04478f8ed4bcf9b0f2676382fa5 Mon Sep 17 00:00:00 2001 From: wing328 Date: Fri, 23 Mar 2018 18:25:08 +0800 Subject: [PATCH] fix issues with form parameter --- .../codegen/CodegenParameter.java | 69 +++++++- .../openapitools/codegen/CodegenProperty.java | 80 +++++++++- .../openapitools/codegen/DefaultCodegen.java | 149 +++++++++++++----- .../codegen/languages/PhpClientCodegen.java | 4 +- 4 files changed, 248 insertions(+), 54 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java index f679f1962b9..e9107585449 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java @@ -149,11 +149,6 @@ public class CodegenParameter { return output; } - @Override - public String toString() { - return String.format("%s(%s)", baseName, dataType); - } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -344,5 +339,69 @@ public class CodegenParameter { 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 + + '}'; + } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java index cbd72dda5c0..35d863efb4a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java @@ -375,11 +375,6 @@ public class CodegenProperty implements Cloneable { this.xmlNamespace = xmlNamespace; } - @Override - public String toString() { - return String.format("%s(%s)", baseName, datatype); - } - @Override 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 + + '}'; + } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 6a68e93679a..cff449feeec 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -1515,6 +1515,7 @@ public class DefaultCodegen implements CodegenConfig { LOGGER.error("Unexpected missing property for name " + name); return null; } + LOGGER.info("debugging fromProperty for " + name + " : " + p); CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY); property.name = toVarName(name); @@ -1549,12 +1550,12 @@ public class DefaultCodegen implements CodegenConfig { String type = getSchemaType(p); //LOGGER.info("from property = " + p); - if (p instanceof IntegerSchema) { + if (p instanceof IntegerSchema || SchemaTypeUtil.INTEGER_TYPE.equals(p.getType())) { property.isNumeric = Boolean.TRUE; if (SchemaTypeUtil.INTEGER64_FORMAT.equals(p.getFormat())) { - property.isInteger = Boolean.TRUE; - } else { property.isLong = Boolean.TRUE; + } else { + property.isInteger = Boolean.TRUE; } 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.minLength = p.getMinLength(); 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.getter = toBooleanGetter(name); } - if (p instanceof BinarySchema) { + if (p instanceof BinarySchema || SchemaTypeUtil.BINARY_FORMAT.equals(p.getFormat())) { property.isBinary = true; } - if (p instanceof FileSchema) { + if (p instanceof FileSchema) { //TODO revise file and binary 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 property.isString =true; property.isUuid = true; } - if (p instanceof ByteArraySchema) { + if (p instanceof ByteArraySchema || SchemaTypeUtil.BYTE_FORMAT.equals(p.getFormat())) { property.isByteArray = true; } - if (p instanceof NumberSchema) { + if (p instanceof NumberSchema || SchemaTypeUtil.NUMBER_TYPE.equals(p.getType())) { property.isNumeric = Boolean.TRUE; if (SchemaTypeUtil.FLOAT_FORMAT.equals(p.getFormat())) { // float 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; if (p.getEnum() != null) { List _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; if (p.getEnum() != null) { List _enum = p.getEnum(); @@ -1756,7 +1757,7 @@ public class DefaultCodegen implements CodegenConfig { setNonArrayMapProperty(property, type); } - //LOGGER.info("from property end = " + property); + LOGGER.info("debugging from property : " + property); return property; } @@ -2037,7 +2038,7 @@ public class DefaultCodegen implements CodegenConfig { } } // 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(getProducesInfo(operation)), responseSchema); op.defaultResponse = toDefaultValue(responseSchema); op.returnType = cm.datatype; op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType); @@ -2101,8 +2102,8 @@ public class DefaultCodegen implements CodegenConfig { bodyParam = fromRequestBody(requestBody, schemas, imports); bodyParams.add(bodyParam); if (schemas != null) { - // TODO need to replace "op.consumes" - //op.requestBodyExamples = new ExampleGenerator(schemas).generate(null, op.consumes, bodyParam.dataType); + // TODO fix NPE + //op.requestBodyExamples = new ExampleGenerator(schemas).generate(null, new ArrayList(getConsumesInfo(operation)), bodyParam.dataType); } allParams.add(bodyParam); } @@ -2110,6 +2111,8 @@ public class DefaultCodegen implements CodegenConfig { if (parameters != null) { for (Parameter param : parameters) { + LOGGER.info("parameter debugging " + param.getName() + " => " + param); + if (StringUtils.isNotBlank(param.get$ref())) { 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."); } - codegenParameter.required = Boolean.TRUE; 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) setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty); + + codegenParameter.required = codegenProperty.required; codegenParameter.dataType = codegenProperty.datatype; codegenParameter.dataFormat = codegenProperty.dataFormat; if (codegenProperty.isEnum) { @@ -3718,11 +3728,11 @@ public class DefaultCodegen implements CodegenConfig { scopes.add(scope); } - LOGGER.info("setOauth2Info setting scopes properly"); + //LOGGER.info("setOauth2Info setting scopes properly"); codegenSecurity.scopes = scopes; } - LOGGER.info("setOauth2Info scope: " + flow.getScopes()); + //LOGGER.info("setOauth2Info scope: " + flow.getScopes()); } private List getInterfaces(ComposedSchema composed) { @@ -3889,26 +3899,10 @@ public class DefaultCodegen implements CodegenConfig { Map properties = schema.getProperties(); for (Map.Entry entry : properties.entrySet()) { CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); - String name = entry.getKey(); - Schema propertySchema = entry.getValue(); - CodegenProperty codegenProperty = fromProperty(name, propertySchema); + // key => property anme + // value => property schema + 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); } } @@ -3920,6 +3914,82 @@ public class DefaultCodegen implements CodegenConfig { return parameters; } + public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set 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 schemas, Set imports) { if (body == null) { LOGGER.error("body in fromRequestBody cannot be null!"); @@ -3956,7 +4026,7 @@ public class DefaultCodegen implements CodegenConfig { codegenParameter.baseType = codegenProperty.baseType; codegenParameter.dataType = codegenProperty.datatype; 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) { imports.add(codegenProperty.complexType); @@ -4019,4 +4089,5 @@ public class DefaultCodegen implements CodegenConfig { } return false; } + } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java index 22d8f4a101e..06be2b11861 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java @@ -587,7 +587,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { type = p.dataType; } - if ("String".equalsIgnoreCase(type)) { + if ("String".equalsIgnoreCase(type) || p.isString) { if (example == null) { example = p.paramName + "_example"; } @@ -604,7 +604,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { if (example == null) { example = "True"; } - } else if ("\\SplFileObject".equalsIgnoreCase(type)) { + } else if ("\\SplFileObject".equalsIgnoreCase(type) || p.isFile) { if (example == null) { example = "/path/to/file"; }