add example to codegen parameter

This commit is contained in:
wing328
2016-03-06 01:24:41 +08:00
parent 7d642b28b9
commit e5ed295a78
6 changed files with 137 additions and 6 deletions

View File

@@ -10,6 +10,7 @@ public class CodegenParameter {
isCookieParam, isBodyParam, hasMore, isContainer,
secondaryParam, isCollectionFormatMulti;
public String baseName, paramName, dataType, datatypeWithEnum, collectionFormat, description, baseType, defaultValue;
public String example; // example value (x-example)
public String jsonSchema;
public Boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime;
public Boolean isListContainer, isMapContainer;
@@ -107,6 +108,7 @@ public class CodegenParameter {
output.multipleOf = this.multipleOf;
output.jsonSchema = this.jsonSchema;
output.defaultValue = this.defaultValue;
output.example = this.example;
output.isEnum = this.isEnum;
if (this._enum != null) {
output._enum = new ArrayList<String>(this._enum);

View File

@@ -652,13 +652,22 @@ public class DefaultCodegen {
}
}
/**
* Return the example value of the parameter.
*
* @param p Swagger property object
* @return string presentation of the example value of the property
*/
public void setParameterExampleValue(CodegenParameter p) {
}
/**
* Return the example value of the property
*
* @param p Swagger property object
* @return string presentation of the example value of the property
*/
@SuppressWarnings("static-method")
public String toExampleValue(Property p) {
if(p.getExample() != null) {
return p.getExample().toString();
@@ -1806,6 +1815,36 @@ public class DefaultCodegen {
p.paramName = toParamName(bp.getName());
}
// set the example value
// if not specified in x-example, generate a default value
if (p.vendorExtensions.containsKey("x-example")) {
p.example = (String) p.vendorExtensions.get("x-example");
} else if (Boolean.TRUE.equals(p.isString)) {
p.example = p.paramName + "_example";
} else if (Boolean.TRUE.equals(p.isBoolean)) {
p.example = new String("true");
} else if (Boolean.TRUE.equals(p.isLong)) {
p.example = new String("789");
} else if (Boolean.TRUE.equals(p.isInteger)) {
p.example = new String("56");
} else if (Boolean.TRUE.equals(p.isFloat)) {
p.example = new String("3.4");
} else if (Boolean.TRUE.equals(p.isDouble)) {
p.example = new String("1.2");
} else if (Boolean.TRUE.equals(p.isBinary)) {
p.example = new String("BINARY_DATA_HERE");
} else if (Boolean.TRUE.equals(p.isByteArray)) {
p.example = new String("B");
} else if (Boolean.TRUE.equals(p.isDate)) {
p.example = new String("2013-10-20");
} else if (Boolean.TRUE.equals(p.isDateTime)) {
p.example = new String("2013-10-20T19:20:30+01:00");
}
// set the parameter excample value
// should be overridden by exmaple value
setParameterExampleValue(p);
postProcessParameter(p);
return p;
}

View File

@@ -1,6 +1,7 @@
package io.swagger.codegen.languages;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;
@@ -9,6 +10,17 @@ import io.swagger.codegen.CliOption;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.StringProperty;
import io.swagger.models.properties.LongProperty;
import io.swagger.models.properties.IntegerProperty;
import io.swagger.models.properties.FloatProperty;
import io.swagger.models.properties.DoubleProperty;
import io.swagger.models.properties.BooleanProperty;
import io.swagger.models.properties.BinaryProperty;
import io.swagger.models.properties.ByteArrayProperty;
import io.swagger.models.properties.DateTimeProperty;
import io.swagger.models.properties.DateProperty;
import java.io.File;
import java.util.Arrays;
@@ -203,6 +215,42 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
@Override
public String toDefaultValue(Property p) {
if (p instanceof StringProperty) {
StringProperty dp = (StringProperty) p;
if (dp.getDefault() != null) {
return "'" + dp.getDefault().toString() + "'";
}
} else if (p instanceof BooleanProperty) {
BooleanProperty dp = (BooleanProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} 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";
}
@@ -324,4 +372,19 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
public void setModuleVersion(String moduleVersion) {
this.moduleVersion = moduleVersion;
}
@Override
public void setParameterExampleValue(CodegenParameter p) {
if (Boolean.TRUE.equals(p.isString) || Boolean.TRUE.equals(p.isBinary) || Boolean.TRUE.equals(p.isByteArray)) {
p.example = "'" + p.example + "'";
} else if (Boolean.TRUE.equals(p.isBoolean)) {
if (Boolean.parseBoolean(p.example))
p.example = new String("1");
else
p.example = new String("0");
} else if (Boolean.TRUE.equals(p.isDateTime) || Boolean.TRUE.equals(p.isDate)) {
p.example = "DateTime->from_epoch(epoch => str2time('" + p.example + "'))";
}
}
}