Introduced a factory for codegen data containers

Moved parameter/response generation code to
dedicated methods so that subclasses may extend it
This commit is contained in:
Martin Ehrnhoefer 2014-09-30 11:12:11 +02:00
parent 7a5068d632
commit 78b36e0ba9

View File

@ -588,14 +588,7 @@ public class DefaultCodegen {
for(String responseCode: operation.getResponses().keySet()) {
Response response = operation.getResponses().get(responseCode);
if(response != methodResponse) {
CodegenResponse r = CodegenModelFactory.newInstance(CodegenModelType.RESPONSE);
if("default".equals(responseCode))
r.code = "0";
else
r.code = responseCode;
r.message = response.getDescription();
r.schema = response.getSchema();
r.examples = toExamples(response.getExamples());
CodegenResponse r = fromResponse(responseCode, response);
op.responses.add(r);
}
for(int i = 0; i < op.responses.size() - 1; i++) {
@ -658,92 +651,7 @@ public class DefaultCodegen {
if(parameters != null) {
for(Parameter param : parameters) {
CodegenParameter p = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
p.baseName = param.getName();
p.description = param.getDescription();
p.required = param.getRequired();
if(param instanceof SerializableParameter) {
SerializableParameter qp = (SerializableParameter) param;
Property property = null;
String collectionFormat = null;
if("array".equals(qp.getType())) {
Property inner = qp.getItems();
property = new ArrayProperty(inner);
collectionFormat = qp.getCollectionFormat();
CodegenProperty pr = fromProperty("inner", inner);
p.baseType = pr.datatype;
imports.add(pr.baseType);
}
else
property = PropertyBuilder.build(qp.getType(), qp.getFormat(), null);
if(property == null) {
System.out.println("couldn't find property for parameter: ");
Json.prettyPrint(param);
}
CodegenProperty model = fromProperty(qp.getName(), property);
p.collectionFormat = collectionFormat;
p.dataType = model.datatype;
p.paramName = toParamName(qp.getName());
if(model.complexType != null) {
imports.add(model.complexType);
}
}
else {
BodyParameter bp = (BodyParameter) param;
Model model = bp.getSchema();
if(model instanceof ModelImpl) {
ModelImpl impl = (ModelImpl) model;
CodegenModel cm = fromModel(bp.getName(), impl);
if(cm.emptyVars != null && cm.emptyVars == false) {
p.dataType = getTypeDeclaration(cm.classname);
imports.add(p.dataType);
}
else {
// TODO: missing format, so this will not always work
Property prop = PropertyBuilder.build(impl.getType(), null, null);
CodegenProperty cp = fromProperty("property", prop);
if(cp != null) {
p.dataType = cp.datatype;
}
}
}
else if(model instanceof ArrayModel) {
// to use the built-in model parsing, we unwrap the ArrayModel
// and get a single property from it
ArrayModel impl = (ArrayModel) model;
CodegenModel cm = fromModel(bp.getName(), impl);
// get the single property
ArrayProperty ap = new ArrayProperty().items(impl.getItems());
CodegenProperty cp = fromProperty("inner", ap);
if(cp.complexType != null) {
imports.add(cp.complexType);
}
imports.add(cp.baseType);
p.dataType = cp.datatype;
p.isContainer = true;
}
else{
Model sub = bp.getSchema();
if(sub instanceof RefModel) {
String name = ((RefModel)sub).getSimpleRef();
if(typeMapping.containsKey(name))
name = typeMapping.get(name);
else {
name = toModelName(name);
if(defaultIncludes.contains(name)) {
imports.add(name);
}
imports.add(name);
name = getTypeDeclaration(name);
}
p.dataType = name;
}
}
p.paramName = toParamName(bp.getName());
}
CodegenParameter p = fromParameter(param, imports);
allParams.add(p);
if(param instanceof QueryParameter) {
queryParams.add(p);
@ -790,10 +698,113 @@ public class DefaultCodegen {
if(op.allParams.size() > 0)
op.hasParams = true;
op.externalDocs = operation.getExternalDocs();
op.externalDocs = operation.getExternalDocs();
return op;
}
public CodegenResponse fromResponse(String responseCode, Response response) {
CodegenResponse r = CodegenModelFactory.newInstance(CodegenModelType.RESPONSE);
if("default".equals(responseCode))
r.code = "0";
else
r.code = responseCode;
r.message = response.getDescription();
r.schema = response.getSchema();
r.examples = toExamples(response.getExamples());
return r;
}
public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
CodegenParameter p = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
p.baseName = param.getName();
p.description = param.getDescription();
p.required = param.getRequired();
if(param instanceof SerializableParameter) {
SerializableParameter qp = (SerializableParameter) param;
Property property = null;
String collectionFormat = null;
if("array".equals(qp.getType())) {
Property inner = qp.getItems();
property = new ArrayProperty(inner);
collectionFormat = qp.getCollectionFormat();
CodegenProperty pr = fromProperty("inner", inner);
p.baseType = pr.datatype;
imports.add(pr.baseType);
}
else
property = PropertyBuilder.build(qp.getType(), qp.getFormat(), null);
if(property == null) {
System.out.println("couldn't find property for parameter: ");
Json.prettyPrint(param);
}
CodegenProperty model = fromProperty(qp.getName(), property);
p.collectionFormat = collectionFormat;
p.dataType = model.datatype;
p.paramName = toParamName(qp.getName());
if(model.complexType != null) {
imports.add(model.complexType);
}
}
else {
BodyParameter bp = (BodyParameter) param;
Model model = bp.getSchema();
if(model instanceof ModelImpl) {
ModelImpl impl = (ModelImpl) model;
CodegenModel cm = fromModel(bp.getName(), impl);
if(cm.emptyVars != null && cm.emptyVars == false) {
p.dataType = getTypeDeclaration(cm.classname);
imports.add(p.dataType);
}
else {
// TODO: missing format, so this will not always work
Property prop = PropertyBuilder.build(impl.getType(), null, null);
CodegenProperty cp = fromProperty("property", prop);
if(cp != null) {
p.dataType = cp.datatype;
}
}
}
else if(model instanceof ArrayModel) {
// to use the built-in model parsing, we unwrap the ArrayModel
// and get a single property from it
ArrayModel impl = (ArrayModel) model;
CodegenModel cm = fromModel(bp.getName(), impl);
// get the single property
ArrayProperty ap = new ArrayProperty().items(impl.getItems());
CodegenProperty cp = fromProperty("inner", ap);
if(cp.complexType != null) {
imports.add(cp.complexType);
}
imports.add(cp.baseType);
p.dataType = cp.datatype;
p.isContainer = true;
}
else{
Model sub = bp.getSchema();
if(sub instanceof RefModel) {
String name = ((RefModel)sub).getSimpleRef();
if(typeMapping.containsKey(name))
name = typeMapping.get(name);
else {
name = toModelName(name);
if(defaultIncludes.contains(name)) {
imports.add(name);
}
imports.add(name);
name = getTypeDeclaration(name);
}
p.dataType = name;
}
}
p.paramName = toParamName(bp.getName());
}
return p;
}
protected List<Map<String, String>> toExamples(Map<String, String> examples) {
if(examples == null)
return null;