mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-09 13:46:10 +00:00
[Python] Handle nullable dictionary values (#17605)
* fix nullable elements in maps * update examples * exclude values typed as Any
This commit is contained in:
@@ -693,14 +693,10 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
|
|||||||
if (ModelUtils.isArraySchema(p)) {
|
if (ModelUtils.isArraySchema(p)) {
|
||||||
ArraySchema ap = (ArraySchema) p;
|
ArraySchema ap = (ArraySchema) p;
|
||||||
Schema inner = ap.getItems();
|
Schema inner = ap.getItems();
|
||||||
String innerDeclaration = getTypeDeclaration(inner);
|
return getSchemaType(p) + "[" + getCollectionItemTypeDeclaration(inner) + "]";
|
||||||
if (inner.getNullable() != null && inner.getNullable()) {
|
|
||||||
innerDeclaration = "Optional[" + innerDeclaration + "]";
|
|
||||||
}
|
|
||||||
return getSchemaType(p) + "[" + innerDeclaration + "]";
|
|
||||||
} else if (ModelUtils.isMapSchema(p)) {
|
} else if (ModelUtils.isMapSchema(p)) {
|
||||||
Schema inner = ModelUtils.getAdditionalProperties(p);
|
Schema inner = ModelUtils.getAdditionalProperties(p);
|
||||||
return getSchemaType(p) + "[str, " + getTypeDeclaration(inner) + "]";
|
return getSchemaType(p) + "[str, " + getCollectionItemTypeDeclaration(inner) + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
String openAPIType = getSchemaType(p);
|
String openAPIType = getSchemaType(p);
|
||||||
@@ -715,6 +711,14 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
|
|||||||
return toModelName(openAPIType);
|
return toModelName(openAPIType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getCollectionItemTypeDeclaration(Schema inner) {
|
||||||
|
String innerDeclaration = getTypeDeclaration(inner);
|
||||||
|
if (Boolean.TRUE.equals(inner.getNullable())) {
|
||||||
|
innerDeclaration = "Optional[" + innerDeclaration + "]";
|
||||||
|
}
|
||||||
|
return innerDeclaration;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSchemaType(Schema p) {
|
public String getSchemaType(Schema p) {
|
||||||
String openAPIType = super.getSchemaType(p);
|
String openAPIType = super.getSchemaType(p);
|
||||||
@@ -1758,7 +1762,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
|
|||||||
|
|
||||||
private PythonType collectionItemType(CodegenProperty itemCp) {
|
private PythonType collectionItemType(CodegenProperty itemCp) {
|
||||||
PythonType itemPt = getType(itemCp);
|
PythonType itemPt = getType(itemCp);
|
||||||
if (itemCp != null && itemCp.isNullable) {
|
if (itemCp != null && !itemPt.type.equals("Any") && itemCp.isNullable) {
|
||||||
moduleImports.add("typing", "Optional");
|
moduleImports.add("typing", "Optional");
|
||||||
PythonType opt = new PythonType("Optional");
|
PythonType opt = new PythonType("Optional");
|
||||||
opt.addTypeParam(itemPt);
|
opt.addTypeParam(itemPt);
|
||||||
@@ -1802,7 +1806,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
|
|||||||
moduleImports.add("typing", "Dict");
|
moduleImports.add("typing", "Dict");
|
||||||
PythonType pt = new PythonType("Dict");
|
PythonType pt = new PythonType("Dict");
|
||||||
pt.addTypeParam(new PythonType("str"));
|
pt.addTypeParam(new PythonType("str"));
|
||||||
pt.addTypeParam(getType(cp.getItems()));
|
pt.addTypeParam(collectionItemType(cp.getItems()));
|
||||||
return pt;
|
return pt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ Name | Type | Description | Notes
|
|||||||
**array_and_items_nullable_prop** | **List[Optional[object]]** | | [optional]
|
**array_and_items_nullable_prop** | **List[Optional[object]]** | | [optional]
|
||||||
**array_items_nullable** | **List[Optional[object]]** | | [optional]
|
**array_items_nullable** | **List[Optional[object]]** | | [optional]
|
||||||
**object_nullable_prop** | **Dict[str, object]** | | [optional]
|
**object_nullable_prop** | **Dict[str, object]** | | [optional]
|
||||||
**object_and_items_nullable_prop** | **Dict[str, object]** | | [optional]
|
**object_and_items_nullable_prop** | **Dict[str, Optional[object]]** | | [optional]
|
||||||
**object_items_nullable** | **Dict[str, object]** | | [optional]
|
**object_items_nullable** | **Dict[str, Optional[object]]** | | [optional]
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
|
|||||||
@@ -38,8 +38,8 @@ class NullableClass(BaseModel):
|
|||||||
array_and_items_nullable_prop: Optional[List[Optional[Dict[str, Any]]]] = None
|
array_and_items_nullable_prop: Optional[List[Optional[Dict[str, Any]]]] = None
|
||||||
array_items_nullable: 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_nullable_prop: Optional[Dict[str, Dict[str, Any]]] = None
|
||||||
object_and_items_nullable_prop: 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, Dict[str, Any]]] = None
|
object_items_nullable: Optional[Dict[str, Optional[Dict[str, Any]]]] = None
|
||||||
additional_properties: Dict[str, Any] = {}
|
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"]
|
__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"]
|
||||||
|
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ Name | Type | Description | Notes
|
|||||||
**array_and_items_nullable_prop** | **List[Optional[object]]** | | [optional]
|
**array_and_items_nullable_prop** | **List[Optional[object]]** | | [optional]
|
||||||
**array_items_nullable** | **List[Optional[object]]** | | [optional]
|
**array_items_nullable** | **List[Optional[object]]** | | [optional]
|
||||||
**object_nullable_prop** | **Dict[str, object]** | | [optional]
|
**object_nullable_prop** | **Dict[str, object]** | | [optional]
|
||||||
**object_and_items_nullable_prop** | **Dict[str, object]** | | [optional]
|
**object_and_items_nullable_prop** | **Dict[str, Optional[object]]** | | [optional]
|
||||||
**object_items_nullable** | **Dict[str, object]** | | [optional]
|
**object_items_nullable** | **Dict[str, Optional[object]]** | | [optional]
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
|
|||||||
@@ -38,8 +38,8 @@ class NullableClass(BaseModel):
|
|||||||
array_and_items_nullable_prop: Optional[List[Optional[Dict[str, Any]]]] = None
|
array_and_items_nullable_prop: Optional[List[Optional[Dict[str, Any]]]] = None
|
||||||
array_items_nullable: 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_nullable_prop: Optional[Dict[str, Dict[str, Any]]] = None
|
||||||
object_and_items_nullable_prop: 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, Dict[str, Any]]] = None
|
object_items_nullable: Optional[Dict[str, Optional[Dict[str, Any]]]] = None
|
||||||
additional_properties: Dict[str, Any] = {}
|
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"]
|
__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"]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user