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

Commit e55be49

Browse files
authored
Fixes issues 90, allow access to deserialized body for non 2XX statuses (#91)
* Template fix * Sample regenerated * Adds verification test, test_response_exception_info * Samples regerated
1 parent 8bf45d9 commit e55be49

File tree

473 files changed

+2539
-523
lines changed

Some content is hidden

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

473 files changed

+2539
-523
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,11 @@ class BaseApi(api_client.Api):
393393
{{/if}}
394394

395395
if not 200 <= response.status <= 299:
396-
raise exceptions.ApiException(api_response=api_response)
396+
raise exceptions.ApiException(
397+
status=response.status,
398+
reason=response.reason,
399+
api_response=api_response
400+
)
397401

398402
return api_response
399403

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# coding: utf-8
22

33
{{>partial_header}}
4+
import dataclasses
5+
import typing
6+
7+
from urllib3._collections import HTTPHeaderDict
48

59

610
class OpenApiException(Exception):
@@ -90,19 +94,26 @@ class ApiKeyError(OpenApiException, KeyError):
9094
super(ApiKeyError, self).__init__(full_msg)
9195

9296

93-
class ApiException(OpenApiException):
97+
T = typing.TypeVar("T")
9498

95-
def __init__(self, status=None, reason=None, api_response: '{{packageName}}.api_client.ApiResponse' = None):
96-
if api_response:
97-
self.status = api_response.response.status
98-
self.reason = api_response.response.reason
99-
self.body = api_response.response.data
100-
self.headers = api_response.response.getheaders()
101-
else:
102-
self.status = status
103-
self.reason = reason
104-
self.body = None
105-
self.headers = None
99+
100+
@dataclasses.dataclass
101+
class ApiException(OpenApiException, typing.Generic[T]):
102+
status: int
103+
reason: str
104+
api_response: typing.Optional[T] = None
105+
106+
@property
107+
def body(self) -> typing.Union[str, bytes, None]:
108+
if not self.api_response:
109+
return None
110+
return self.api_response.response.data
111+
112+
@property
113+
def headers(self) -> typing.Optional[HTTPHeaderDict]:
114+
if not self.api_response:
115+
return None
116+
return self.api_response.response.getheaders()
106117

107118
def __str__(self):
108119
"""Custom error messages for exception"""

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ paths:
655655
description: Success
656656
'404':
657657
description: Not found
658+
content:
659+
application/json:
660+
schema:
661+
type: object
658662
requestBody:
659663
content:
660664
application/x-www-form-urlencoded:

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/exceptions.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
The version of the OpenAPI document: 0.0.1
99
Generated by: https://openapi-generator.tech
1010
"""
11+
import dataclasses
12+
import typing
13+
14+
from urllib3._collections import HTTPHeaderDict
1115

1216

1317
class OpenApiException(Exception):
@@ -97,19 +101,26 @@ def __init__(self, msg, path_to_item=None):
97101
super(ApiKeyError, self).__init__(full_msg)
98102

99103

100-
class ApiException(OpenApiException):
104+
T = typing.TypeVar("T")
101105

102-
def __init__(self, status=None, reason=None, api_response: 'unit_test_api.api_client.ApiResponse' = None):
103-
if api_response:
104-
self.status = api_response.response.status
105-
self.reason = api_response.response.reason
106-
self.body = api_response.response.data
107-
self.headers = api_response.response.getheaders()
108-
else:
109-
self.status = status
110-
self.reason = reason
111-
self.body = None
112-
self.headers = None
106+
107+
@dataclasses.dataclass
108+
class ApiException(OpenApiException, typing.Generic[T]):
109+
status: int
110+
reason: str
111+
api_response: typing.Optional[T] = None
112+
113+
@property
114+
def body(self) -> typing.Union[str, bytes, None]:
115+
if not self.api_response:
116+
return None
117+
return self.api_response.response.data
118+
119+
@property
120+
def headers(self) -> typing.Optional[HTTPHeaderDict]:
121+
if not self.api_response:
122+
return None
123+
return self.api_response.response.getheaders()
113124

114125
def __str__(self):
115126
"""Custom error messages for exception"""

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_additionalproperties_allows_a_schema_which_should_validate_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_additionalproperties_allows_a_schema_which_should_validate_request_body/post.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class BaseApi(api_client.Api):
150150
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
151151

152152
if not 200 <= response.status <= 299:
153-
raise exceptions.ApiException(api_response=api_response)
153+
raise exceptions.ApiException(
154+
status=response.status,
155+
reason=response.reason,
156+
api_response=api_response
157+
)
154158

155159
return api_response
156160

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_additionalproperties_are_allowed_by_default_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_additionalproperties_are_allowed_by_default_request_body/post.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class BaseApi(api_client.Api):
150150
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
151151

152152
if not 200 <= response.status <= 299:
153-
raise exceptions.ApiException(api_response=api_response)
153+
raise exceptions.ApiException(
154+
status=response.status,
155+
reason=response.reason,
156+
api_response=api_response
157+
)
154158

155159
return api_response
156160

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_additionalproperties_can_exist_by_itself_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_additionalproperties_can_exist_by_itself_request_body/post.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class BaseApi(api_client.Api):
150150
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
151151

152152
if not 200 <= response.status <= 299:
153-
raise exceptions.ApiException(api_response=api_response)
153+
raise exceptions.ApiException(
154+
status=response.status,
155+
reason=response.reason,
156+
api_response=api_response
157+
)
154158

155159
return api_response
156160

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_additionalproperties_should_not_look_in_applicators_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_additionalproperties_should_not_look_in_applicators_request_body/post.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class BaseApi(api_client.Api):
150150
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
151151

152152
if not 200 <= response.status <= 299:
153-
raise exceptions.ApiException(api_response=api_response)
153+
raise exceptions.ApiException(
154+
status=response.status,
155+
reason=response.reason,
156+
api_response=api_response
157+
)
154158

155159
return api_response
156160

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_combined_with_anyof_oneof_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_combined_with_anyof_oneof_request_body/post.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class BaseApi(api_client.Api):
150150
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
151151

152152
if not 200 <= response.status <= 299:
153-
raise exceptions.ApiException(api_response=api_response)
153+
raise exceptions.ApiException(
154+
status=response.status,
155+
reason=response.reason,
156+
api_response=api_response
157+
)
154158

155159
return api_response
156160

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_request_body/post.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class BaseApi(api_client.Api):
150150
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
151151

152152
if not 200 <= response.status <= 299:
153-
raise exceptions.ApiException(api_response=api_response)
153+
raise exceptions.ApiException(
154+
status=response.status,
155+
reason=response.reason,
156+
api_response=api_response
157+
)
154158

155159
return api_response
156160

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_simple_types_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_simple_types_request_body/post.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class BaseApi(api_client.Api):
150150
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
151151

152152
if not 200 <= response.status <= 299:
153-
raise exceptions.ApiException(api_response=api_response)
153+
raise exceptions.ApiException(
154+
status=response.status,
155+
reason=response.reason,
156+
api_response=api_response
157+
)
154158

155159
return api_response
156160

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_with_base_schema_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_with_base_schema_request_body/post.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class BaseApi(api_client.Api):
150150
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
151151

152152
if not 200 <= response.status <= 299:
153-
raise exceptions.ApiException(api_response=api_response)
153+
raise exceptions.ApiException(
154+
status=response.status,
155+
reason=response.reason,
156+
api_response=api_response
157+
)
154158

155159
return api_response
156160

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_with_one_empty_schema_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_with_one_empty_schema_request_body/post.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class BaseApi(api_client.Api):
150150
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
151151

152152
if not 200 <= response.status <= 299:
153-
raise exceptions.ApiException(api_response=api_response)
153+
raise exceptions.ApiException(
154+
status=response.status,
155+
reason=response.reason,
156+
api_response=api_response
157+
)
154158

155159
return api_response
156160

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_with_the_first_empty_schema_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_with_the_first_empty_schema_request_body/post.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class BaseApi(api_client.Api):
150150
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
151151

152152
if not 200 <= response.status <= 299:
153-
raise exceptions.ApiException(api_response=api_response)
153+
raise exceptions.ApiException(
154+
status=response.status,
155+
reason=response.reason,
156+
api_response=api_response
157+
)
154158

155159
return api_response
156160

samples/openapi3/client/3_0_3_unit_test/python/unit_test_api/paths/request_body_post_allof_with_the_last_empty_schema_request_body/post.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class instances
155155
api_response = api_client.ApiResponseWithoutDeserialization(response=response)
156156

157157
if not 200 <= response.status <= 299:
158-
raise exceptions.ApiException(api_response=api_response)
158+
raise exceptions.ApiException(
159+
status=response.status,
160+
reason=response.reason,
161+
api_response=api_response
162+
)
159163

160164
return api_response
161165

0 commit comments

Comments
 (0)