Skip to content

Commit 87fb1f4

Browse files
committed
Rework _prepare_*_doc callbacks
1 parent 85bc74f commit 87fb1f4

3 files changed

Lines changed: 57 additions & 59 deletions

File tree

flask_smorest/arguments.py

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -94,55 +94,54 @@ def wrapper(*f_args, **f_kwargs):
9494
def _prepare_arguments_doc(self, doc, doc_info, spec, **kwargs):
9595
# This callback should run first as it overrides existing parameters
9696
# in doc. Following callbacks should append to parameters list.
97-
operation = doc_info.get('arguments', {})
98-
99-
parameters = [
100-
p for p in operation.get('parameters', [])
101-
if isinstance(p, abc.Mapping)
102-
]
103-
104-
# OAS 2
105-
if spec.openapi_version.major < 3:
106-
for param in parameters:
107-
if param['in'] in (
108-
self.DEFAULT_LOCATION_CONTENT_TYPE_MAPPING
109-
):
110-
content_type = (
111-
param.pop('content_type', None) or
112-
self.DEFAULT_LOCATION_CONTENT_TYPE_MAPPING[
113-
param['in']]
114-
)
115-
if content_type != DEFAULT_REQUEST_BODY_CONTENT_TYPE:
116-
operation['consumes'] = [content_type, ]
117-
# body and formData are mutually exclusive
118-
break
119-
# OAS 3
120-
else:
121-
for param in parameters:
122-
if param['in'] in (
123-
self.DEFAULT_LOCATION_CONTENT_TYPE_MAPPING
124-
):
125-
request_body = {
126-
x: param[x]
127-
for x in ('description', 'required')
128-
if x in param
129-
}
130-
fields = {
131-
x: param.pop(x)
132-
for x in ('schema', 'example', 'examples')
133-
if x in param
134-
}
135-
content_type = (
136-
param.pop('content_type', None) or
137-
self.DEFAULT_LOCATION_CONTENT_TYPE_MAPPING[
138-
param['in']]
139-
)
140-
request_body['content'] = {content_type: fields}
141-
operation['requestBody'] = request_body
142-
# There can be only one requestBody
143-
operation['parameters'].remove(param)
144-
if not operation['parameters']:
145-
del operation['parameters']
146-
break
147-
doc = deepupdate(doc, operation)
97+
operation = doc_info.get('arguments')
98+
if operation:
99+
parameters = [
100+
p for p in operation['parameters']
101+
if isinstance(p, abc.Mapping)
102+
]
103+
# OAS 2
104+
if spec.openapi_version.major < 3:
105+
for param in parameters:
106+
if param['in'] in (
107+
self.DEFAULT_LOCATION_CONTENT_TYPE_MAPPING
108+
):
109+
content_type = (
110+
param.pop('content_type', None) or
111+
self.DEFAULT_LOCATION_CONTENT_TYPE_MAPPING[
112+
param['in']]
113+
)
114+
if content_type != DEFAULT_REQUEST_BODY_CONTENT_TYPE:
115+
operation['consumes'] = [content_type, ]
116+
# body and formData are mutually exclusive
117+
break
118+
# OAS 3
119+
else:
120+
for param in parameters:
121+
if param['in'] in (
122+
self.DEFAULT_LOCATION_CONTENT_TYPE_MAPPING
123+
):
124+
request_body = {
125+
x: param[x]
126+
for x in ('description', 'required')
127+
if x in param
128+
}
129+
fields = {
130+
x: param.pop(x)
131+
for x in ('schema', 'example', 'examples')
132+
if x in param
133+
}
134+
content_type = (
135+
param.pop('content_type', None) or
136+
self.DEFAULT_LOCATION_CONTENT_TYPE_MAPPING[
137+
param['in']]
138+
)
139+
request_body['content'] = {content_type: fields}
140+
operation['requestBody'] = request_body
141+
# There can be only one requestBody
142+
operation['parameters'].remove(param)
143+
if not operation['parameters']:
144+
del operation['parameters']
145+
break
146+
doc = deepupdate(doc, operation)
148147
return doc

flask_smorest/pagination.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,8 @@ def _document_pagination_metadata(self, spec, resp_doc):
288288
def _prepare_pagination_doc(self, doc, doc_info, spec, **kwargs):
289289
operation = doc_info.get('pagination')
290290
if operation:
291-
parameters = operation.get('parameters')
292-
doc.setdefault('parameters', []).append(parameters)
293-
response = operation.get('response')
294-
doc.setdefault('responses', {}).update(response)
291+
doc.setdefault('parameters', []).append(operation['parameters'])
292+
doc.setdefault('responses', {}).update(operation['response'])
295293
success_status_code = doc_info.get('success_status_code')
296294
if success_status_code is not None:
297295
self._document_pagination_metadata(

flask_smorest/response.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ def _prepare_response_content(data):
158158

159159
@staticmethod
160160
def _prepare_response_doc(doc, doc_info, spec, **kwargs):
161-
operation = doc_info.get('response', {})
162-
for response in operation.get('responses', {}).values():
163-
prepare_response(response, spec, DEFAULT_RESPONSE_CONTENT_TYPE)
164-
doc = deepupdate(doc, operation)
161+
operation = doc_info.get('response')
162+
if operation:
163+
for response in operation['responses'].values():
164+
prepare_response(response, spec, DEFAULT_RESPONSE_CONTENT_TYPE)
165+
doc = deepupdate(doc, operation)
165166
return doc

0 commit comments

Comments
 (0)