Skip to content

Commit

Permalink
[Python] Handle nullable dictionary values (OpenAPITools#17605)
Browse files Browse the repository at this point in the history
* fix nullable elements in maps

* update examples

* exclude values typed as Any
  • Loading branch information
changhc authored Jan 13, 2024
1 parent 406bc28 commit 8e9a17f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -693,14 +693,10 @@ public String getTypeDeclaration(Schema p) {
if (ModelUtils.isArraySchema(p)) {
ArraySchema ap = (ArraySchema) p;
Schema inner = ap.getItems();
String innerDeclaration = getTypeDeclaration(inner);
if (inner.getNullable() != null && inner.getNullable()) {
innerDeclaration = "Optional[" + innerDeclaration + "]";
}
return getSchemaType(p) + "[" + innerDeclaration + "]";
return getSchemaType(p) + "[" + getCollectionItemTypeDeclaration(inner) + "]";
} else if (ModelUtils.isMapSchema(p)) {
Schema inner = ModelUtils.getAdditionalProperties(p);
return getSchemaType(p) + "[str, " + getTypeDeclaration(inner) + "]";
return getSchemaType(p) + "[str, " + getCollectionItemTypeDeclaration(inner) + "]";
}

String openAPIType = getSchemaType(p);
Expand All @@ -715,6 +711,14 @@ public String getTypeDeclaration(Schema p) {
return toModelName(openAPIType);
}

private String getCollectionItemTypeDeclaration(Schema inner) {
String innerDeclaration = getTypeDeclaration(inner);
if (Boolean.TRUE.equals(inner.getNullable())) {
innerDeclaration = "Optional[" + innerDeclaration + "]";
}
return innerDeclaration;
}

@Override
public String getSchemaType(Schema p) {
String openAPIType = super.getSchemaType(p);
Expand Down Expand Up @@ -1758,7 +1762,7 @@ private PythonType arrayType(IJsonSchemaValidationProperties cp) {

private PythonType collectionItemType(CodegenProperty itemCp) {
PythonType itemPt = getType(itemCp);
if (itemCp != null && itemCp.isNullable) {
if (itemCp != null && !itemPt.type.equals("Any") && itemCp.isNullable) {
moduleImports.add("typing", "Optional");
PythonType opt = new PythonType("Optional");
opt.addTypeParam(itemPt);
Expand Down Expand Up @@ -1802,7 +1806,7 @@ private PythonType mapType(IJsonSchemaValidationProperties cp) {
moduleImports.add("typing", "Dict");
PythonType pt = new PythonType("Dict");
pt.addTypeParam(new PythonType("str"));
pt.addTypeParam(getType(cp.getItems()));
pt.addTypeParam(collectionItemType(cp.getItems()));
return pt;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Name | Type | Description | Notes
**array_and_items_nullable_prop** | **List[Optional[object]]** | | [optional]
**array_items_nullable** | **List[Optional[object]]** | | [optional]
**object_nullable_prop** | **Dict[str, object]** | | [optional]
**object_and_items_nullable_prop** | **Dict[str, object]** | | [optional]
**object_items_nullable** | **Dict[str, object]** | | [optional]
**object_and_items_nullable_prop** | **Dict[str, Optional[object]]** | | [optional]
**object_items_nullable** | **Dict[str, Optional[object]]** | | [optional]

## Example

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class NullableClass(BaseModel):
array_and_items_nullable_prop: Optional[List[Optional[Dict[str, Any]]]] = None
array_items_nullable: Optional[List[Optional[Dict[str, Any]]]] = None
object_nullable_prop: Optional[Dict[str, Dict[str, Any]]] = None
object_and_items_nullable_prop: Optional[Dict[str, Dict[str, Any]]] = None
object_items_nullable: Optional[Dict[str, Dict[str, Any]]] = None
object_and_items_nullable_prop: Optional[Dict[str, Optional[Dict[str, Any]]]] = None
object_items_nullable: Optional[Dict[str, Optional[Dict[str, Any]]]] = None
additional_properties: Dict[str, Any] = {}
__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"]

Expand Down
4 changes: 2 additions & 2 deletions samples/openapi3/client/petstore/python/docs/NullableClass.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Name | Type | Description | Notes
**array_and_items_nullable_prop** | **List[Optional[object]]** | | [optional]
**array_items_nullable** | **List[Optional[object]]** | | [optional]
**object_nullable_prop** | **Dict[str, object]** | | [optional]
**object_and_items_nullable_prop** | **Dict[str, object]** | | [optional]
**object_items_nullable** | **Dict[str, object]** | | [optional]
**object_and_items_nullable_prop** | **Dict[str, Optional[object]]** | | [optional]
**object_items_nullable** | **Dict[str, Optional[object]]** | | [optional]

## Example

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class NullableClass(BaseModel):
array_and_items_nullable_prop: Optional[List[Optional[Dict[str, Any]]]] = None
array_items_nullable: Optional[List[Optional[Dict[str, Any]]]] = None
object_nullable_prop: Optional[Dict[str, Dict[str, Any]]] = None
object_and_items_nullable_prop: Optional[Dict[str, Dict[str, Any]]] = None
object_items_nullable: Optional[Dict[str, Dict[str, Any]]] = None
object_and_items_nullable_prop: Optional[Dict[str, Optional[Dict[str, Any]]]] = None
object_items_nullable: Optional[Dict[str, Optional[Dict[str, Any]]]] = None
additional_properties: Dict[str, Any] = {}
__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"]

Expand Down

0 comments on commit 8e9a17f

Please sign in to comment.