Fix for issue#5788 -CPPRest generated models are not using inheritance as specified in contract (#5791)

* fixed map to use value instead of mapentry while doing fromJson.

* cpprest models now use inheritance properly instead of always extending from ModelBase

* cpprest models now use inheritance properly instead of always extending from ModelBase

* removed a sysout used for debugging

* toJson() and fromJson() now leverages parent class's corresponding methods

* virtual is not needed as override essentially does the same thing.

* added docstring for getModelByName

* corrected the javadoc

* fixed @param issue in javadoc

* fixed @param uncapitalized P in param in javadoc
This commit is contained in:
harishchoragudi
2017-06-08 12:41:20 -05:00
committed by wing328
parent 73f5266596
commit 2d008be181
6 changed files with 109 additions and 33 deletions

View File

@@ -1,7 +1,10 @@
package io.swagger.codegen.languages;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import io.swagger.codegen.*;
import io.swagger.codegen.examples.ExampleGenerator;
import io.swagger.codegen.utils.ModelUtils;
import io.swagger.models.Model;
import io.swagger.models.Operation;
import io.swagger.models.Response;
@@ -11,6 +14,8 @@ import io.swagger.models.properties.*;
import java.util.*;
import java.io.File;
import static com.google.common.base.Strings.isNullOrEmpty;
public class CppRestClientCodegen extends DefaultCodegen implements CodegenConfig {
public static final String DECLSPEC = "declspec";
@@ -20,6 +25,9 @@ public class CppRestClientCodegen extends DefaultCodegen implements CodegenConfi
protected String declspec = "";
protected String defaultInclude = "";
private final Set<String> parentModels = new HashSet<>();
private final Multimap<String, CodegenModel> childrenByParent = ArrayListMultimap.create();
/**
* Configures the type of generator.
*
@@ -236,6 +244,13 @@ public class CppRestClientCodegen extends DefaultCodegen implements CodegenConfi
if (isFileProperty(property)) {
property.vendorExtensions.put("x-codegen-file", true);
}
if (!isNullOrEmpty(model.parent)) {
parentModels.add(model.parent);
if (!childrenByParent.containsEntry(model.parent, model)) {
childrenByParent.put(model.parent, model);
}
}
}
protected boolean isFileProperty(CodegenProperty property) {
@@ -397,4 +412,38 @@ public class CppRestClientCodegen extends DefaultCodegen implements CodegenConfi
return input.replace("*/", "*_/").replace("/*", "/_*");
}
@Override
public Map<String, Object> postProcessAllModels(final Map<String, Object> models) {
final Map<String, Object> processed = super.postProcessAllModels(models);
postProcessParentModels(models);
return processed;
}
private void postProcessParentModels(final Map<String, Object> models) {
for (final String parent : parentModels) {
final CodegenModel parentModel = ModelUtils.getModelByName(parent, models);
final Collection<CodegenModel> childrenModels = childrenByParent.get(parent);
for (final CodegenModel child : childrenModels) {
processParentPropertiesInChildModel(parentModel, child);
}
}
}
/**
* Sets the child property's isInherited flag to true if it is an inherited property
*/
private void processParentPropertiesInChildModel(final CodegenModel parent, final CodegenModel child) {
final Map<String, CodegenProperty> childPropertiesByName = new HashMap<>(child.vars.size());
for (final CodegenProperty childProperty : child.vars) {
childPropertiesByName.put(childProperty.name, childProperty);
}
for (final CodegenProperty parentProperty : parent.vars) {
final CodegenProperty duplicatedByParent = childPropertiesByName.get(parentProperty.name);
if (duplicatedByParent != null) {
duplicatedByParent.isInherited = true;
}
}
}
}

View File

@@ -12,6 +12,7 @@ import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.SupportingFile;
import io.swagger.codegen.utils.ModelUtils;
import io.swagger.models.Swagger;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.StringProperty;
@@ -228,7 +229,7 @@ public class NancyFXServerCodegen extends AbstractCSharpCodegen {
private void postProcessParentModels(final Map<String, Object> models) {
log.debug("Processing parents: " + parentModels);
for (final String parent : parentModels) {
final CodegenModel parentModel = modelByName(parent, models);
final CodegenModel parentModel = ModelUtils.getModelByName(parent, models);
parentModel.hasChildren = true;
final Collection<CodegenModel> childrenModels = childrenByParent.get(parent);
for (final CodegenModel child : childrenModels) {
@@ -237,27 +238,6 @@ public class NancyFXServerCodegen extends AbstractCSharpCodegen {
}
}
private CodegenModel modelByName(final String name, final Map<String, Object> models) {
final Object data = models.get(name);
if (data instanceof Map) {
final Map<?, ?> dataMap = (Map<?, ?>) data;
final Object dataModels = dataMap.get("models");
if (dataModels instanceof List) {
final List<?> dataModelsList = (List<?>) dataModels;
for (final Object entry : dataModelsList) {
if (entry instanceof Map) {
final Map<?, ?> entryMap = (Map<?, ?>) entry;
final Object model = entryMap.get("model");
if (model instanceof CodegenModel) {
return (CodegenModel) model;
}
}
}
}
}
return null;
}
private void processParentPropertiesInChildModel(final CodegenModel parent, final CodegenModel child) {
final Map<String, CodegenProperty> childPropertiesByName = new HashMap<>(child.vars.size());
for (final CodegenProperty property : child.vars) {

View File

@@ -0,0 +1,36 @@
package io.swagger.codegen.utils;
import io.swagger.codegen.CodegenModel;
import java.util.List;
import java.util.Map;
public class ModelUtils {
/**
* Searches for the model by name in the map of models and returns it
*
* @param name Name of the model
* @param models Map of models
* @return model
*/
public static CodegenModel getModelByName(final String name, final Map<String, Object> models) {
final Object data = models.get(name);
if (data instanceof Map) {
final Map<?, ?> dataMap = (Map<?, ?>) data;
final Object dataModels = dataMap.get("models");
if (dataModels instanceof List) {
final List<?> dataModelsList = (List<?>) dataModels;
for (final Object entry : dataModelsList) {
if (entry instanceof Map) {
final Map<?, ?> entryMap = (Map<?, ?>) entry;
final Object model = entryMap.get("model");
if (model instanceof CodegenModel) {
return (CodegenModel) model;
}
}
}
}
}
return null;
}
}