openapi-core icon indicating copy to clipboard operation
openapi-core copied to clipboard

Yaml: status codes are converted as integers

Open ghost opened this issue 6 years ago • 6 comments

When writing status codes in yaml as:

reponses:
   200:
     description: OK

They are internally converted to integers and json_schema trips over them here. In addition this isn't caught and so the default TypeError: expected string or bytes-like object tells you nothing.

The same schema converted to Json by ApiMatic converts status codes to strings and so the work around is to quote the response codes in your yaml file.

ghost avatar Jul 10 '18 12:07 ghost

@mes3yd This is openapi-spec-validator related bug https://github.com/p1c2u/openapi-spec-validator/pull/30 and should be fixed in openapi-spec-validator version 0.2.2 . Check you have latest version.

p1c2u avatar Jul 10 '18 13:07 p1c2u

I have 0.2.2 - but am passing the pyyaml dict to create_spec, as per README.md. So, not using the safe loader from openapi_spec_validator.

In short, I'm doing:

import yaml
import json

stream = open(path, 'rt', encoding='utf-8')
fname, ext = os.path.splitext(path)
if ext in ('.yaml', '.yml'):
    spec_dict = yaml.load(stream)
else:
    spec_dict = json.load(stream)

spec = create_spec(spec_dict)

Can you classify this as a documentation bug? Then I'll add this to the docs.

ghost avatar Jul 10 '18 14:07 ghost

This problem still seems to be here, even when using openapi-spec-validator==0.2.4:

❯ pip install PyYAML==3.13 openapi-spec-validator==0.2.4; python -c "import yaml
import openapi_spec_validator
d = yaml.load('''openapi: '3.0.0'
info:
  title: Minimal valid OpenAPI specification
  version: '0.1'
paths:
  /status:
    get:
      responses:
        200:
          description: Return the API status.''')
openapi_spec_validator.validate_spec(d)"

Requirement already satisfied: PyYAML==3.13 in /usr/local/lib/python3.6/site-packages (3.13)
Requirement already satisfied: openapi-spec-validator==0.2.4 in /usr/local/lib/python3.6/site-packages (0.2.4)
Requirement already satisfied: six in /usr/local/lib/python3.6/site-packages (from openapi-spec-validator==0.2.4) (1.11.0)
Requirement already satisfied: jsonschema in /usr/local/lib/python3.6/site-packages (from openapi-spec-validator==0.2.4) (2.6.0)

Traceback (most recent call last):
  File "<string>", line 13, in <module>
  File "/usr/local/lib/python3.6/site-packages/openapi_spec_validator/shortcuts.py", line 7, in validate
    return validator_callable(spec, spec_url=spec_url)
[...SNIP...]
  File "/usr/local/lib/python3.6/site-packages/jsonschema/_validators.py", line 25, in additionalProperties
    extras = set(_utils.find_additional_properties(instance, schema))
  File "/usr/local/lib/python3.6/site-packages/jsonschema/_utils.py", line 104, in find_additional_properties
    if patterns and re.search(patterns, property):
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 182, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

Seems that https://github.com/p1c2u/openapi-spec-validator/pull/30 didn't fix the problem.

jacobsvante avatar Jan 09 '19 14:01 jacobsvante

Any updates here? Because I have this problem too and didn't know how to resolve it

gigimon avatar Feb 07 '19 11:02 gigimon

I suggest wrapping the string 200 with singlequotes.

strets123 avatar Mar 20 '19 13:03 strets123

You can use read_yaml_file from openapi_spec_validator.schemas instead of yaml.load. Works for me.

plambein avatar Oct 08 '19 15:10 plambein