forked from loafle/openapi-generator-original
Merge pull request #1457 from xhh/java-default-value
[Java] Assign default value for primitive properties
This commit is contained in:
commit
4ed819c4a0
@ -12,9 +12,14 @@ import io.swagger.codegen.DefaultCodegen;
|
|||||||
import io.swagger.codegen.SupportingFile;
|
import io.swagger.codegen.SupportingFile;
|
||||||
import io.swagger.models.Model;
|
import io.swagger.models.Model;
|
||||||
import io.swagger.models.properties.ArrayProperty;
|
import io.swagger.models.properties.ArrayProperty;
|
||||||
|
import io.swagger.models.properties.BooleanProperty;
|
||||||
|
import io.swagger.models.properties.DoubleProperty;
|
||||||
|
import io.swagger.models.properties.FloatProperty;
|
||||||
|
import io.swagger.models.properties.IntegerProperty;
|
||||||
import io.swagger.models.properties.LongProperty;
|
import io.swagger.models.properties.LongProperty;
|
||||||
import io.swagger.models.properties.MapProperty;
|
import io.swagger.models.properties.MapProperty;
|
||||||
import io.swagger.models.properties.Property;
|
import io.swagger.models.properties.Property;
|
||||||
|
import io.swagger.models.properties.StringProperty;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -347,12 +352,48 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
pattern = "new HashMap<String, %s>()";
|
pattern = "new HashMap<String, %s>()";
|
||||||
}
|
}
|
||||||
return String.format(pattern, getTypeDeclaration(ap.getAdditionalProperties()));
|
return String.format(pattern, getTypeDeclaration(ap.getAdditionalProperties()));
|
||||||
|
} else if (p instanceof IntegerProperty) {
|
||||||
|
IntegerProperty dp = (IntegerProperty) p;
|
||||||
|
if (dp.getDefault() != null) {
|
||||||
|
return dp.getDefault().toString();
|
||||||
|
}
|
||||||
|
return "null";
|
||||||
} else if (p instanceof LongProperty) {
|
} else if (p instanceof LongProperty) {
|
||||||
LongProperty dp = (LongProperty) p;
|
LongProperty dp = (LongProperty) p;
|
||||||
if (dp.getDefault() != null) {
|
if (dp.getDefault() != null) {
|
||||||
return dp.getDefault().toString()+"l";
|
return dp.getDefault().toString()+"l";
|
||||||
}
|
}
|
||||||
return "null";
|
return "null";
|
||||||
|
} else if (p instanceof DoubleProperty) {
|
||||||
|
DoubleProperty dp = (DoubleProperty) p;
|
||||||
|
if (dp.getDefault() != null) {
|
||||||
|
return dp.getDefault().toString() + "d";
|
||||||
|
}
|
||||||
|
return "null";
|
||||||
|
} else if (p instanceof FloatProperty) {
|
||||||
|
FloatProperty dp = (FloatProperty) p;
|
||||||
|
if (dp.getDefault() != null) {
|
||||||
|
return dp.getDefault().toString() + "f";
|
||||||
|
}
|
||||||
|
return "null";
|
||||||
|
} else if (p instanceof BooleanProperty) {
|
||||||
|
BooleanProperty bp = (BooleanProperty) p;
|
||||||
|
if (bp.getDefault() != null) {
|
||||||
|
return bp.getDefault().toString();
|
||||||
|
}
|
||||||
|
return "null";
|
||||||
|
} else if (p instanceof StringProperty) {
|
||||||
|
StringProperty sp = (StringProperty) p;
|
||||||
|
if (sp.getDefault() != null) {
|
||||||
|
String _default = sp.getDefault();
|
||||||
|
if (sp.getEnum() == null) {
|
||||||
|
return "\"" + escapeText(_default) + "\"";
|
||||||
|
} else {
|
||||||
|
// convert to enum var name later in postProcessModels
|
||||||
|
return _default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "null";
|
||||||
}
|
}
|
||||||
return super.toDefaultValue(p);
|
return super.toDefaultValue(p);
|
||||||
}
|
}
|
||||||
@ -445,6 +486,20 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
enumVars.add(enumVar);
|
enumVars.add(enumVar);
|
||||||
}
|
}
|
||||||
allowableValues.put("enumVars", enumVars);
|
allowableValues.put("enumVars", enumVars);
|
||||||
|
// handle default value for enum, e.g. available => StatusEnum.AVAILABLE
|
||||||
|
if (var.defaultValue != null && !"null".equals(var.defaultValue)) {
|
||||||
|
String enumName = null;
|
||||||
|
for (Map<String, String> enumVar : enumVars) {
|
||||||
|
if (var.defaultValue.equals(enumVar.get("value"))) {
|
||||||
|
enumName = enumVar.get("name");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (enumName == null) {
|
||||||
|
throw new RuntimeException("default value of property \"" + var.baseName + "\" is not in allowed values: " + var.defaultValue);
|
||||||
|
}
|
||||||
|
var.defaultValue = var.datatypeWithEnum + "." + enumName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return objs;
|
return objs;
|
||||||
|
@ -2,7 +2,9 @@ package io.swagger.codegen.languages;
|
|||||||
|
|
||||||
import io.swagger.codegen.CodegenConfig;
|
import io.swagger.codegen.CodegenConfig;
|
||||||
import io.swagger.codegen.CodegenConstants;
|
import io.swagger.codegen.CodegenConstants;
|
||||||
|
import io.swagger.codegen.CodegenModel;
|
||||||
import io.swagger.codegen.CodegenOperation;
|
import io.swagger.codegen.CodegenOperation;
|
||||||
|
import io.swagger.codegen.CodegenProperty;
|
||||||
import io.swagger.codegen.CodegenType;
|
import io.swagger.codegen.CodegenType;
|
||||||
import io.swagger.codegen.SupportingFile;
|
import io.swagger.codegen.SupportingFile;
|
||||||
import io.swagger.models.Operation;
|
import io.swagger.models.Operation;
|
||||||
@ -121,6 +123,22 @@ public class JavaInflectorServerCodegen extends JavaClientCodegen implements Cod
|
|||||||
co.baseName = basePath;
|
co.baseName = basePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||||
|
List<Object> models = (List<Object>) objs.get("models");
|
||||||
|
for (Object _mo : models) {
|
||||||
|
Map<String, Object> mo = (Map<String, Object>) _mo;
|
||||||
|
CodegenModel cm = (CodegenModel) mo.get("model");
|
||||||
|
for (CodegenProperty var : cm.vars) {
|
||||||
|
// handle default value for enum, e.g. available => StatusEnum.available
|
||||||
|
if (var.isEnum && var.defaultValue != null && !"null".equals(var.defaultValue)) {
|
||||||
|
var.defaultValue = var.datatypeWithEnum + "." + var.defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return objs;
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
||||||
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
|
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
|
||||||
if (operations != null) {
|
if (operations != null) {
|
||||||
|
@ -2,11 +2,15 @@ package io.swagger.codegen.languages;
|
|||||||
|
|
||||||
import io.swagger.codegen.CodegenConfig;
|
import io.swagger.codegen.CodegenConfig;
|
||||||
import io.swagger.codegen.CodegenConstants;
|
import io.swagger.codegen.CodegenConstants;
|
||||||
|
import io.swagger.codegen.CodegenModel;
|
||||||
import io.swagger.codegen.CodegenOperation;
|
import io.swagger.codegen.CodegenOperation;
|
||||||
|
import io.swagger.codegen.CodegenProperty;
|
||||||
import io.swagger.codegen.CodegenResponse;
|
import io.swagger.codegen.CodegenResponse;
|
||||||
import io.swagger.codegen.CodegenType;
|
import io.swagger.codegen.CodegenType;
|
||||||
import io.swagger.codegen.SupportingFile;
|
import io.swagger.codegen.SupportingFile;
|
||||||
import io.swagger.models.Operation;
|
import io.swagger.models.Operation;
|
||||||
|
import io.swagger.models.properties.Property;
|
||||||
|
import io.swagger.models.properties.StringProperty;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -114,6 +118,22 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
|
|||||||
co.baseName = basePath;
|
co.baseName = basePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||||
|
List<Object> models = (List<Object>) objs.get("models");
|
||||||
|
for (Object _mo : models) {
|
||||||
|
Map<String, Object> mo = (Map<String, Object>) _mo;
|
||||||
|
CodegenModel cm = (CodegenModel) mo.get("model");
|
||||||
|
for (CodegenProperty var : cm.vars) {
|
||||||
|
// handle default value for enum, e.g. available => StatusEnum.available
|
||||||
|
if (var.isEnum && var.defaultValue != null && !"null".equals(var.defaultValue)) {
|
||||||
|
var.defaultValue = var.datatypeWithEnum + "." + var.defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return objs;
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
||||||
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
|
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
|
||||||
if (operations != null) {
|
if (operations != null) {
|
||||||
|
@ -193,6 +193,17 @@ public class SpringMVCServerCodegen extends JavaClientCodegen implements Codegen
|
|||||||
String _import = iterator.next().get("import");
|
String _import = iterator.next().get("import");
|
||||||
if (_import.endsWith(".Object")) iterator.remove();
|
if (_import.endsWith(".Object")) iterator.remove();
|
||||||
}
|
}
|
||||||
|
List<Object> models = (List<Object>) objs.get("models");
|
||||||
|
for (Object _mo : models) {
|
||||||
|
Map<String, Object> mo = (Map<String, Object>) _mo;
|
||||||
|
CodegenModel cm = (CodegenModel) mo.get("model");
|
||||||
|
for (CodegenProperty var : cm.vars) {
|
||||||
|
// handle default value for enum, e.g. available => StatusEnum.available
|
||||||
|
if (var.isEnum && var.defaultValue != null && !"null".equals(var.defaultValue)) {
|
||||||
|
var.defaultValue = var.datatypeWithEnum + "." + var.defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return objs;
|
return objs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user