Skip to content

Commit 843c638

Browse files
author
Netter, Joshua (319E-Affiliate)
committed
Added the ability for generated Python clients to access the additional properties of models with additionalProperties defined.
1 parent e5a77af commit 843c638

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public PythonClientCodegen() {
7777
languageSpecificPrimitives.add("date");
7878
languageSpecificPrimitives.add("object");
7979

80+
instantiationTypes.put("array", "list");
81+
instantiationTypes.put("map", "dict");
82+
8083
typeMapping.clear();
8184
typeMapping.put("integer", "int");
8285
typeMapping.put("float", "float");
@@ -350,6 +353,14 @@ public String modelTestFileFolder() {
350353
return outputFolder + File.separatorChar + testFolder;
351354
}
352355

356+
@Override
357+
public String toInstantiationType(Property p) {
358+
if (p instanceof MapProperty) {
359+
return instantiationTypes.get("map");
360+
}
361+
return super.toInstantiationType(p);
362+
}
363+
353364
@Override
354365
public String getTypeDeclaration(Property p) {
355366
if (p instanceof ArrayProperty) {

modules/swagger-codegen/src/main/resources/python/api_client.mustache

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,12 @@ class ApiClient(object):
620620

621621
instance = klass(**kwargs)
622622

623+
if (isinstance(instance, dict) and
624+
klass.swagger_types is not None and
625+
isinstance(data, dict)):
626+
for key, value in data.items():
627+
if key not in klass.swagger_types:
628+
instance[key] = value
623629
if hasattr(instance, 'get_real_child_model'):
624630
klass_name = instance.get_real_child_model(data)
625631
if klass_name:

modules/swagger-codegen/src/main/resources/python/model.mustache

100644100755
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import six
1414

1515
{{#models}}
1616
{{#model}}
17-
class {{classname}}(object):
17+
class {{classname}}({{#parent}}{{parent}}{{/parent}}{{^parent}}object{{/parent}}):
1818
"""NOTE: This class is auto generated by the swagger code generator program.
1919

2020
Do not edit the class manually.
@@ -194,6 +194,9 @@ class {{classname}}(object):
194194
))
195195
else:
196196
result[attr] = value
197+
if issubclass({{classname}}, dict):
198+
for key, value in self.items():
199+
result[key] = value
197200

198201
return result
199202

0 commit comments

Comments
 (0)