Skip to content

Commit 8e9a17f

Browse files
authored
[Python] Handle nullable dictionary values (#17605)
* fix nullable elements in maps * update examples * exclude values typed as Any
1 parent 406bc28 commit 8e9a17f

5 files changed

Lines changed: 20 additions & 16 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -693,14 +693,10 @@ public String getTypeDeclaration(Schema p) {
693693
if (ModelUtils.isArraySchema(p)) {
694694
ArraySchema ap = (ArraySchema) p;
695695
Schema inner = ap.getItems();
696-
String innerDeclaration = getTypeDeclaration(inner);
697-
if (inner.getNullable() != null && inner.getNullable()) {
698-
innerDeclaration = "Optional[" + innerDeclaration + "]";
699-
}
700-
return getSchemaType(p) + "[" + innerDeclaration + "]";
696+
return getSchemaType(p) + "[" + getCollectionItemTypeDeclaration(inner) + "]";
701697
} else if (ModelUtils.isMapSchema(p)) {
702698
Schema inner = ModelUtils.getAdditionalProperties(p);
703-
return getSchemaType(p) + "[str, " + getTypeDeclaration(inner) + "]";
699+
return getSchemaType(p) + "[str, " + getCollectionItemTypeDeclaration(inner) + "]";
704700
}
705701

706702
String openAPIType = getSchemaType(p);
@@ -715,6 +711,14 @@ public String getTypeDeclaration(Schema p) {
715711
return toModelName(openAPIType);
716712
}
717713

714+
private String getCollectionItemTypeDeclaration(Schema inner) {
715+
String innerDeclaration = getTypeDeclaration(inner);
716+
if (Boolean.TRUE.equals(inner.getNullable())) {
717+
innerDeclaration = "Optional[" + innerDeclaration + "]";
718+
}
719+
return innerDeclaration;
720+
}
721+
718722
@Override
719723
public String getSchemaType(Schema p) {
720724
String openAPIType = super.getSchemaType(p);
@@ -1758,7 +1762,7 @@ private PythonType arrayType(IJsonSchemaValidationProperties cp) {
17581762

17591763
private PythonType collectionItemType(CodegenProperty itemCp) {
17601764
PythonType itemPt = getType(itemCp);
1761-
if (itemCp != null && itemCp.isNullable) {
1765+
if (itemCp != null && !itemPt.type.equals("Any") && itemCp.isNullable) {
17621766
moduleImports.add("typing", "Optional");
17631767
PythonType opt = new PythonType("Optional");
17641768
opt.addTypeParam(itemPt);
@@ -1802,7 +1806,7 @@ private PythonType mapType(IJsonSchemaValidationProperties cp) {
18021806
moduleImports.add("typing", "Dict");
18031807
PythonType pt = new PythonType("Dict");
18041808
pt.addTypeParam(new PythonType("str"));
1805-
pt.addTypeParam(getType(cp.getItems()));
1809+
pt.addTypeParam(collectionItemType(cp.getItems()));
18061810
return pt;
18071811
}
18081812

samples/openapi3/client/petstore/python-aiohttp/docs/NullableClass.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Name | Type | Description | Notes
1616
**array_and_items_nullable_prop** | **List[Optional[object]]** | | [optional]
1717
**array_items_nullable** | **List[Optional[object]]** | | [optional]
1818
**object_nullable_prop** | **Dict[str, object]** | | [optional]
19-
**object_and_items_nullable_prop** | **Dict[str, object]** | | [optional]
20-
**object_items_nullable** | **Dict[str, object]** | | [optional]
19+
**object_and_items_nullable_prop** | **Dict[str, Optional[object]]** | | [optional]
20+
**object_items_nullable** | **Dict[str, Optional[object]]** | | [optional]
2121

2222
## Example
2323

samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/nullable_class.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class NullableClass(BaseModel):
3838
array_and_items_nullable_prop: Optional[List[Optional[Dict[str, Any]]]] = None
3939
array_items_nullable: Optional[List[Optional[Dict[str, Any]]]] = None
4040
object_nullable_prop: Optional[Dict[str, Dict[str, Any]]] = None
41-
object_and_items_nullable_prop: Optional[Dict[str, Dict[str, Any]]] = None
42-
object_items_nullable: Optional[Dict[str, Dict[str, Any]]] = None
41+
object_and_items_nullable_prop: Optional[Dict[str, Optional[Dict[str, Any]]]] = None
42+
object_items_nullable: Optional[Dict[str, Optional[Dict[str, Any]]]] = None
4343
additional_properties: Dict[str, Any] = {}
4444
__properties: ClassVar[List[str]] = ["required_integer_prop", "integer_prop", "number_prop", "boolean_prop", "string_prop", "date_prop", "datetime_prop", "array_nullable_prop", "array_and_items_nullable_prop", "array_items_nullable", "object_nullable_prop", "object_and_items_nullable_prop", "object_items_nullable"]
4545

samples/openapi3/client/petstore/python/docs/NullableClass.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Name | Type | Description | Notes
1616
**array_and_items_nullable_prop** | **List[Optional[object]]** | | [optional]
1717
**array_items_nullable** | **List[Optional[object]]** | | [optional]
1818
**object_nullable_prop** | **Dict[str, object]** | | [optional]
19-
**object_and_items_nullable_prop** | **Dict[str, object]** | | [optional]
20-
**object_items_nullable** | **Dict[str, object]** | | [optional]
19+
**object_and_items_nullable_prop** | **Dict[str, Optional[object]]** | | [optional]
20+
**object_items_nullable** | **Dict[str, Optional[object]]** | | [optional]
2121

2222
## Example
2323

samples/openapi3/client/petstore/python/petstore_api/models/nullable_class.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class NullableClass(BaseModel):
3838
array_and_items_nullable_prop: Optional[List[Optional[Dict[str, Any]]]] = None
3939
array_items_nullable: Optional[List[Optional[Dict[str, Any]]]] = None
4040
object_nullable_prop: Optional[Dict[str, Dict[str, Any]]] = None
41-
object_and_items_nullable_prop: Optional[Dict[str, Dict[str, Any]]] = None
42-
object_items_nullable: Optional[Dict[str, Dict[str, Any]]] = None
41+
object_and_items_nullable_prop: Optional[Dict[str, Optional[Dict[str, Any]]]] = None
42+
object_items_nullable: Optional[Dict[str, Optional[Dict[str, Any]]]] = None
4343
additional_properties: Dict[str, Any] = {}
4444
__properties: ClassVar[List[str]] = ["required_integer_prop", "integer_prop", "number_prop", "boolean_prop", "string_prop", "date_prop", "datetime_prop", "array_nullable_prop", "array_and_items_nullable_prop", "array_items_nullable", "object_nullable_prop", "object_and_items_nullable_prop", "object_items_nullable"]
4545

0 commit comments

Comments
 (0)