Skip to content

Commit e95377a

Browse files
committed
fix(python): support default values for array/map field
1 parent f659457 commit e95377a

File tree

46 files changed

+1482
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1482
-17
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,13 +2160,8 @@ private String finalizeType(CodegenProperty cp, PythonType pt) {
21602160
if (cp.defaultValue == null) {
21612161
pt.setDefaultValue("None");
21622162
} else {
2163-
if (cp.isArray || cp.isMap) {
2164-
// TODO handle default value for array/map
2165-
pt.setDefaultValue("None");
2166-
} else {
2167-
//defaultValue = ;
2168-
pt.setDefaultValue(cp.defaultValue);
2169-
}
2163+
//defaultValue = ;
2164+
pt.setDefaultValue(cp.defaultValue);
21702165
}
21712166
}
21722167

modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,3 +2910,20 @@ components:
29102910
- 1.0
29112911
- 0.5
29122912
- 0.25
2913+
ModelWithArrayAndMapDefaults:
2914+
type: object
2915+
properties:
2916+
array:
2917+
type: array
2918+
items:
2919+
type: number
2920+
default: [1, 2]
2921+
map:
2922+
type: object
2923+
properties:
2924+
key1:
2925+
type: string
2926+
default: "value1"
2927+
key2:
2928+
type: string
2929+
default: "value2"

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/default_value.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class DefaultValue(BaseModel):
2828
"""
2929
to test the default value of properties
3030
""" # noqa: E501
31-
array_string_enum_ref_default: Optional[List[StringEnumRef]] = None
32-
array_string_enum_default: Optional[List[StrictStr]] = None
33-
array_string_default: Optional[List[StrictStr]] = None
34-
array_integer_default: Optional[List[StrictInt]] = None
31+
array_string_enum_ref_default: Optional[List[StringEnumRef]] = ["success","failure"]
32+
array_string_enum_default: Optional[List[StrictStr]] = ["success","failure"]
33+
array_string_default: Optional[List[StrictStr]] = ["failure","skipped"]
34+
array_integer_default: Optional[List[StrictInt]] = [1,3]
3535
array_string: Optional[List[StrictStr]] = None
3636
array_string_nullable: Optional[List[StrictStr]] = None
3737
array_string_extension_nullable: Optional[List[StrictStr]] = None

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Query(BaseModel):
2828
Query
2929
""" # noqa: E501
3030
id: Optional[StrictInt] = Field(default=None, description="Query")
31-
outcomes: Optional[List[StrictStr]] = None
31+
outcomes: Optional[List[StrictStr]] = ["SUCCESS","FAILURE"]
3232
__properties: ClassVar[List[str]] = ["id", "outcomes"]
3333

3434
@field_validator('outcomes')

samples/client/echo_api/python/openapi_client/models/default_value.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class DefaultValue(BaseModel):
2828
"""
2929
to test the default value of properties
3030
""" # noqa: E501
31-
array_string_enum_ref_default: Optional[List[StringEnumRef]] = None
32-
array_string_enum_default: Optional[List[StrictStr]] = None
33-
array_string_default: Optional[List[StrictStr]] = None
34-
array_integer_default: Optional[List[StrictInt]] = None
31+
array_string_enum_ref_default: Optional[List[StringEnumRef]] = ["success","failure"]
32+
array_string_enum_default: Optional[List[StrictStr]] = ["success","failure"]
33+
array_string_default: Optional[List[StrictStr]] = ["failure","skipped"]
34+
array_integer_default: Optional[List[StrictInt]] = [1,3]
3535
array_string: Optional[List[StrictStr]] = None
3636
array_string_nullable: Optional[List[StrictStr]] = None
3737
array_string_extension_nullable: Optional[List[StrictStr]] = None

samples/client/echo_api/python/openapi_client/models/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Query(BaseModel):
2828
Query
2929
""" # noqa: E501
3030
id: Optional[StrictInt] = Field(default=None, description="Query")
31-
outcomes: Optional[List[StrictStr]] = None
31+
outcomes: Optional[List[StrictStr]] = ["SUCCESS","FAILURE"]
3232
__properties: ClassVar[List[str]] = ["id", "outcomes"]
3333

3434
@field_validator('outcomes')

samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ docs/Model200Response.md
6969
docs/ModelApiResponse.md
7070
docs/ModelField.md
7171
docs/ModelReturn.md
72+
docs/ModelWithArrayAndMapDefaults.md
73+
docs/ModelWithArrayAndMapDefaultsMap.md
7274
docs/MultiArrays.md
7375
docs/Name.md
7476
docs/NullableClass.md
@@ -198,6 +200,8 @@ petstore_api/models/model200_response.py
198200
petstore_api/models/model_api_response.py
199201
petstore_api/models/model_field.py
200202
petstore_api/models/model_return.py
203+
petstore_api/models/model_with_array_and_map_defaults.py
204+
petstore_api/models/model_with_array_and_map_defaults_map.py
201205
petstore_api/models/multi_arrays.py
202206
petstore_api/models/name.py
203207
petstore_api/models/nullable_class.py
@@ -256,4 +260,5 @@ setup.cfg
256260
setup.py
257261
test-requirements.txt
258262
test/__init__.py
263+
test/test_model_with_array_and_map_defaults_map.py
259264
tox.ini

samples/openapi3/client/petstore/python-aiohttp/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ Class | Method | HTTP request | Description
212212
- [ModelApiResponse](docs/ModelApiResponse.md)
213213
- [ModelField](docs/ModelField.md)
214214
- [ModelReturn](docs/ModelReturn.md)
215+
- [ModelWithArrayAndMapDefaults](docs/ModelWithArrayAndMapDefaults.md)
216+
- [ModelWithArrayAndMapDefaultsMap](docs/ModelWithArrayAndMapDefaultsMap.md)
215217
- [MultiArrays](docs/MultiArrays.md)
216218
- [Name](docs/Name.md)
217219
- [NullableClass](docs/NullableClass.md)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ModelWithArrayAndMapDefaults
2+
3+
4+
## Properties
5+
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
**array** | **List[float]** | | [optional] [default to [1,2]]
9+
**map** | [**ModelWithArrayAndMapDefaultsMap**](ModelWithArrayAndMapDefaultsMap.md) | | [optional]
10+
11+
## Example
12+
13+
```python
14+
from petstore_api.models.model_with_array_and_map_defaults import ModelWithArrayAndMapDefaults
15+
16+
# TODO update the JSON string below
17+
json = "{}"
18+
# create an instance of ModelWithArrayAndMapDefaults from a JSON string
19+
model_with_array_and_map_defaults_instance = ModelWithArrayAndMapDefaults.from_json(json)
20+
# print the JSON string representation of the object
21+
print(ModelWithArrayAndMapDefaults.to_json())
22+
23+
# convert the object into a dict
24+
model_with_array_and_map_defaults_dict = model_with_array_and_map_defaults_instance.to_dict()
25+
# create an instance of ModelWithArrayAndMapDefaults from a dict
26+
model_with_array_and_map_defaults_from_dict = ModelWithArrayAndMapDefaults.from_dict(model_with_array_and_map_defaults_dict)
27+
```
28+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
29+
30+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ModelWithArrayAndMapDefaultsMap
2+
3+
4+
## Properties
5+
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
**key1** | **str** | | [optional] [default to 'value1']
9+
**key2** | **str** | | [optional] [default to 'value2']
10+
11+
## Example
12+
13+
```python
14+
from petstore_api.models.model_with_array_and_map_defaults_map import ModelWithArrayAndMapDefaultsMap
15+
16+
# TODO update the JSON string below
17+
json = "{}"
18+
# create an instance of ModelWithArrayAndMapDefaultsMap from a JSON string
19+
model_with_array_and_map_defaults_map_instance = ModelWithArrayAndMapDefaultsMap.from_json(json)
20+
# print the JSON string representation of the object
21+
print(ModelWithArrayAndMapDefaultsMap.to_json())
22+
23+
# convert the object into a dict
24+
model_with_array_and_map_defaults_map_dict = model_with_array_and_map_defaults_map_instance.to_dict()
25+
# create an instance of ModelWithArrayAndMapDefaultsMap from a dict
26+
model_with_array_and_map_defaults_map_from_dict = ModelWithArrayAndMapDefaultsMap.from_dict(model_with_array_and_map_defaults_map_dict)
27+
```
28+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
29+
30+

0 commit comments

Comments
 (0)