Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/flaskConnexion-python2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fi
# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
#ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l python-flask -o samples/server/petstore/flaskConnexion-python2 -DsupportPython2=true"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l python-flask -o samples/server/petstore/flaskConnexion-python2 -c bin/supportPython2.json"
ags="$@ generate -t modules/swagger-codegen/src/main/resources/flaskConnexion -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l python-flask -o samples/server/petstore/flaskConnexion-python2 -c bin/supportPython2.json"

rm -rf samples/server/petstore/flaskConnexion-python2/*
java $JAVA_OPTS -Dservice -jar $executable $ags
3 changes: 2 additions & 1 deletion bin/flaskConnexion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l python-flask -o samples/server/petstore/flaskConnexion "
ags="$@ generate -t modules/swagger-codegen/src/main/resources/flaskConnexion -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l python-flask -o samples/server/petstore/flaskConnexion "

rm -rf samples/server/petstore/flaskConnexion/*
java $JAVA_OPTS -Dservice -jar $executable $ags
Original file line number Diff line number Diff line change
Expand Up @@ -2234,6 +2234,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
collectionFormat = "csv";
}
CodegenProperty pr = fromProperty("inner", inner);
p.items = pr;
p.baseType = pr.datatype;
p.isContainer = true;
p.isListContainer = true;
Expand All @@ -2247,6 +2248,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
property = new MapProperty(inner);
collectionFormat = qp.getCollectionFormat();
CodegenProperty pr = fromProperty("inner", inner);
p.items = pr;
p.baseType = pr.datatype;
p.isContainer = true;
p.isMapContainer = true;
Expand Down Expand Up @@ -2360,6 +2362,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
imports.add(cp.complexType);
}
imports.add(cp.baseType);
p.items = cp;
p.dataType = cp.datatype;
p.baseType = cp.complexType;
p.isPrimitiveType = cp.isPrimitiveType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

import config.ConfigParser;
import io.swagger.codegen.*;
import io.swagger.models.HttpMethod;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Swagger;
import io.swagger.models.properties.*;
import io.swagger.util.Yaml;

import java.io.File;
import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,20 +40,22 @@ public FlaskConnexionCodegen() {
languageSpecificPrimitives.clear();
languageSpecificPrimitives.add("int");
languageSpecificPrimitives.add("float");
languageSpecificPrimitives.add("list");
languageSpecificPrimitives.add("List");
languageSpecificPrimitives.add("Dict");
languageSpecificPrimitives.add("bool");
languageSpecificPrimitives.add("str");
languageSpecificPrimitives.add("datetime");
languageSpecificPrimitives.add("date");
languageSpecificPrimitives.add("file");

typeMapping.clear();
typeMapping.put("integer", "int");
typeMapping.put("float", "float");
typeMapping.put("number", "float");
typeMapping.put("long", "int");
typeMapping.put("double", "float");
typeMapping.put("array", "list");
typeMapping.put("map", "dict");
typeMapping.put("array", "List");
typeMapping.put("map", "Dict");
typeMapping.put("boolean", "bool");
typeMapping.put("string", "str");
typeMapping.put("date", "date");
Expand All @@ -63,9 +66,8 @@ public FlaskConnexionCodegen() {
// set the output folder here
outputFolder = "generated-code/connexion";

modelTemplateFiles.clear();

apiTemplateFiles.put("controller.mustache", ".py");
modelTemplateFiles.put("model.mustache", ".py");

/*
* Template Location. This is the location which templates will be read from. The generator
Expand Down Expand Up @@ -102,11 +104,15 @@ public FlaskConnexionCodegen() {
"",
"app.py")
);
supportingFiles.add(new SupportingFile("util.mustache",
"",
"util.py")
);
supportingFiles.add(new SupportingFile("README.mustache",
"",
"README.md")
);
supportingFiles.add(new SupportingFile("__init__.mustache",
supportingFiles.add(new SupportingFile("__init__controller.mustache",
"",
"__init__.py")
);
Expand Down Expand Up @@ -142,14 +148,29 @@ public void processOpts() {

if (Boolean.TRUE.equals(additionalProperties.get(SUPPORT_PYTHON2))) {
additionalProperties.put(SUPPORT_PYTHON2, Boolean.TRUE);
typeMapping.put("long", "long");
}

if(!new java.io.File(controllerPackage + File.separator + defaultController + ".py").exists()) {
supportingFiles.add(new SupportingFile("__init__.mustache",
supportingFiles.add(new SupportingFile("__init__controller.mustache",
controllerPackage,
"__init__.py")
);
}

supportingFiles.add(new SupportingFile("__init__model.mustache",
modelPackage,
"__init__.py")
);

supportingFiles.add(new SupportingFile("base_model_.mustache",
modelPackage,
"base_model_.py")
);
}

private static String dropDots(String str) {
return str.replaceAll("\\.", "_");
}

@Override
Expand Down Expand Up @@ -224,6 +245,36 @@ public String apiFileFolder() {
return outputFolder + File.separator + apiPackage().replace('.', File.separatorChar);
}

@Override
public String getTypeDeclaration(Property p) {
if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Property inner = ap.getItems();
return getSwaggerType(p) + "[" + getTypeDeclaration(inner) + "]";
} else if (p instanceof MapProperty) {
MapProperty mp = (MapProperty) p;
Property inner = mp.getAdditionalProperties();

return getSwaggerType(p) + "[str, " + getTypeDeclaration(inner) + "]";
}
return super.getTypeDeclaration(p);
}

@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
String type = null;
if (typeMapping.containsKey(swaggerType)) {
type = typeMapping.get(swaggerType);
if (languageSpecificPrimitives.contains(type)) {
return type;
}
} else {
type = toModelName(swaggerType);
}
return type;
}


@Override
public void preprocessSwagger(Swagger swagger) {
Expand Down Expand Up @@ -333,6 +384,96 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
return super.postProcessSupportingFileData(objs);
}

@Override
public String toVarName(String name) {
// sanitize name
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.

// remove dollar sign
name = name.replaceAll("$", "");

// if it's all uppper case, convert to lower case
if (name.matches("^[A-Z_]*$")) {
name = name.toLowerCase();
}

// underscore the variable name
// petId => pet_id
name = underscore(name);

// remove leading underscore
name = name.replaceAll("^_*", "");

// for reserved word or word starting with number, append _
if (isReservedWord(name) || name.matches("^\\d.*")) {
name = escapeReservedWord(name);
}

return name;
}

@Override
public String toModelFilename(String name) {
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
// remove dollar sign
name = name.replaceAll("$", "");

// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name)) {
LOGGER.warn(name + " (reserved word) cannot be used as model filename. Renamed to " + underscore(dropDots("model_" + name)));
name = "model_" + name; // e.g. return => ModelReturn (after camelize)
}

// model name starts with number
if (name.matches("^\\d.*")) {
LOGGER.warn(name + " (model name starts with number) cannot be used as model name. Renamed to " + underscore("model_" + name));
name = "model_" + name; // e.g. 200Response => Model200Response (after camelize)
}

if (!StringUtils.isEmpty(modelNamePrefix)) {
name = modelNamePrefix + "_" + name;
}

if (!StringUtils.isEmpty(modelNameSuffix)) {
name = name + "_" + modelNameSuffix;
}

// underscore the model file name
// PhoneNumber => phone_number
return underscore(dropDots(name));
}

@Override
public String toModelName(String name) {
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
// remove dollar sign
name = name.replaceAll("$", "");

// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name)) {
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + camelize("model_" + name));
name = "model_" + name; // e.g. return => ModelReturn (after camelize)
}

// model name starts with number
if (name.matches("^\\d.*")) {
LOGGER.warn(name + " (model name starts with number) cannot be used as model name. Renamed to " + camelize("model_" + name));
name = "model_" + name; // e.g. 200Response => Model200Response (after camelize)
}

if (!StringUtils.isEmpty(modelNamePrefix)) {
name = modelNamePrefix + "_" + name;
}

if (!StringUtils.isEmpty(modelNameSuffix)) {
name = name + "_" + modelNameSuffix;
}

// camelize the model name
// phone_number => PhoneNumber
return camelize(name);
}

@Override
public String toOperationId(String operationId) {
operationId = super.toOperationId(operationId); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
Expand All @@ -344,6 +485,56 @@ public String toOperationId(String operationId) {
return underscore(operationId);
}

/**
* Return the default value of the property
*
* @param p Swagger property object
* @return string presentation of the default value of the property
*/
@Override
public String toDefaultValue(Property p) {
if (p instanceof StringProperty) {
StringProperty dp = (StringProperty) p;
if (dp.getDefault() != null) {
return "'" + dp.getDefault() + "'";
}
} else if (p instanceof BooleanProperty) {
BooleanProperty dp = (BooleanProperty) p;
if (dp.getDefault() != null) {
if (dp.getDefault().toString().equalsIgnoreCase("false"))
return "False";
else
return "True";
}
} else if (p instanceof DateProperty) {
// TODO
} else if (p instanceof DateTimeProperty) {
// TODO
} else if (p instanceof DoubleProperty) {
DoubleProperty dp = (DoubleProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof FloatProperty) {
FloatProperty dp = (FloatProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof IntegerProperty) {
IntegerProperty dp = (IntegerProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof LongProperty) {
LongProperty dp = (LongProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
}

return null;
}

@Override
public String escapeQuotationMark(String input) {
// remove ' to avoid code injection
Expand All @@ -355,4 +546,14 @@ public String escapeUnsafeCharacters(String input) {
// remove multiline comment
return input.replace("'''", "'_'_'");
}

@Override
public String toModelImport(String name) {
String modelImport = "from ";
if (!"".equals(modelPackage())) {
modelImport += modelPackage() + ".";
}
modelImport += toModelFilename(name)+ " import " + name;
return modelImport;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ To run the server, please execute the following:

```
{{#supportPython2}}
sudo pip install -U connexion # install Connexion from PyPI
sudo pip install -U connexion typing # install Connexion and Typing from PyPI
python app.py
{{/supportPython2}}
{{^supportPython2}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# coding: utf-8

from __future__ import absolute_import
# import models into model package
{{#models}}{{#model}}from .{{classFilename}} import {{classname}}{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,29 @@
{{/supportPython2}}

import connexion
from connexion.decorators import produces
from six import iteritems
from {{modelPackage}}.base_model_ import Model


class JSONEncoder(produces.JSONEncoder):
include_nulls = False

def default(self, o):
if isinstance(o, Model):
dikt = {}
for attr, _ in iteritems(o.swagger_types):
value = getattr(o, attr)
if value is None and not self.include_nulls:
continue
attr = o.attribute_map[attr]
dikt[attr] = value
return dikt
return produces.JSONEncoder.default(self, o)


if __name__ == '__main__':
app = connexion.App(__name__, specification_dir='./swagger/')
app.app.json_encoder = JSONEncoder
app.add_api('swagger.yaml', arguments={'title': '{{appDescription}}'})
app.run(port={{serverPort}})
Loading