You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using your great lib, I found very annoying bug: validating wrong response (that does not corresponds to the response given in OpenAPI spec) produce fatal AttributeError and script terminates it's work.
Expected behaviour
Library correctly handle wrong responses, give helpful validation error, and continue to work.
Steps to reproduce
Install openapi-core:
pip3 install openapi-core
Create and save following script as listofobjectscheck.py:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import sys
import yaml
from openapi_core import create_spec
from openapi_core.validators import RequestValidator, ResponseValidator
from openapi_core.wrappers import MockRequest, MockResponse
def validate(openapi_file):
with open(openapi_file, 'r') as myfile:
spec_dict = yaml.safe_load(myfile)
spec = create_spec(spec_dict)
openapi_request = MockRequest('localhost', 'get', '/someobjects')
validator = RequestValidator(spec)
result = validator.validate(openapi_request)
request_errors = result.errors
data = json.dumps([
{
'someint': 123,
'somestr': '123'
},
{
'someint': 345,
'somestr': '345'
}
])
openapi_response = MockResponse(data)
validator = ResponseValidator(spec)
result = validator.validate(openapi_request, openapi_response)
response_errors = result.errors
print('Request errors: {} Response errors: {}'.format(request_errors, response_errors))
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Specify path to openapi.yaml file!")
exit(1)
else:
validate(sys.argv[1])
Create and save following specification as listofobjectsfail.yaml, that give us fatal error during validation:
openapi: "3.0.0"
info:
version: "0.1"
title: List of objects
description: Test for list of objects
components:
schemas:
SomeObject:
type: object
properties:
someint:
description: Some integer
type: integer
format: int32
minimum: 0
example: 667
somestr:
description: Some string
type: string
paths:
/someobjects:
get:
summary: Get the someobjects list
operationId: getSomeObjects
responses:
'200':
description: List of someobjects
content:
application/json:
schema:
$ref: '#/components/schemas/SomeObject'
$ python3 listofobjectscheck.py listofobjectsfail.yaml
Traceback (most recent call last):
File "listofobjectscheck.py", line 45, in <module>
validate(sys.argv[1])
File "listofobjectscheck.py", line 34, in validate
result = validator.validate(openapi_request, openapi_response)
File "~/.local/lib/python3.5/site-packages/openapi_core/validators.py", line 201, in validate
data = media_type.unmarshal(raw_data)
File "~/.local/lib/python3.5/site-packages/openapi_core/media_types.py", line 19, in unmarshal
return self.schema.unmarshal(value)
File "~/.local/lib/python3.5/site-packages/openapi_core/schemas.py", line 104, in unmarshal
casted = self.cast(value)
File "~/.local/lib/python3.5/site-packages/openapi_core/schemas.py", line 93, in cast
return cast_callable(value)
File "~/.local/lib/python3.5/site-packages/openapi_core/schemas.py", line 126, in _unmarshal_object
value_keys = value.keys()
AttributeError: 'list' object has no attribute 'keys'
Fix wrong openapi file and save it as listofobjectsok.yaml:
openapi: "3.0.0"
info:
version: "0.1"
title: List of objects
description: Test for list of objects
components:
schemas:
SomeObject:
type: object
properties:
someint:
description: Some integer
type: integer
format: int32
minimum: 0
example: 667
somestr:
description: Some string
type: string
paths:
/someobjects:
get:
summary: Get the someobjects list
operationId: getSomeObjects
responses:
'200':
description: List of someobjects
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/SomeObject'
I edited listofobjectscheck.py in initial report message to pass json string into openapi_response = MockResponse(data) instead of Python list (as we investigated earlier in #27 pull request).
Now everything works fine! Amazing work!
Can confirm this wrong API response now correctly handled without fatals:
$ python3 listofobjectscheck.py listofobjectsfail.yaml
Request errors: [] Response errors: [InvalidMediaTypeValue("Value of [{'somestr': '123', 'someint': 123}, {'somestr': '345', 'someint': 345}] not an object",)]
Hi!
Using your great lib, I found very annoying bug: validating wrong response (that does not corresponds to the response given in OpenAPI spec) produce fatal AttributeError and script terminates it's work.
Expected behaviour
Library correctly handle wrong responses, give helpful validation error, and continue to work.
Steps to reproduce
Install openapi-core:
Create and save following script as
listofobjectscheck.py
:Create and save following specification as
listofobjectsfail.yaml
, that give us fatal error during validation:Execute script to validate spec:
This give us fatal error:
Fix wrong openapi file and save it as
listofobjectsok.yaml
:Sure that now validation works fine:
The text was updated successfully, but these errors were encountered: