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

Commit 55875b1

Browse files
authored
v2.0.0 adds request_body module (#46)
* Java and template updates for separate request body module * Samples updated * Samples regenerated
1 parent e76fe9f commit 55875b1

File tree

500 files changed

+11618
-10932
lines changed

Some content is hidden

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

500 files changed

+11618
-10932
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,17 @@ protected void generateEndpoints(OperationsMap objs) {
578578
outputFilename = packageFilename(Arrays.asList("paths", pathModuleName, co.httpMethod, "__init__.py"));
579579
pathsFiles.add(Arrays.asList(endpointMap, "endpoint.handlebars", outputFilename));
580580

581+
// paths.some_path.post.request_body.py
582+
if (co.bodyParam != null) {
583+
Map<String, Object> paramMap = new HashMap<>();
584+
paramMap.put("requestBody", co.bodyParam);
585+
// TODO consolidate imports into body param only
586+
paramMap.put("imports", co.imports);
587+
paramMap.put("packageName", packageName);
588+
outputFilename = packageFilename(Arrays.asList("paths", pathModuleName, co.httpMethod, "request_body.py"));
589+
pathsFiles.add(Arrays.asList(paramMap, "endpoint_request_body.handlebars", outputFilename));
590+
}
591+
581592
for (CodegenResponse response: co.responses) {
582593
// paths.some_path.post.response_for_200.py (file per response)
583594
Map<String, Object> responseMap = new HashMap<>();

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Method | HTTP request | Description
4949
Name | Type | Description | Notes
5050
------------- | ------------- | ------------- | -------------
5151
{{#with bodyParam}}
52-
[{{baseName}}](#{{operationId}}.RequestBody) | typing.Union[{{#each content}}{{#unless @first}}, {{/unless}}{{#with this.schema}}[RequestBody.Schemas.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}}](#{{../operationId}}.RequestBody.Schemas.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}}){{/with}}{{/each}}{{#unless required}}, Unset]{{else}}]{{/unless}} | {{#if required}}required{{else}}optional, default is unset{{/if}} |
52+
[{{baseName}}](#{{operationId}}.request_body) | typing.Union[{{#each content}}{{#unless @first}}, {{/unless}}{{#with this.schema}}[request_body.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}}](#{{../operationId}}.request_body.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}}){{/with}}{{/each}}{{#unless required}}, Unset]{{else}}]{{/unless}} | {{#if required}}required{{else}}optional, default is unset{{/if}} |
5353
{{/with}}
5454
{{#if queryParams}}
5555
[query_params](#{{operationId}}.RequestQueryParameters) | [RequestQueryParameters.Params](#{{operationId}}.RequestQueryParameters.Params) | |
@@ -81,10 +81,10 @@ timeout | typing.Optional[typing.Union[int, typing.Tuple]] | default is None | t
8181
skip_deserialization | bool | default is False | when True, headers and body will be unset and an instance of api_client.ApiResponseWithoutDeserialization will be returned
8282
{{#with bodyParam}}
8383
84-
### <a id="{{operationId}}.RequestBody" >body</a>
84+
### <a id="{{operationId}}.request_body" >body</a>
8585
{{#each content}}
8686
{{#with this.schema}}
87-
{{> api_doc_schema_type_hint anchorPrefix=../operationId schemaNamePrefix1="RequestBody.Schemas." complexTypePrefix="../../models/" }}
87+
{{> api_doc_schema_type_hint anchorPrefix=../operationId schemaNamePrefix1="request_body." complexTypePrefix="../../models/" }}
8888
{{/with}}
8989
{{/each}}
9090
{{/with}}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Test{{operationIdSnakeCase}}(ApiTestMixin, unittest.TestCase):
109109
{{/with}}
110110
)
111111
{{#if valid}}
112-
body = {{httpMethod}}.RequestBody.Schemas.{{#if schema.nameInSnakeCase}}{{schema.name}}{{else}}{{schema.baseName}}{{/if}}.from_openapi_data_oapg(
112+
body = {{httpMethod}}.request_body.{{#if schema.nameInSnakeCase}}{{schema.name}}{{else}}{{schema.baseName}}{{/if}}.from_openapi_data_oapg(
113113
payload,
114114
_configuration=self._configuration
115115
)
@@ -123,7 +123,7 @@ class Test{{operationIdSnakeCase}}(ApiTestMixin, unittest.TestCase):
123123
assert isinstance(api_response.body, schemas.Unset)
124124
{{else}}
125125
with self.assertRaises(({{packageName}}.ApiValueError, {{packageName}}.ApiTypeError)):
126-
body = {{httpMethod}}.RequestBody.Schemas.{{#if schema.nameInSnakeCase}}{{schema.name}}{{else}}{{schema.baseName}}{{/if}}.from_openapi_data_oapg(
126+
body = {{httpMethod}}.request_body.{{#if schema.nameInSnakeCase}}{{schema.name}}{{else}}{{schema.baseName}}{{/if}}.from_openapi_data_oapg(
127127
payload,
128128
_configuration=self._configuration
129129
)

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

+3-32
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ from .. import path
2222
{{#each responses}}
2323
from . import response_for_{{#if isDefault}}default{{else}}{{code}}{{/if}}
2424
{{/each}}
25+
{{#if bodyParam}}
26+
from . import request_body
27+
{{/if}}
2528

2629
{{#or queryParams headerParams pathParams cookieParams}}
2730
{{#if queryParams}}
@@ -37,38 +40,6 @@ from . import response_for_{{#if isDefault}}default{{else}}{{code}}{{/if}}
3740
{{> endpoint_parameter_schema_and_def xParams=cookieParams xParamsName="RequestCookieParameters" }}
3841
{{/if}}
3942
{{/or}}
40-
{{#if bodyParam}}
41-
{{#with bodyParam}}
42-
43-
44-
class RequestBody:
45-
class Schemas:
46-
{{#each content}}
47-
{{#with this}}
48-
{{#with schema}}
49-
{{> model_templates/schema }}
50-
{{/with}}
51-
{{/with}}
52-
{{/each}}
53-
54-
parameter = api_client.RequestBody(
55-
content={
56-
{{#each content}}
57-
'{{{@key}}}': api_client.MediaType(
58-
{{#with this}}
59-
{{#with schema}}
60-
schema=Schemas.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}}
61-
{{/with}}
62-
{{/with}}
63-
),
64-
{{/each}}
65-
},
66-
{{#if required}}
67-
required=True,
68-
{{/if}}
69-
)
70-
{{/with}}
71-
{{/if}}
7243
{{#unless isStub}}
7344
{{#each authMethods}}
7445
{{#if @first}}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
{{#if bodyParam.required}}
44
{{#with bodyParam}}
55
{{#eq ../contentType "null"}}
6-
body: typing.Union[{{#each getContent}}{{#with this.schema}}RequestBody.Schemas.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}},{{> model_templates/schema_python_types }}{{/with}}{{/each}}],
6+
body: typing.Union[{{#each getContent}}{{#with this.schema}}request_body.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}},{{> model_templates/schema_python_types }}{{/with}}{{/each}}],
77
{{else}}
8-
body: typing.Union[{{#each getContent}}{{#eq @key ../../contentType }}{{#with this.schema}}RequestBody.Schemas.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}},{{> model_templates/schema_python_types }}{{/with}}{{/eq}}{{/each}}],
8+
body: typing.Union[{{#each getContent}}{{#eq @key ../../contentType }}{{#with this.schema}}request_body.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}},{{> model_templates/schema_python_types }}{{/with}}{{/eq}}{{/each}}],
99
{{/eq}}
1010
{{/with}}
1111
{{#if isOverload}}
@@ -67,9 +67,9 @@
6767
{{/if}}
6868
{{#with bodyParam}}
6969
{{#eq ../contentType "null"}}
70-
body: typing.Union[{{#each getContent}}{{#with this.schema}}RequestBody.Schemas.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}}, {{> model_templates/schema_python_types }}{{/with}}{{/each}}schemas.Unset] = schemas.unset,
70+
body: typing.Union[{{#each getContent}}{{#with this.schema}}request_body.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}}, {{> model_templates/schema_python_types }}{{/with}}{{/each}}schemas.Unset] = schemas.unset,
7171
{{else}}
72-
body: typing.Union[{{#each getContent}}{{#eq @key ../../contentType }}{{#with this.schema}}RequestBody.Schemas.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}}, {{> model_templates/schema_python_types }}{{/with}}{{/eq}}{{/each}}schemas.Unset] = schemas.unset,
72+
body: typing.Union[{{#each getContent}}{{#eq @key ../../contentType }}{{#with this.schema}}request_body.{{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}}, {{> model_templates/schema_python_types }}{{/with}}{{/eq}}{{/each}}schemas.Unset] = schemas.unset,
7373
{{/eq}}
7474
{{/with}}
7575
{{/if}}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
serialized_data = RequestBody.parameter.serialize(body, content_type)
1+
serialized_data = request_body.parameter_oapg.serialize(body, content_type)
22
_headers.add('Content-Type', content_type)
33
if 'fields' in serialized_data:
44
_fields = serialized_data['fields']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# coding: utf-8
2+
3+
{{>partial_header}}
4+
5+
from dataclasses import dataclass
6+
import typing_extensions
7+
import urllib3
8+
9+
from {{packageName}} import api_client, exceptions
10+
{{> model_templates/imports_schema_types }}
11+
{{> model_templates/imports_schemas }}
12+
{{#with requestBody}}
13+
14+
15+
{{#each content}}
16+
{{#with this}}
17+
{{#with schema}}
18+
{{> model_templates/schema }}
19+
{{/with}}
20+
{{/with}}
21+
{{/each}}
22+
23+
parameter_oapg = api_client.RequestBody(
24+
content={
25+
{{#each content}}
26+
'{{{@key}}}': api_client.MediaType(
27+
{{#with this}}
28+
{{#with schema}}
29+
schema={{#if nameInSnakeCase}}{{name}}{{else}}{{baseName}}{{/if}}
30+
{{/with}}
31+
{{/with}}
32+
),
33+
{{/each}}
34+
},
35+
{{#if required}}
36+
required=True,
37+
{{/if}}
38+
)
39+
{{/with}}

samples/openapi3/client/3_0_3_unit_test/python/docs/apis/tags/AdditionalPropertiesApi.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ with unit_test_api.ApiClient(configuration) as api_client:
5252

5353
Name | Type | Description | Notes
5454
------------- | ------------- | ------------- | -------------
55-
[body](#post_additionalproperties_allows_a_schema_which_should_validate_request_body.RequestBody) | typing.Union[[RequestBody.Schemas.application_json](#post_additionalproperties_allows_a_schema_which_should_validate_request_body.RequestBody.Schemas.application_json)] | required |
55+
[body](#post_additionalproperties_allows_a_schema_which_should_validate_request_body.request_body) | typing.Union[[request_body.application_json](#post_additionalproperties_allows_a_schema_which_should_validate_request_body.request_body.application_json)] | required |
5656
content_type | str | optional, default is 'application/json' | Selects the schema and serialization of the request body
5757
stream | bool | default is False | if True then the response.content will be streamed and loaded from a file like object. When downloading a file, set this to True to force the code to deserialize the content to a FileSchema file
5858
timeout | typing.Optional[typing.Union[int, typing.Tuple]] | default is None | the timeout used by the rest client
5959
skip_deserialization | bool | default is False | when True, headers and body will be unset and an instance of api_client.ApiResponseWithoutDeserialization will be returned
6060

61-
### <a id="post_additionalproperties_allows_a_schema_which_should_validate_request_body.RequestBody" >body</a>
61+
### <a id="post_additionalproperties_allows_a_schema_which_should_validate_request_body.request_body" >body</a>
6262

63-
# <a id="post_additionalproperties_allows_a_schema_which_should_validate_request_body.RequestBody.Schemas.application_json" >RequestBody.Schemas.application_json</a>
63+
# <a id="post_additionalproperties_allows_a_schema_which_should_validate_request_body.request_body.application_json" >request_body.application_json</a>
6464
Type | Description | Notes
6565
------------- | ------------- | -------------
6666
[**AdditionalpropertiesAllowsASchemaWhichShouldValidate**](../../models/AdditionalpropertiesAllowsASchemaWhichShouldValidate.md) | |
@@ -178,15 +178,15 @@ with unit_test_api.ApiClient(configuration) as api_client:
178178

179179
Name | Type | Description | Notes
180180
------------- | ------------- | ------------- | -------------
181-
[body](#post_additionalproperties_are_allowed_by_default_request_body.RequestBody) | typing.Union[[RequestBody.Schemas.application_json](#post_additionalproperties_are_allowed_by_default_request_body.RequestBody.Schemas.application_json)] | required |
181+
[body](#post_additionalproperties_are_allowed_by_default_request_body.request_body) | typing.Union[[request_body.application_json](#post_additionalproperties_are_allowed_by_default_request_body.request_body.application_json)] | required |
182182
content_type | str | optional, default is 'application/json' | Selects the schema and serialization of the request body
183183
stream | bool | default is False | if True then the response.content will be streamed and loaded from a file like object. When downloading a file, set this to True to force the code to deserialize the content to a FileSchema file
184184
timeout | typing.Optional[typing.Union[int, typing.Tuple]] | default is None | the timeout used by the rest client
185185
skip_deserialization | bool | default is False | when True, headers and body will be unset and an instance of api_client.ApiResponseWithoutDeserialization will be returned
186186

187-
### <a id="post_additionalproperties_are_allowed_by_default_request_body.RequestBody" >body</a>
187+
### <a id="post_additionalproperties_are_allowed_by_default_request_body.request_body" >body</a>
188188

189-
# <a id="post_additionalproperties_are_allowed_by_default_request_body.RequestBody.Schemas.application_json" >RequestBody.Schemas.application_json</a>
189+
# <a id="post_additionalproperties_are_allowed_by_default_request_body.request_body.application_json" >request_body.application_json</a>
190190
Type | Description | Notes
191191
------------- | ------------- | -------------
192192
[**AdditionalpropertiesAreAllowedByDefault**](../../models/AdditionalpropertiesAreAllowedByDefault.md) | |
@@ -306,15 +306,15 @@ with unit_test_api.ApiClient(configuration) as api_client:
306306

307307
Name | Type | Description | Notes
308308
------------- | ------------- | ------------- | -------------
309-
[body](#post_additionalproperties_can_exist_by_itself_request_body.RequestBody) | typing.Union[[RequestBody.Schemas.application_json](#post_additionalproperties_can_exist_by_itself_request_body.RequestBody.Schemas.application_json)] | required |
309+
[body](#post_additionalproperties_can_exist_by_itself_request_body.request_body) | typing.Union[[request_body.application_json](#post_additionalproperties_can_exist_by_itself_request_body.request_body.application_json)] | required |
310310
content_type | str | optional, default is 'application/json' | Selects the schema and serialization of the request body
311311
stream | bool | default is False | if True then the response.content will be streamed and loaded from a file like object. When downloading a file, set this to True to force the code to deserialize the content to a FileSchema file
312312
timeout | typing.Optional[typing.Union[int, typing.Tuple]] | default is None | the timeout used by the rest client
313313
skip_deserialization | bool | default is False | when True, headers and body will be unset and an instance of api_client.ApiResponseWithoutDeserialization will be returned
314314

315-
### <a id="post_additionalproperties_can_exist_by_itself_request_body.RequestBody" >body</a>
315+
### <a id="post_additionalproperties_can_exist_by_itself_request_body.request_body" >body</a>
316316

317-
# <a id="post_additionalproperties_can_exist_by_itself_request_body.RequestBody.Schemas.application_json" >RequestBody.Schemas.application_json</a>
317+
# <a id="post_additionalproperties_can_exist_by_itself_request_body.request_body.application_json" >request_body.application_json</a>
318318
Type | Description | Notes
319319
------------- | ------------- | -------------
320320
[**AdditionalpropertiesCanExistByItself**](../../models/AdditionalpropertiesCanExistByItself.md) | |
@@ -432,15 +432,15 @@ with unit_test_api.ApiClient(configuration) as api_client:
432432

433433
Name | Type | Description | Notes
434434
------------- | ------------- | ------------- | -------------
435-
[body](#post_additionalproperties_should_not_look_in_applicators_request_body.RequestBody) | typing.Union[[RequestBody.Schemas.application_json](#post_additionalproperties_should_not_look_in_applicators_request_body.RequestBody.Schemas.application_json)] | required |
435+
[body](#post_additionalproperties_should_not_look_in_applicators_request_body.request_body) | typing.Union[[request_body.application_json](#post_additionalproperties_should_not_look_in_applicators_request_body.request_body.application_json)] | required |
436436
content_type | str | optional, default is 'application/json' | Selects the schema and serialization of the request body
437437
stream | bool | default is False | if True then the response.content will be streamed and loaded from a file like object. When downloading a file, set this to True to force the code to deserialize the content to a FileSchema file
438438
timeout | typing.Optional[typing.Union[int, typing.Tuple]] | default is None | the timeout used by the rest client
439439
skip_deserialization | bool | default is False | when True, headers and body will be unset and an instance of api_client.ApiResponseWithoutDeserialization will be returned
440440

441-
### <a id="post_additionalproperties_should_not_look_in_applicators_request_body.RequestBody" >body</a>
441+
### <a id="post_additionalproperties_should_not_look_in_applicators_request_body.request_body" >body</a>
442442

443-
# <a id="post_additionalproperties_should_not_look_in_applicators_request_body.RequestBody.Schemas.application_json" >RequestBody.Schemas.application_json</a>
443+
# <a id="post_additionalproperties_should_not_look_in_applicators_request_body.request_body.application_json" >request_body.application_json</a>
444444
Type | Description | Notes
445445
------------- | ------------- | -------------
446446
[**AdditionalpropertiesShouldNotLookInApplicators**](../../models/AdditionalpropertiesShouldNotLookInApplicators.md) | |

0 commit comments

Comments
 (0)