Skip to content

Yaml: status codes are converted as integers #54

@ghost

Description

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.

Activity

p1c2u

p1c2u commented on Jul 10, 2018

@p1c2u
Collaborator

@mes3yd
This is openapi-spec-validator related bug
python-openapi/openapi-spec-validator#30
and should be fixed in openapi-spec-validator version 0.2.2 . Check you have latest version.

ghost

ghost commented on Jul 10, 2018

@ghost

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.

jacobsvante

jacobsvante commented on Jan 9, 2019

@jacobsvante

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 python-openapi/openapi-spec-validator#30 didn't fix the problem.

gigimon

gigimon commented on Feb 7, 2019

@gigimon

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

strets123

strets123 commented on Mar 20, 2019

@strets123

I suggest wrapping the string 200 with singlequotes.

plambein

plambein commented on Oct 8, 2019

@plambein

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jacobsvante@gigimon@p1c2u@strets123@plambein

        Issue actions

          Yaml: status codes are converted as integers · Issue #54 · python-openapi/openapi-core