Skip to content

Commit a5e2fdf

Browse files
authored
[PYTHON] switch from mutable bytearray to immutable bytes to avoid pydantic error (#22988)
* switch from mutable bytearray to immutable bytes * update python samples
1 parent 905b930 commit a5e2fdf

33 files changed

Lines changed: 231 additions & 232 deletions

File tree

docs/generators/python.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5555
<li>List</li>
5656
<li>UUID</li>
5757
<li>bool</li>
58-
<li>bytearray</li>
5958
<li>bytes</li>
6059
<li>date</li>
6160
<li>datetime</li>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ public PythonClientCodegen() {
100100
typeMapping.put("set", "List");
101101
typeMapping.put("map", "Dict");
102102
typeMapping.put("decimal", "decimal.Decimal");
103-
typeMapping.put("file", "bytearray");
104-
typeMapping.put("binary", "bytearray");
105-
typeMapping.put("ByteArray", "bytearray");
103+
typeMapping.put("file", "bytes");
104+
typeMapping.put("binary", "bytes");
105+
typeMapping.put("ByteArray", "bytes");
106106

107107
languageSpecificPrimitives.remove("file");
108108
languageSpecificPrimitives.add("decimal.Decimal");
109-
languageSpecificPrimitives.add("bytearray");
109+
languageSpecificPrimitives.add("bytes");
110110
languageSpecificPrimitives.add("none_type");
111111

112112
supportsInheritance = true;

modules/openapi-generator/src/main/resources/python/api_client.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class ApiClient:
313313
response_text = None
314314
return_data = None
315315
try:
316-
if response_type == "bytearray":
316+
if response_type in ("bytearray", "bytes"):
317317
return_data = response_data.data
318318
elif response_type == "file":
319319
return_data = self.__deserialize_file(response_data)

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/docs/BodyApi.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Method | HTTP request | Description
1717

1818

1919
# **test_binary_gif**
20-
> bytearray test_binary_gif()
20+
> bytes test_binary_gif()
2121
2222
Test binary (gif) response body
2323

@@ -60,7 +60,7 @@ This endpoint does not need any parameter.
6060

6161
### Return type
6262

63-
**bytearray**
63+
**bytes**
6464

6565
### Authorization
6666

@@ -105,7 +105,7 @@ configuration = openapi_client.Configuration(
105105
with openapi_client.ApiClient(configuration) as api_client:
106106
# Create an instance of the API class
107107
api_instance = openapi_client.BodyApi(api_client)
108-
body = None # bytearray | (optional)
108+
body = None # bytes | (optional)
109109

110110
try:
111111
# Test body parameter(s)
@@ -123,7 +123,7 @@ with openapi_client.ApiClient(configuration) as api_client:
123123

124124
Name | Type | Description | Notes
125125
------------- | ------------- | ------------- | -------------
126-
**body** | **bytearray**| | [optional]
126+
**body** | **bytes**| | [optional]
127127

128128
### Return type
129129

@@ -172,7 +172,7 @@ configuration = openapi_client.Configuration(
172172
with openapi_client.ApiClient(configuration) as api_client:
173173
# Create an instance of the API class
174174
api_instance = openapi_client.BodyApi(api_client)
175-
files = None # List[bytearray] |
175+
files = None # List[bytes] |
176176

177177
try:
178178
# Test array of binary in multipart mime
@@ -190,7 +190,7 @@ with openapi_client.ApiClient(configuration) as api_client:
190190

191191
Name | Type | Description | Notes
192192
------------- | ------------- | ------------- | -------------
193-
**files** | **List[bytearray]**| |
193+
**files** | **List[bytes]**| |
194194

195195
### Return type
196196

@@ -239,7 +239,7 @@ configuration = openapi_client.Configuration(
239239
with openapi_client.ApiClient(configuration) as api_client:
240240
# Create an instance of the API class
241241
api_instance = openapi_client.BodyApi(api_client)
242-
my_file = None # bytearray | (optional)
242+
my_file = None # bytes | (optional)
243243

244244
try:
245245
# Test single binary in multipart mime
@@ -257,7 +257,7 @@ with openapi_client.ApiClient(configuration) as api_client:
257257

258258
Name | Type | Description | Notes
259259
------------- | ------------- | ------------- | -------------
260-
**my_file** | **bytearray**| | [optional]
260+
**my_file** | **bytes**| | [optional]
261261

262262
### Return type
263263

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/api/body_api.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_binary_gif(
5656
_content_type: Optional[StrictStr] = None,
5757
_headers: Optional[Dict[StrictStr, Any]] = None,
5858
_host_index: Annotated[StrictInt, Field(ge=0, le=0)] = 0,
59-
) -> bytearray:
59+
) -> bytes:
6060
"""Test binary (gif) response body
6161
6262
Test binary (gif) response body
@@ -91,7 +91,7 @@ def test_binary_gif(
9191
)
9292

9393
_response_types_map: Dict[str, Optional[str]] = {
94-
'200': "bytearray",
94+
'200': "bytes",
9595
}
9696
response_data = self.api_client.call_api(
9797
*_param,
@@ -119,7 +119,7 @@ def test_binary_gif_with_http_info(
119119
_content_type: Optional[StrictStr] = None,
120120
_headers: Optional[Dict[StrictStr, Any]] = None,
121121
_host_index: Annotated[StrictInt, Field(ge=0, le=0)] = 0,
122-
) -> ApiResponse[bytearray]:
122+
) -> ApiResponse[bytes]:
123123
"""Test binary (gif) response body
124124
125125
Test binary (gif) response body
@@ -154,7 +154,7 @@ def test_binary_gif_with_http_info(
154154
)
155155

156156
_response_types_map: Dict[str, Optional[str]] = {
157-
'200': "bytearray",
157+
'200': "bytes",
158158
}
159159
response_data = self.api_client.call_api(
160160
*_param,
@@ -217,7 +217,7 @@ def test_binary_gif_without_preload_content(
217217
)
218218

219219
_response_types_map: Dict[str, Optional[str]] = {
220-
'200': "bytearray",
220+
'200': "bytes",
221221
}
222222
response_data = self.api_client.call_api(
223223
*_param,
@@ -308,7 +308,7 @@ def test_body_application_octetstream_binary(
308308
Test body parameter(s)
309309
310310
:param body:
311-
:type body: bytearray
311+
:type body: bytes
312312
:param _request_timeout: timeout setting for this request. If one
313313
number provided, it will be total request
314314
timeout. It can also be a pair (tuple) of
@@ -375,7 +375,7 @@ def test_body_application_octetstream_binary_with_http_info(
375375
Test body parameter(s)
376376
377377
:param body:
378-
:type body: bytearray
378+
:type body: bytes
379379
:param _request_timeout: timeout setting for this request. If one
380380
number provided, it will be total request
381381
timeout. It can also be a pair (tuple) of
@@ -442,7 +442,7 @@ def test_body_application_octetstream_binary_without_preload_content(
442442
Test body parameter(s)
443443
444444
:param body:
445-
:type body: bytearray
445+
:type body: bytes
446446
:param _request_timeout: timeout setting for this request. If one
447447
number provided, it will be total request
448448
timeout. It can also be a pair (tuple) of
@@ -589,7 +589,7 @@ def test_body_multipart_formdata_array_of_binary(
589589
Test array of binary in multipart mime
590590
591591
:param files: (required)
592-
:type files: List[bytearray]
592+
:type files: List[bytes]
593593
:param _request_timeout: timeout setting for this request. If one
594594
number provided, it will be total request
595595
timeout. It can also be a pair (tuple) of
@@ -656,7 +656,7 @@ def test_body_multipart_formdata_array_of_binary_with_http_info(
656656
Test array of binary in multipart mime
657657
658658
:param files: (required)
659-
:type files: List[bytearray]
659+
:type files: List[bytes]
660660
:param _request_timeout: timeout setting for this request. If one
661661
number provided, it will be total request
662662
timeout. It can also be a pair (tuple) of
@@ -723,7 +723,7 @@ def test_body_multipart_formdata_array_of_binary_without_preload_content(
723723
Test array of binary in multipart mime
724724
725725
:param files: (required)
726-
:type files: List[bytearray]
726+
:type files: List[bytes]
727727
:param _request_timeout: timeout setting for this request. If one
728728
number provided, it will be total request
729729
timeout. It can also be a pair (tuple) of
@@ -863,7 +863,7 @@ def test_body_multipart_formdata_single_binary(
863863
Test single binary in multipart mime
864864
865865
:param my_file:
866-
:type my_file: bytearray
866+
:type my_file: bytes
867867
:param _request_timeout: timeout setting for this request. If one
868868
number provided, it will be total request
869869
timeout. It can also be a pair (tuple) of
@@ -930,7 +930,7 @@ def test_body_multipart_formdata_single_binary_with_http_info(
930930
Test single binary in multipart mime
931931
932932
:param my_file:
933-
:type my_file: bytearray
933+
:type my_file: bytes
934934
:param _request_timeout: timeout setting for this request. If one
935935
number provided, it will be total request
936936
timeout. It can also be a pair (tuple) of
@@ -997,7 +997,7 @@ def test_body_multipart_formdata_single_binary_without_preload_content(
997997
Test single binary in multipart mime
998998
999999
:param my_file:
1000-
:type my_file: bytearray
1000+
:type my_file: bytes
10011001
:param _request_timeout: timeout setting for this request. If one
10021002
number provided, it will be total request
10031003
timeout. It can also be a pair (tuple) of

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/api_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def response_deserialize(
306306
response_text = None
307307
return_data = None
308308
try:
309-
if response_type == "bytearray":
309+
if response_type in ("bytearray", "bytes"):
310310
return_data = response_data.data
311311
elif response_type == "file":
312312
return_data = self.__deserialize_file(response_data)

samples/client/echo_api/python/docs/BodyApi.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Method | HTTP request | Description
1717

1818

1919
# **test_binary_gif**
20-
> bytearray test_binary_gif()
20+
> bytes test_binary_gif()
2121
2222
Test binary (gif) response body
2323

@@ -60,7 +60,7 @@ This endpoint does not need any parameter.
6060

6161
### Return type
6262

63-
**bytearray**
63+
**bytes**
6464

6565
### Authorization
6666

@@ -105,7 +105,7 @@ configuration = openapi_client.Configuration(
105105
with openapi_client.ApiClient(configuration) as api_client:
106106
# Create an instance of the API class
107107
api_instance = openapi_client.BodyApi(api_client)
108-
body = None # bytearray | (optional)
108+
body = None # bytes | (optional)
109109

110110
try:
111111
# Test body parameter(s)
@@ -123,7 +123,7 @@ with openapi_client.ApiClient(configuration) as api_client:
123123

124124
Name | Type | Description | Notes
125125
------------- | ------------- | ------------- | -------------
126-
**body** | **bytearray**| | [optional]
126+
**body** | **bytes**| | [optional]
127127

128128
### Return type
129129

@@ -172,7 +172,7 @@ configuration = openapi_client.Configuration(
172172
with openapi_client.ApiClient(configuration) as api_client:
173173
# Create an instance of the API class
174174
api_instance = openapi_client.BodyApi(api_client)
175-
files = None # List[bytearray] |
175+
files = None # List[bytes] |
176176

177177
try:
178178
# Test array of binary in multipart mime
@@ -190,7 +190,7 @@ with openapi_client.ApiClient(configuration) as api_client:
190190

191191
Name | Type | Description | Notes
192192
------------- | ------------- | ------------- | -------------
193-
**files** | **List[bytearray]**| |
193+
**files** | **List[bytes]**| |
194194

195195
### Return type
196196

@@ -239,7 +239,7 @@ configuration = openapi_client.Configuration(
239239
with openapi_client.ApiClient(configuration) as api_client:
240240
# Create an instance of the API class
241241
api_instance = openapi_client.BodyApi(api_client)
242-
my_file = None # bytearray | (optional)
242+
my_file = None # bytes | (optional)
243243

244244
try:
245245
# Test single binary in multipart mime
@@ -257,7 +257,7 @@ with openapi_client.ApiClient(configuration) as api_client:
257257

258258
Name | Type | Description | Notes
259259
------------- | ------------- | ------------- | -------------
260-
**my_file** | **bytearray**| | [optional]
260+
**my_file** | **bytes**| | [optional]
261261

262262
### Return type
263263

0 commit comments

Comments
 (0)