|
16 | 16 | import typing as ty |
17 | 17 |
|
18 | 18 | import flask |
| 19 | +from oslo_log import log |
19 | 20 | from oslo_serialization import jsonutils |
20 | 21 |
|
21 | 22 | from keystone.api.validation import validators |
| 23 | +import keystone.conf |
| 24 | +from keystone import exception |
| 25 | + |
| 26 | +CONF = keystone.conf.CONF |
| 27 | +LOG = log.getLogger(__name__) |
22 | 28 |
|
23 | 29 |
|
24 | 30 | def validated(cls): |
@@ -138,26 +144,40 @@ def add_validator(func): |
138 | 144 | def wrapper(*args, **kwargs): |
139 | 145 | response = func(*args, **kwargs) |
140 | 146 |
|
141 | | - if schema is not None: |
142 | | - # In Flask it is not uncommon that the method return a tuple of |
143 | | - # body and the status code. In the runtime Keystone only return |
144 | | - # a body, but some of the used testtools do return a tuple. |
145 | | - if isinstance(response, tuple): |
146 | | - _body = response[0] |
147 | | - else: |
148 | | - _body = response |
149 | | - |
150 | | - # NOTE(stephenfin): If our response is an object, we need to |
151 | | - # serializer and deserialize to convert e.g. date-time |
152 | | - # to strings |
153 | | - _body = jsonutils.dump_as_bytes(_body) |
154 | | - |
155 | | - if _body == b"": |
156 | | - body = None |
| 147 | + if CONF.api.response_validation == 'ignore': |
| 148 | + # don't waste our time checking anything if we're ignoring |
| 149 | + # schema errors |
| 150 | + return response |
| 151 | + |
| 152 | + if schema is None: |
| 153 | + return response |
| 154 | + |
| 155 | + # In Flask it is not uncommon that the method return a tuple of |
| 156 | + # body and the status code. In the runtime Keystone only return |
| 157 | + # a body, but some of the used testtools do return a tuple. |
| 158 | + if isinstance(response, tuple): |
| 159 | + _body = response[0] |
| 160 | + else: |
| 161 | + _body = response |
| 162 | + |
| 163 | + # NOTE(stephenfin): If our response is an object, we need to |
| 164 | + # serializer and deserialize to convert e.g. date-time |
| 165 | + # to strings |
| 166 | + _body = jsonutils.dump_as_bytes(_body) |
| 167 | + |
| 168 | + if _body == b'': |
| 169 | + body = None |
| 170 | + else: |
| 171 | + body = jsonutils.loads(_body) |
| 172 | + |
| 173 | + try: |
| 174 | + _schema_validator(schema, body, args, kwargs, is_body=True) |
| 175 | + except exception.SchemaValidationError: |
| 176 | + if CONF.api.response_validation == 'warn': |
| 177 | + LOG.exception('Schema failed to validate') |
157 | 178 | else: |
158 | | - body = jsonutils.loads(_body) |
| 179 | + raise |
159 | 180 |
|
160 | | - _schema_validator(schema, body, args, kwargs, is_body=True) |
161 | 181 | return response |
162 | 182 |
|
163 | 183 | wrapper._response_body_schema = schema |
|
0 commit comments