Skip to content

Commit

Permalink
Handle imports of referenced models in additional properties
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-rosset committed May 19, 2020
1 parent b05b415 commit f57338d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
public class PythonClientExperimentalCodegen extends PythonClientCodegen {
private static final Logger LOGGER = LoggerFactory.getLogger(PythonClientExperimentalCodegen.class);

private static final String referencedModelNamesExtension = "x-python-referenced-model-names";

public PythonClientExperimentalCodegen() {
super();

Expand Down Expand Up @@ -361,7 +363,9 @@ private void fixModelImports(Set<String> imports) {
}
}

// override with any special post-processing for all models
/**
* Override with special post-processing for all models.
*/
@SuppressWarnings({"static-method", "unchecked"})
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
// loop through all models and delete ones where type!=object and the model has no validations and enums
Expand All @@ -373,6 +377,14 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
for (Map<String, Object> mo : models) {
CodegenModel cm = (CodegenModel) mo.get("model");

// Make sure the models listed in 'additionalPropertiesType' are included in imports.
List<String> refModelNames = (List<String>) cm.vendorExtensions.get(referencedModelNamesExtension);
if (refModelNames != null) {
for (String refModelName : refModelNames) {
cm.imports.add(refModelName);
}
}

// make sure discriminator models are included in imports
CodegenDiscriminator discriminator = cm.discriminator;
if (discriminator != null) {
Expand Down Expand Up @@ -901,9 +913,11 @@ public String getSimpleTypeDeclaration(Schema schema) {
* @param p The OAS schema.
* @param prefix prepended to the returned value.
* @param suffix appended to the returned value.
* @param referencedModelNames a list of models that are being referenced while generating the types,
* may be used to generate imports.
* @return a comma-separated string representation of the Python types
*/
private String getTypeString(Schema p, String prefix, String suffix) {
private String getTypeString(Schema p, String prefix, String suffix, List<String> referencedModelNames) {
// this is used to set dataType, which defines a python tuple of classes
String fullSuffix = suffix;
if (")".equals(suffix)) {
Expand All @@ -915,6 +929,9 @@ private String getTypeString(Schema p, String prefix, String suffix) {
Schema s = ModelUtils.getReferencedSchema(this.openAPI, p);
if (s instanceof ComposedSchema) {
String modelName = ModelUtils.getSimpleRef(p.get$ref());
if (referencedModelNames != null) {
referencedModelNames.add(modelName);
}
return prefix + toModelName(modelName) + fullSuffix;
}
}
Expand All @@ -930,7 +947,7 @@ private String getTypeString(Schema p, String prefix, String suffix) {
}
if ((ModelUtils.isMapSchema(p) || "object".equals(p.getType())) && getAdditionalProperties(p) != null) {
Schema inner = getAdditionalProperties(p);
return prefix + "{str: " + getTypeString(inner, "(", ")") + "}" + fullSuffix;
return prefix + "{str: " + getTypeString(inner, "(", ")", referencedModelNames) + "}" + fullSuffix;
} else if (ModelUtils.isArraySchema(p)) {
ArraySchema ap = (ArraySchema) p;
Schema inner = ap.getItems();
Expand All @@ -943,9 +960,9 @@ private String getTypeString(Schema p, String prefix, String suffix) {
// "[bool, date, datetime, dict, float, int, list, str, none_type]"
// Using recursion to wrap the allowed python types in an array.
Schema anyType = new Schema(); // A Schema without any attribute represents 'any type'.
return getTypeString(anyType, "[", "]");
return getTypeString(anyType, "[", "]", referencedModelNames);
} else {
return prefix + getTypeString(inner, "[", "]") + fullSuffix;
return prefix + getTypeString(inner, "[", "]", referencedModelNames) + fullSuffix;
}
}
if (ModelUtils.isFileSchema(p)) {
Expand All @@ -967,7 +984,7 @@ public String getTypeDeclaration(Schema p) {
// in Python we will wrap this in () to make it a tuple but here we
// will omit the parens so the generated documentaion will not include
// them
return getTypeString(p, "", "");
return getTypeString(p, "", "", null);
}

@Override
Expand All @@ -986,7 +1003,11 @@ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Sc
// store it in codegenModel.additionalPropertiesType.
// The 'addProps' may be a reference, getTypeDeclaration will resolve
// the reference.
codegenModel.additionalPropertiesType = getTypeDeclaration(addProps);
List<String> referencedModelNames = new ArrayList<String>();
codegenModel.additionalPropertiesType = getTypeString(addProps, "", "", referencedModelNames);
if (referencedModelNames.size() != 0) {
codegenModel.vendorExtensions.put(referencedModelNamesExtension, referencedModelNames);
}
}
// If addProps is null, the value of the 'additionalProperties' keyword is set
// to false, i.e. no additional properties are allowed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
str,
validate_get_composed_info,
)
try:
from petstore_api.models import fruit
except ImportError:
fruit = sys.modules[
'petstore_api.models.fruit']
try:
from petstore_api.models import nullable_shape
except ImportError:
Expand Down

0 comments on commit f57338d

Please sign in to comment.