Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added support for additionalProperties in Python code generator. Fixes
  • Loading branch information
ivan-gomes committed Jan 31, 2019
commit 75f2d2c25588a33a8937ee9475de39cf6b9e46fd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openapitools.codegen.languages;

import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -91,6 +92,8 @@ public PythonClientCodegen() {
// TODO file and binary is mapped as `file`
languageSpecificPrimitives.add("file");

instantiationTypes.put("map", "dict");

typeMapping.clear();
typeMapping.put("integer", "int");
typeMapping.put("float", "float");
Expand Down Expand Up @@ -412,10 +415,19 @@ public String getTypeDeclaration(Schema p) {
Schema inner = ModelUtils.getAdditionalProperties(p);

return getSchemaType(p) + "(str, " + getTypeDeclaration(inner) + ")";

}
return super.getTypeDeclaration(p);
}

@Override
public String toInstantiationType(Schema property) {
if (property instanceof ArraySchema || property instanceof MapSchema || property.getAdditionalProperties() != null) {
return getSchemaType(property);
}
return super.toInstantiationType(property);
}

@Override
public String getSchemaType(Schema p) {
String openAPIType = super.getSchemaType(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ class ApiClient(object):
return obj.isoformat()

if isinstance(obj, dict):
obj_dict = obj
to_dict_attr = getattr(obj, 'to_dict', None)
if to_dict_attr is not None and callable(to_dict_attr):
obj_dict = obj.to_dict()
else:
obj_dict = obj
else:
# Convert model obj to dict except
# attributes `openapi_types`, `attribute_map`
Expand Down Expand Up @@ -634,6 +638,12 @@ class ApiClient(object):

instance = klass(**kwargs)

if (isinstance(instance, dict) and
klass.openapi_types is not None and
isinstance(data, dict)):
for key, value in data.items():
if key not in klass.openapi_types:
instance[key] = value
if hasattr(instance, 'get_real_child_model'):
klass_name = instance.get_real_child_model(data)
if klass_name:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import six

{{#models}}
{{#model}}
class {{classname}}(object):
class {{classname}}({{#parent}}{{{parent}}}{{/parent}}{{^parent}}object{{/parent}}):
"""NOTE: This class is auto generated by OpenAPI Generator.
Ref: https://openapi-generator.tech

Expand Down Expand Up @@ -199,6 +199,9 @@ class {{classname}}(object):
))
else:
result[attr] = value
if issubclass({{classname}}, dict):
for key, value in self.items():
result[key] = value

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public void arrayModelTest() {
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.description, "an array model");
Assert.assertEquals(cm.vars.size(), 0);
Assert.assertEquals(cm.parent, "null<Children>");
Assert.assertEquals(cm.parent, "list");
Assert.assertEquals(cm.imports.size(), 1);
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}
Expand All @@ -291,7 +291,7 @@ public void mapModelTest() {
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.description, "a map model");
Assert.assertEquals(cm.vars.size(), 0);
Assert.assertEquals(cm.parent, "null<String, Children>");
Assert.assertEquals(cm.parent, "dict");
Assert.assertEquals(cm.imports.size(), 1);
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
}
Expand Down