forked from loafle/openapi-generator-original
Merge branch 'develop_2.0' of github.com:swagger-api/swagger-codegen into develop_2.0
This commit is contained in:
commit
33834d6cce
@ -4,6 +4,7 @@ import com.wordnik.swagger.models.*;
|
||||
import com.wordnik.swagger.models.parameters.*;
|
||||
import com.wordnik.swagger.models.properties.*;
|
||||
import com.wordnik.swagger.util.Json;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
@ -578,10 +579,12 @@ public class DefaultCodegen {
|
||||
}
|
||||
|
||||
if(operation.getResponses() != null) {
|
||||
for(String responseCode: operation.getResponses().keySet()) {
|
||||
for(String responseCode: new TreeSet<String>(operation.getResponses().keySet())) {
|
||||
Response response = operation.getResponses().get(responseCode);
|
||||
if("200".equals(responseCode)) {
|
||||
if (responseCode.startsWith("2")) {
|
||||
// use the first, i.e. the smallest 2xx response status as methodResponse
|
||||
methodResponse = response;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(methodResponse == null && operation.getResponses().keySet().contains("default")) {
|
||||
|
117
src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java
Executable file
117
src/main/java/com/wordnik/swagger/codegen/languages/PythonClientCodegen.java
Executable file
@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Copyright 2014 Wordnik, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.wordnik.swagger.codegen.languages;
|
||||
|
||||
import com.wordnik.swagger.codegen.*;
|
||||
import com.wordnik.swagger.models.properties.*;
|
||||
|
||||
public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
public String getName() {
|
||||
return "python";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a Python client library.";
|
||||
}
|
||||
|
||||
public PythonClientCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/python";
|
||||
modelTemplateFiles.put("model.mustache", ".py");
|
||||
apiTemplateFiles.put("api.mustache", ".py");
|
||||
templateDir = "python";
|
||||
|
||||
apiPackage = "";
|
||||
modelPackage = "models";
|
||||
|
||||
languageSpecificPrimitives.clear();
|
||||
languageSpecificPrimitives.add("int");
|
||||
languageSpecificPrimitives.add("float");
|
||||
languageSpecificPrimitives.add("long");
|
||||
languageSpecificPrimitives.add("list");
|
||||
languageSpecificPrimitives.add("bool");
|
||||
languageSpecificPrimitives.add("str");
|
||||
languageSpecificPrimitives.add("datetime");
|
||||
|
||||
typeMapping.clear();
|
||||
typeMapping.put("integer", "int");
|
||||
typeMapping.put("float", "float");
|
||||
typeMapping.put("long", "long");
|
||||
typeMapping.put("double", "float");
|
||||
typeMapping.put("array", "list");
|
||||
typeMapping.put("map", "map");
|
||||
typeMapping.put("boolean", "bool");
|
||||
typeMapping.put("string", "str");
|
||||
typeMapping.put("date", "datetime");
|
||||
|
||||
|
||||
supportingFiles.add(new SupportingFile("swagger.mustache", "", "swagger.py"));
|
||||
supportingFiles.add(new SupportingFile("__init__.mustache", "", "__init__.py"));
|
||||
supportingFiles.add(new SupportingFile("__init__.mustache", modelPackage, "__init__.py"));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String escapeReservedWord(String name) {
|
||||
return "_" + name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apiFileFolder() {
|
||||
return outputFolder + "/" + apiPackage().replaceAll("\\.", "/");
|
||||
}
|
||||
|
||||
public String modelFileFolder() {
|
||||
return outputFolder + "/" + modelPackage().replaceAll("\\.", "/");
|
||||
}
|
||||
|
||||
@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) + "(String, " + 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 = swaggerType;
|
||||
return type;
|
||||
}
|
||||
|
||||
public String toDefaultValue(Property p) {
|
||||
// TODO: Support Python def value
|
||||
return "null";
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ package {{package}};
|
||||
{{/imports}}
|
||||
|
||||
import com.wordnik.swagger.annotations.*;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
{{#models}}
|
||||
|
||||
{{#model}}{{#description}}
|
||||
@ -24,6 +25,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} { {{#vars}
|
||||
* maximum: {{maximum}}{{/maximum}}
|
||||
**/
|
||||
@ApiModelProperty(required = {{required}}, value = "{{{description}}}")
|
||||
@JsonProperty("{{name}}")
|
||||
public {{{datatype}}} {{getter}}() {
|
||||
return {{name}};
|
||||
}
|
||||
@ -44,4 +46,4 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} { {{#vars}
|
||||
}
|
||||
}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
{{/models}}
|
||||
|
@ -9,4 +9,5 @@ com.wordnik.swagger.codegen.languages.StaticDocCodegen
|
||||
com.wordnik.swagger.codegen.languages.StaticHtmlGenerator
|
||||
com.wordnik.swagger.codegen.languages.SwaggerGenerator
|
||||
com.wordnik.swagger.codegen.languages.TizenClientCodegen
|
||||
com.wordnik.swagger.codegen.languages.PhpClientCodegen
|
||||
com.wordnik.swagger.codegen.languages.PhpClientCodegen
|
||||
com.wordnik.swagger.codegen.languages.PythonClientCodegen
|
@ -239,4 +239,30 @@ class JavaModelTest extends FlatSpec with Matchers {
|
||||
cm.imports.size should be (3)
|
||||
(cm.imports.asScala.toSet & Set("Map", "HashMap", "Children")).size should be (3)
|
||||
}
|
||||
}
|
||||
|
||||
it should "convert a model with upper-case property names" in {
|
||||
val model = new ModelImpl()
|
||||
.description("a model with upper-case property names")
|
||||
.property("NAME", new StringProperty())
|
||||
.required("NAME")
|
||||
|
||||
val codegen = new JavaClientCodegen()
|
||||
val cm = codegen.fromModel("sample", model)
|
||||
|
||||
cm.name should be ("sample")
|
||||
cm.classname should be ("Sample")
|
||||
cm.vars.size should be (1)
|
||||
|
||||
val vars = cm.vars
|
||||
vars.get(0).baseName should be ("NAME")
|
||||
vars.get(0).getter should be ("getNAME")
|
||||
vars.get(0).setter should be ("setNAME")
|
||||
vars.get(0).datatype should be ("String")
|
||||
vars.get(0).name should be ("NAME")
|
||||
vars.get(0).defaultValue should be ("null")
|
||||
vars.get(0).baseType should be ("String")
|
||||
vars.get(0).hasMore should equal (null)
|
||||
vars.get(0).required should equal (true)
|
||||
vars.get(0).isNotContainer should equal (true)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user