format code, add helper function to check body/form param

This commit is contained in:
wing328 2018-04-01 22:25:22 +08:00
parent 240d1fe7eb
commit b0fc3e94a3
3 changed files with 201 additions and 177 deletions

View File

@ -399,6 +399,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* override with any special text escaping logic to handle unsafe
* characters so as to avoid code injection
*
* @param input String to be cleaned up
* @return string with unsafe characters removed or escaped
*/
@ -414,6 +415,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Escape single and/or double quote to avoid code injection
*
* @param input String to be cleaned up
* @return string with quotation mark removed or escaped
*/
@ -745,7 +747,7 @@ public class DefaultCodegen implements CodegenConfig {
*
* @param name the name to be escaped
* @return the escaped reserved word
*
* <p>
* throws Runtime exception as reserved word is not allowed (default behavior)
*/
@SuppressWarnings("static-method")
@ -782,8 +784,8 @@ public class DefaultCodegen implements CodegenConfig {
* This method will map between OAS type and language-specified type, as well as mapping
* between OAS type and the corresponding import statement for the language. This will
* also add some language specified CLI options, if any.
*
*
* <p>
* <p>
* returns string presentation of the example path (it's a constructor)
*/
public DefaultCodegen() {
@ -963,11 +965,9 @@ public class DefaultCodegen implements CodegenConfig {
paramPart.append("&").append(param.getName()).append("=");
paramPart.append(param.getName()).append("2");
}
}
else if (Parameter.StyleEnum.PIPEDELIMITED.equals(qp.getStyle())) {
} else if (Parameter.StyleEnum.PIPEDELIMITED.equals(qp.getStyle())) {
paramPart.append("|");
}
else if (Parameter.StyleEnum.SPACEDELIMITED.equals(qp.getStyle())) {
} else if (Parameter.StyleEnum.SPACEDELIMITED.equals(qp.getStyle())) {
paramPart.append("%20");
} else {
LOGGER.warn("query parameter '" + param.getName() + "style not support: " + qp.getStyle());
@ -1094,6 +1094,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* returns the OpenAPI type for the property
*
* @param schema property schema
* @return string presentation of the type
**/
@ -1216,6 +1217,7 @@ public class DefaultCodegen implements CodegenConfig {
* Determine the type alias for the given type if it exists. This feature
* is only used for Java, because the language does not have a aliasing
* mechanism of its own.
*
* @param name The type name.
* @return The alias of the given type, if it exists. If there is no alias
* for this type, then returns the input type name.
@ -1788,6 +1790,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Update property for array(list) container
*
* @param property Codegen property
* @param innerProperty Codegen inner property of map or list
*/
@ -1819,6 +1822,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Update property for map container
*
* @param property Codegen property
* @param innerProperty Codegen inner property of map or list
*/
@ -1850,6 +1854,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Update property for map container
*
* @param property Codegen property
* @return True if the inner most type is enum
*/
@ -1876,6 +1881,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Update datatypeWithEnum for array container
*
* @param property Codegen property
*/
protected void updateDataTypeWithEnumForArray(CodegenProperty property) {
@ -1903,6 +1909,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Update datatypeWithEnum for map container
*
* @param property Codegen property
*/
protected void updateDataTypeWithEnumForMap(CodegenProperty property) {
@ -1938,6 +1945,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Override with any special handling of response codes
*
* @param responses OAS Operation's responses
* @return default method response or <tt>null</tt> if not found
*/
@ -2683,6 +2691,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Returns the data type of a parameter.
* Returns null by default to use the CodegenProperty.datatype value
*
* @param parameter
* @param property
* @return
@ -3118,6 +3127,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Determine all of the types in the model definitions (schemas) that are aliases of
* simple types.
*
* @param schemas The complete set of model definitions (schemas).
* @return A mapping from model name to type alias
*/
@ -3262,7 +3272,6 @@ public class DefaultCodegen implements CodegenConfig {
*
* @param templateName template name
* @param tag tag
*
* @return the API documentation file name with full path
*/
public String apiDocFilename(String templateName, String tag) {
@ -3275,7 +3284,6 @@ public class DefaultCodegen implements CodegenConfig {
*
* @param templateName template name
* @param tag tag
*
* @return the API test file name with full path
*/
public String apiTestFilename(String templateName, String tag) {
@ -3306,6 +3314,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* All library templates supported.
* (key: library name, value: library description)
*
* @return the supported libraries
*/
public Map<String, String> supportedLibraries() {
@ -3470,8 +3479,7 @@ public class DefaultCodegen implements CodegenConfig {
// $php_variable => php_variable
if (allowUnicodeIdentifiers) { //could be converted to a single line with ?: operator
name = Pattern.compile("\\W", Pattern.UNICODE_CHARACTER_CLASS).matcher(name).replaceAll("");
}
else {
} else {
name = name.replaceAll("\\W", "");
}
@ -3510,8 +3518,7 @@ public class DefaultCodegen implements CodegenConfig {
folder += supportingFile.folder;
if (!"".equals(folder)) {
folder += File.separator + supportingFile.destinationFilename;
}
else {
} else {
folder = supportingFile.destinationFilename;
}
if (!new File(folder).exists()) {
@ -3679,7 +3686,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Provides an override location, if any is specified, for the .swagger-codegen-ignore.
*
* <p>
* This is originally intended for the first generation only.
*
* @return a string of the full path to an override ignore file.
@ -3815,6 +3822,44 @@ public class DefaultCodegen implements CodegenConfig {
return operation.getRequestBody().getContent().keySet();
}
public Boolean hasFormParameter(Operation operation) {
if (getConsumesInfo(operation) == null) {
return Boolean.FALSE;
}
List<String> consumes = new ArrayList<String>(getConsumesInfo(operation));
if (consumes == null) {
return Boolean.FALSE;
}
for (String consume : consumes) {
if ("application/x-www-form-urlencoded".equalsIgnoreCase(consume) || "multipart/form-data".equalsIgnoreCase(consume)) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
}
public Boolean hasBodyParameter(Operation operation) {
RequestBody requestBody = operation.getRequestBody();
if (requestBody == null) {
return Boolean.FALSE;
}
Schema schema = getSchemaFromBody(requestBody);
if (schema == null) {
return Boolean.FALSE;
}
if (!StringUtils.isEmpty(schema.get$ref())) {
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
}
private void addProducesInfo(ApiResponse response, CodegenOperation codegenOperation) {
if (response == null || response.getContent() == null || response.getContent().isEmpty()) {
return;
@ -3896,14 +3941,11 @@ public class DefaultCodegen implements CodegenConfig {
} else {
return "multi";
}
}
else if (Parameter.StyleEnum.PIPEDELIMITED.equals(parameter.getStyle())) {
} else if (Parameter.StyleEnum.PIPEDELIMITED.equals(parameter.getStyle())) {
return "pipe";
}
else if (Parameter.StyleEnum.SPACEDELIMITED.equals(parameter.getStyle())) {
} else if (Parameter.StyleEnum.SPACEDELIMITED.equals(parameter.getStyle())) {
return "space";
}
else {
} else {
return null;
}
}

View File

@ -2,12 +2,14 @@ package org.openapitools.codegen.languages;
import java.io.File;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import io.swagger.v3.oas.models.PathItem;
@ -599,18 +601,16 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
ArraySchema ap = (ArraySchema) p;
Schema inner = ap.getItems();
if (inner == null) {
LOGGER.warn(ap.getName() + "(array property) does not have a proper inner type defined");
// TODO maybe better defaulting to StringSchema than returning null
return null;
LOGGER.warn(ap.getName() + "(array property) does not have a proper inner type defined.Default to string");
inner = new StringSchema().description("TODO default missing array inner type to string");
}
return getSchemaType(p) + "<" + getTypeDeclaration(inner) + ">";
} else if (isMapSchema(p)) {
MapSchema mp = (MapSchema) p;
Schema inner = (Schema) mp.getAdditionalProperties();
if (inner == null) {
LOGGER.warn(mp.getName() + "(map property) does not have a proper inner type defined");
// TODO maybe better defaulting to StringSchema than returning null
return null;
LOGGER.warn(mp.getName() + "(map property) does not have a proper inner type defined. Default to string");
inner = new StringSchema().description("TODO default missing array inner type to string");
}
return getSchemaType(p) + "<String, " + getTypeDeclaration(inner) + ">";
}
@ -917,26 +917,15 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
continue;
}
for (Operation operation : path.readOperations()) {
// TODO revise the logic below
/*
boolean hasFormParameters = false;
boolean hasBodyParameters = false;
for (Parameter parameter : operation.getParameters()) {
if (parameter instanceof FormParameter) {
hasFormParameters = true;
}
if (parameter instanceof BodyParameter) {
hasBodyParameters = true;
}
}
if (hasBodyParameters || hasFormParameters){
String defaultContentType = hasFormParameters ? "application/x-www-form-urlencoded" : "application/json";
String contentType = operation.getConsumes() == null || operation.getConsumes().isEmpty() ? defaultContentType : operation.getConsumes().get(0);
operation.setExtensions("x-contentType", contentType);
if (hasBodyParameter(operation) || hasFormParameter(operation)){
String defaultContentType = hasFormParameter(operation) ? "application/x-www-form-urlencoded" : "application/json";
List<String> consumes = new ArrayList<String>(getConsumesInfo(operation));
String contentType = consumes == null || consumes.isEmpty() ? defaultContentType : consumes.get(0);
operation.getExtensions().put("x-contentType", contentType);
}
String accepts = getAccept(operation);
operation.setExtension("x-accepts", accepts);
*/
operation.getExtensions().put("x-accepts", accepts);
}
}
}
@ -944,18 +933,18 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
private static String getAccept(Operation operation) {
String accepts = null;
String defaultContentType = "application/json";
/* TODO need to revise the logic below
if (operation.getProduces() != null && !operation.getProduces().isEmpty()) {
ArrayList<String> produces = new ArrayList<String>(getProducesInfo(operation));
if (produces != null && !produces.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (String produces : operation.getProduces()) {
if (defaultContentType.equalsIgnoreCase(produces)) {
for (String produce : produces) {
if (defaultContentType.equalsIgnoreCase(produce)) {
accepts = defaultContentType;
break;
} else {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(produces);
sb.append(produce);
}
}
if (accepts == null) {
@ -964,7 +953,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
} else {
accepts = defaultContentType;
}
*/
return accepts;
}

View File

@ -277,18 +277,12 @@ public class ApexClientCodegen extends AbstractJavaCodegen {
Map<String, Schema> definitions,
OpenAPI openAPI) {
Boolean hasFormParams = false;
// need to revise the logic below as there's no form parameters
// comment out the following as there's no consume/produce in OAS3.0
// we can move the logic below to postProcessOperations if needed
/*
for (Parameter p : operation.getParameters()) {
if ("formData".equals(p.getIn())) {
hasFormParams = true;
break;
}
}
// only support serialization into JSON and urlencoded forms for now
operation.setConsumes(
Collections.singletonList(hasFormParams
Collections.singletonList(hasFormParameter(operation)
? "application/x-www-form-urlencoded"
: "application/json"));