Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit cfc9993

Browse files
committed
Adds and uses getContentTypeToOperation
1 parent 8f9b901 commit cfc9993

File tree

117 files changed

+1921
-1859
lines changed

Some content is hidden

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

117 files changed

+1921
-1859
lines changed

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ public boolean getAllResponsesAreErrors() {
197197
return responses.stream().allMatch(response -> response.is4xx || response.is5xx);
198198
}
199199

200+
/**
201+
* @return contentTypeToOperation
202+
* returns a mof where the key is the request body content type and the value is the current CodegenOperation
203+
* this is needed by templates when a different signature is needed for each request body content type
204+
*/
205+
public Map<String, CodegenOperation> getContentTypeToOperation() {
206+
LinkedHashMap<String, CodegenOperation> contentTypeToOperation = new LinkedHashMap<>();
207+
if (bodyParam == null) {
208+
return null;
209+
}
210+
LinkedHashMap<String, CodegenMediaType> content = bodyParam.getContent();
211+
for (String contentType: content.keySet()) {
212+
contentTypeToOperation.put(contentType, this);
213+
}
214+
return contentTypeToOperation;
215+
}
216+
200217
/**
201218
* Check if there's at least one vendor extension
202219
*

modules/openapi-json-schema-generator/src/main/resources/python/endpoint.handlebars

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -373,21 +373,26 @@ _all_accept_content_types = (
373373

374374

375375
class BaseApi(api_client.Api):
376-
376+
{{#if bodyParam}}
377+
{{#each getContentTypeToOperation}}
378+
{{> endpoint_args_wrapper contentType=@key}}
379+
{{/each}}
380+
{{else}}
377381
@typing.overload
378382
def _{{operationId}}_oapg(
379-
{{> endpoint_args isOverload=true skipDeserialization="False"}}
383+
{{> endpoint_args isOverload=true skipDeserialization="False" contentType="null"}}
384+
{{/if}}
380385

381386
@typing.overload
382387
def _{{operationId}}_oapg(
383-
{{> endpoint_args isOverload=true skipDeserialization="True"}}
388+
{{> endpoint_args isOverload=true skipDeserialization="True" contentType="null"}}
384389

385390
@typing.overload
386391
def _{{operationId}}_oapg(
387-
{{> endpoint_args isOverload=true skipDeserialization="null"}}
392+
{{> endpoint_args isOverload=true skipDeserialization="null" contentType="null"}}
388393

389394
def _{{operationId}}_oapg(
390-
{{> endpoint_args isOverload=false skipDeserialization="null"}}
395+
{{> endpoint_args isOverload=false skipDeserialization="null" contentType="null"}}
391396
"""
392397
{{#if summary}}
393398
{{summary}}
@@ -543,10 +548,10 @@ class {{operationIdCamelCase}}(BaseApi):
543548

544549
@typing.overload
545550
def {{operationId}}(
546-
{{> endpoint_args isOverload=true skipDeserialization="null"}}
551+
{{> endpoint_args isOverload=true skipDeserialization="null" contentType="null"}}
547552

548553
def {{operationId}}(
549-
{{> endpoint_args isOverload=false skipDeserialization="null"}}
554+
{{> endpoint_args isOverload=false skipDeserialization="null" contentType="null"}}
550555
return self._{{operationId}}_oapg(
551556
{{> endpoint_args_passed }}
552557
)
@@ -561,14 +566,14 @@ class ApiFor{{httpMethod}}(BaseApi):
561566

562567
@typing.overload
563568
def {{httpMethod}}(
564-
{{> endpoint_args isOverload=true skipDeserialization="True"}}
569+
{{> endpoint_args isOverload=true skipDeserialization="True" contentType="null"}}
565570

566571
@typing.overload
567572
def {{httpMethod}}(
568-
{{> endpoint_args isOverload=true skipDeserialization="null"}}
573+
{{> endpoint_args isOverload=true skipDeserialization="null" contentType="null"}}
569574

570575
def {{httpMethod}}(
571-
{{> endpoint_args isOverload=false skipDeserialization="null"}}
576+
{{> endpoint_args isOverload=false skipDeserialization="null" contentType="null"}}
572577
return self._{{operationId}}_oapg(
573578
{{> endpoint_args_passed }}
574579
)

modules/openapi-json-schema-generator/src/main/resources/python/endpoint_args.handlebars

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{#if bodyParam}}
33
{{#if bodyParam.required}}
44
{{#with bodyParam}}
5-
body: typing.Union[{{#each content}}{{#with this.schema}}{{baseName}},{{> model_templates/schema_python_types }}{{/with}}{{/each}}],
5+
body: typing.Union[{{#each content}}{{#eq key contentType }}{{#with this.schema}}{{baseName}},{{> model_templates/schema_python_types }}{{/with}}{{/eq}}{{/each}}],
66
{{/with}}
77
{{#if isOverload}}
88
{{#eq skipDeserialization "True"}}
@@ -16,7 +16,7 @@
1616
{{/eq}}
1717
{{/if}}
1818
{{#with bodyParam}}
19-
body: typing.Union[{{#each content}}{{#with this.schema}}{{baseName}}, {{> model_templates/schema_python_types }}{{/with}}{{/each}}schemas.Unset] = schemas.unset,
19+
body: typing.Union[{{#each content}}{{#eq key contentType }}{{#with this.schema}}{{baseName}}, {{> model_templates/schema_python_types }}{{/with}}{{/eq}}{{/each}}schemas.Unset] = schemas.unset,
2020
{{/with}}
2121
{{/if}}
2222
{{/if}}
@@ -32,12 +32,12 @@
3232
{{#if cookieParams}}
3333
cookie_params: RequestCookieParams = frozendict.frozendict(),
3434
{{/if}}
35-
{{#with bodyParam}}
36-
{{#each content}}
37-
{{#if @first}}
38-
content_type: str = '{{{@key}}}',
39-
{{/if}}
40-
{{/each}}
35+
{{#if bodyParam}}
36+
{{#if isOverload}}
37+
{{else}}
38+
content_type: typing_extensions.Literal["{{contentType}}"] = '{{contentType}}',
39+
{{/if}}
40+
content_type: typing_extensions.Literal["{{contentType}}"] = '{{contentType}}',
4141
{{/with}}
4242
{{#if produces}}
4343
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
contentType="{{{contentType}}}"
2+
@typing.overload
3+
{{#with this}}
4+
def _{{operationId}}_oapg(
5+
{{> endpoint_args isOverload=true skipDeserialization="False" contentType="{{{contentType}}}"}}
6+
{{/with}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.9.9
1+
unset

samples/openapi3/client/petstore/python/petstore_api/paths/another_fake_dummy/patch.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ class ApiResponseFor200(api_client.ApiResponse):
6868

6969

7070
class BaseApi(api_client.Api):
71-
71+
contentType="application/json"
7272
@typing.overload
7373
def _call_123_test_special_tags_oapg(
7474
self,
75-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
76-
content_type: str = 'application/json',
75+
body: typing.Union[],
76+
content_type: typing_extensions.Literal[""] = '',
7777
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
7878
stream: bool = False,
7979
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -85,9 +85,9 @@ def _call_123_test_special_tags_oapg(
8585
@typing.overload
8686
def _call_123_test_special_tags_oapg(
8787
self,
88-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
88+
body: typing.Union[],
8989
skip_deserialization: typing_extensions.Literal[True],
90-
content_type: str = 'application/json',
90+
content_type: typing_extensions.Literal[""] = '',
9191
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
9292
stream: bool = False,
9393
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -96,8 +96,8 @@ def _call_123_test_special_tags_oapg(
9696
@typing.overload
9797
def _call_123_test_special_tags_oapg(
9898
self,
99-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
100-
content_type: str = 'application/json',
99+
body: typing.Union[],
100+
content_type: typing_extensions.Literal[""] = '',
101101
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
102102
stream: bool = False,
103103
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -109,8 +109,8 @@ def _call_123_test_special_tags_oapg(
109109

110110
def _call_123_test_special_tags_oapg(
111111
self,
112-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
113-
content_type: str = 'application/json',
112+
body: typing.Union[],
113+
content_type: typing_extensions.Literal[""] = '',
114114
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
115115
stream: bool = False,
116116
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -172,8 +172,8 @@ class Call123TestSpecialTags(BaseApi):
172172
@typing.overload
173173
def call_123_test_special_tags(
174174
self,
175-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
176-
content_type: str = 'application/json',
175+
body: typing.Union[],
176+
content_type: typing_extensions.Literal[""] = '',
177177
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
178178
stream: bool = False,
179179
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -185,9 +185,9 @@ def call_123_test_special_tags(
185185
@typing.overload
186186
def call_123_test_special_tags(
187187
self,
188-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
188+
body: typing.Union[],
189189
skip_deserialization: typing_extensions.Literal[True],
190-
content_type: str = 'application/json',
190+
content_type: typing_extensions.Literal[""] = '',
191191
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
192192
stream: bool = False,
193193
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -196,8 +196,8 @@ def call_123_test_special_tags(
196196
@typing.overload
197197
def call_123_test_special_tags(
198198
self,
199-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
200-
content_type: str = 'application/json',
199+
body: typing.Union[],
200+
content_type: typing_extensions.Literal[""] = '',
201201
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
202202
stream: bool = False,
203203
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -209,8 +209,8 @@ def call_123_test_special_tags(
209209

210210
def call_123_test_special_tags(
211211
self,
212-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
213-
content_type: str = 'application/json',
212+
body: typing.Union[],
213+
content_type: typing_extensions.Literal[""] = '',
214214
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
215215
stream: bool = False,
216216
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -232,8 +232,8 @@ class ApiForpatch(BaseApi):
232232
@typing.overload
233233
def patch(
234234
self,
235-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
236-
content_type: str = 'application/json',
235+
body: typing.Union[],
236+
content_type: typing_extensions.Literal[""] = '',
237237
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
238238
stream: bool = False,
239239
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -245,9 +245,9 @@ def patch(
245245
@typing.overload
246246
def patch(
247247
self,
248-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
248+
body: typing.Union[],
249249
skip_deserialization: typing_extensions.Literal[True],
250-
content_type: str = 'application/json',
250+
content_type: typing_extensions.Literal[""] = '',
251251
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
252252
stream: bool = False,
253253
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -256,8 +256,8 @@ def patch(
256256
@typing.overload
257257
def patch(
258258
self,
259-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
260-
content_type: str = 'application/json',
259+
body: typing.Union[],
260+
content_type: typing_extensions.Literal[""] = '',
261261
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
262262
stream: bool = False,
263263
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,
@@ -269,8 +269,8 @@ def patch(
269269

270270
def patch(
271271
self,
272-
body: typing.Union[SchemaForRequestBodyApplicationJson,],
273-
content_type: str = 'application/json',
272+
body: typing.Union[],
273+
content_type: typing_extensions.Literal[""] = '',
274274
accept_content_types: typing.Tuple[str] = _all_accept_content_types,
275275
stream: bool = False,
276276
timeout: typing.Optional[typing.Union[int, typing.Tuple]] = None,

0 commit comments

Comments
 (0)