forked from loafle/openapi-generator-original
[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)) {
|
||||
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);
|
||||
@@ -715,6 +711,14 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
|
||||
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);
|
||||
@@ -1758,7 +1762,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
|
||||
|
||||
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);
|
||||
@@ -1802,7 +1806,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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"]
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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"]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user