Copy properties of parent model to model with allOf

Also fix #936
This commit is contained in:
xhh 2015-07-02 17:48:58 +08:00
parent aba755d3de
commit b61a400d91
3 changed files with 28 additions and 8 deletions

View File

@ -65,6 +65,8 @@ public interface CodegenConfig {
CodegenModel fromModel(String name, Model model);
CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions);
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions);
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);

View File

@ -506,6 +506,10 @@ public class DefaultCodegen {
}
public CodegenModel fromModel(String name, Model model) {
return fromModel(name, model, null);
}
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
CodegenModel m = CodegenModelFactory.newInstance(CodegenModelType.MODEL);
if (reservedWords.contains(name)) {
m.name = escapeReservedWord(name);
@ -526,11 +530,25 @@ public class DefaultCodegen {
} else if (model instanceof ComposedModel) {
final ComposedModel composed = (ComposedModel) model;
final RefModel parent = (RefModel) composed.getParent();
final String parentModel = toModelName(parent.getSimpleRef());
m.parent = parentModel;
addImport(m, parentModel);
final ModelImpl child = (ModelImpl) composed.getChild();
addVars(m, child.getProperties(), child.getRequired());
if (parent != null) {
final String parentRef = toModelName(parent.getSimpleRef());
final Model parentModel = allDefinitions.get(parentRef);
if (parentModel instanceof ModelImpl) {
final ModelImpl _parent = (ModelImpl) parentModel;
m.parent = parentRef;
addImport(m, parentRef);
addVars(m, _parent.getProperties(), _parent.getRequired());
}
}
Model child = composed.getChild();
if (child != null && child instanceof RefModel) {
final String childRef = ((RefModel) child).getSimpleRef();
child = allDefinitions.get(childRef);
}
if (child != null && child instanceof ModelImpl) {
final ModelImpl _child = (ModelImpl) child;
addVars(m, _child.getProperties(), _child.getRequired());
}
} else {
ModelImpl impl = (ModelImpl) model;
if (impl.getAdditionalProperties() != null) {

View File

@ -122,7 +122,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
Model model = definitions.get(name);
Map<String, Model> modelMap = new HashMap<String, Model>();
modelMap.put(name, model);
Map<String, Object> models = processModels(config, modelMap);
Map<String, Object> models = processModels(config, modelMap, definitions);
models.putAll(config.additionalProperties());
allModels.add(((List<Object>) models.get("models")).get(0));
@ -442,14 +442,14 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
return operations;
}
public Map<String, Object> processModels(CodegenConfig config, Map<String, Model> definitions) {
public Map<String, Object> processModels(CodegenConfig config, Map<String, Model> definitions, Map<String, Model> allDefinitions) {
Map<String, Object> objs = new HashMap<String, Object>();
objs.put("package", config.modelPackage());
List<Object> models = new ArrayList<Object>();
Set<String> allImports = new LinkedHashSet<String>();
for (String key : definitions.keySet()) {
Model mm = definitions.get(key);
CodegenModel cm = config.fromModel(key, mm);
CodegenModel cm = config.fromModel(key, mm, allDefinitions);
Map<String, Object> mo = new HashMap<String, Object>();
mo.put("model", cm);
mo.put("importPath", config.toModelImport(key));