[Flask] Add generated tests (#4209)

This commit is contained in:
Christophe Bornet
2016-11-19 09:31:31 +01:00
committed by wing328
parent b02d505ad9
commit df15799839
17 changed files with 806 additions and 38 deletions

View File

@@ -2392,6 +2392,31 @@ public class DefaultCodegen {
p.paramName = toParamName(bp.getName());
}
// Issue #2561 (neilotoole) : Set the is<TYPE>Param flags.
// This code has been moved to here from #fromOperation
// because these values should be set before calling #postProcessParameter.
// See: https://github.com/swagger-api/swagger-codegen/issues/2561
if (param instanceof QueryParameter) {
p.isQueryParam = true;
} else if (param instanceof PathParameter) {
p.required = true;
p.isPathParam = true;
} else if (param instanceof HeaderParameter) {
p.isHeaderParam = true;
} else if (param instanceof CookieParameter) {
p.isCookieParam = true;
} else if (param instanceof BodyParameter) {
p.isBodyParam = true;
p.isBinary = isDataTypeBinary(p.dataType);
} else if (param instanceof FormParameter) {
if ("file".equalsIgnoreCase(((FormParameter) param).getType()) || "file".equals(p.baseType)) {
p.isFile = true;
} else {
p.notFile = true;
}
p.isFormParam = true;
}
// set the example value
// if not specified in x-example, generate a default value
if (p.vendorExtensions.containsKey("x-example")) {
@@ -2416,10 +2441,7 @@ public class DefaultCodegen {
p.example = "2013-10-20";
} else if (Boolean.TRUE.equals(p.isDateTime)) {
p.example = "2013-10-20T19:20:30+01:00";
} else if (param instanceof FormParameter &&
("file".equalsIgnoreCase(((FormParameter) param).getType()) ||
"file".equals(p.baseType))) {
p.isFile = true;
} else if (Boolean.TRUE.equals(p.isFile)) {
p.example = "/path/to/file.txt";
}
@@ -2427,33 +2449,6 @@ public class DefaultCodegen {
// should be overridden by lang codegen
setParameterExampleValue(p);
// Issue #2561 (neilotoole) : Set the is<TYPE>Param flags.
// This code has been moved to here from #fromOperation
// because these values should be set before calling #postProcessParameter.
// See: https://github.com/swagger-api/swagger-codegen/issues/2561
if (param instanceof QueryParameter) {
p.isQueryParam = true;
} else if (param instanceof PathParameter) {
p.required = true;
p.isPathParam = true;
} else if (param instanceof HeaderParameter) {
p.isHeaderParam = true;
} else if (param instanceof CookieParameter) {
p.isCookieParam = true;
} else if (param instanceof BodyParameter) {
p.isBodyParam = true;
p.isBinary = isDataTypeBinary(p.dataType);
} else if (param instanceof FormParameter) {
if ("file".equalsIgnoreCase(((FormParameter) param).getType())) {
p.isFile = true;
} else if("file".equals(p.baseType)){
p.isFile = true;
} else {
p.notFile = true;
}
p.isFormParam = true;
}
postProcessParameter(p);
return p;
}

View File

@@ -10,6 +10,8 @@ import io.swagger.models.HttpMethod;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.BodyParameter;
import io.swagger.models.parameters.FormParameter;
import io.swagger.models.properties.*;
import io.swagger.util.Yaml;
@@ -36,6 +38,8 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
public FlaskConnexionCodegen() {
super();
modelPackage = "models";
testPackage = "test";
languageSpecificPrimitives.clear();
languageSpecificPrimitives.add("int");
@@ -68,6 +72,7 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
apiTemplateFiles.put("controller.mustache", ".py");
modelTemplateFiles.put("model.mustache", ".py");
apiTestTemplateFiles().put("controller_test.mustache", ".py");
/*
* Template Location. This is the location which templates will be read from. The generator
@@ -167,6 +172,11 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
modelPackage,
"base_model_.py")
);
supportingFiles.add(new SupportingFile("__init__test.mustache",
testPackage,
"__init__.py")
);
}
private static String dropDots(String str) {
@@ -178,6 +188,7 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
return controllerPackage;
}
/**
* Configures the type of generator.
*
@@ -225,6 +236,11 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
return underscore(toApiName(name));
}
@Override
public String toApiTestFilename(String name) {
return "test_" + toApiFilename(name);
}
/**
* Escapes a reserved word as defined in the `reservedWords` array. Handle escaping
* those terms here. This logic is only called if a variable matches the reseved words
@@ -275,7 +291,6 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
return type;
}
@Override
public void preprocessSwagger(Swagger swagger) {
// need vendor extensions for x-swagger-router-controller
@@ -513,6 +528,93 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
return null;
}
@Override
public void setParameterExampleValue(CodegenParameter p) {
String example;
if (p.defaultValue == null) {
example = p.example;
} else {
example = p.defaultValue;
}
String type = p.baseType;
if (type == null) {
type = p.dataType;
}
if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
if (example == null) {
example = p.paramName + "_example";
}
example = "'" + escapeText(example) + "'";
} else if ("Integer".equals(type) || "int".equals(type)) {
if(p.minimum != null) {
example = "" + (p.minimum.intValue() + 1);
}
if(p.maximum != null) {
example = "" + p.maximum.intValue();
} else if (example == null) {
example = "56";
}
} else if ("Long".equalsIgnoreCase(type)) {
if(p.minimum != null) {
example = "" + (p.minimum.longValue() + 1);
}
if(p.maximum != null) {
example = "" + p.maximum.longValue();
} else if (example == null) {
example = "789";
}
} else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) {
if(p.minimum != null) {
example = "" + p.minimum;
} else if(p.maximum != null) {
example = "" + p.maximum;
} else if (example == null) {
example = "3.4";
}
} else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) {
if (example == null) {
example = "True";
}
} else if ("file".equalsIgnoreCase(type)) {
example = "(BytesIO(b'some file data'), 'file.txt')";
} else if ("Date".equalsIgnoreCase(type)) {
if (example == null) {
example = "2013-10-20";
}
example = "'" + escapeText(example) + "'";
} else if ("DateTime".equalsIgnoreCase(type)) {
if (example == null) {
example = "2013-10-20T19:20:30+01:00";
}
example = "'" + escapeText(example) + "'";
} else if (!languageSpecificPrimitives.contains(type)) {
// type is a model class, e.g. User
example = type + "()";
} else {
LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue");
}
if(p.items != null && p.items.defaultValue != null) {
example = p.items.defaultValue;
}
if (example == null) {
example = "None";
} else if (Boolean.TRUE.equals(p.isListContainer)) {
if (Boolean.TRUE.equals(p.isBodyParam)) {
example = "[" + example + "]";
}
} else if (Boolean.TRUE.equals(p.isMapContainer)) {
example = "{'key': " + example + "}";
}
p.example = example;
}
@Override
public String escapeQuotationMark(String input) {
// remove ' to avoid code injection