Yelp / bravado-core

Other
109 stars 98 forks source link

Spec.from_dict() fails with a local file uri as origin_url #311

Closed tscheburaschka closed 5 years ago

tscheburaschka commented 5 years ago

Problem

I was trying to solve problem #310 and ran into the following issue.

Repro steps

The file petstoreExample.yaml is identical to the original example under: https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/yaml/petstore.yaml The file path to petstoreExample.yaml is D:/Jan/eight/more/subdirs/without/spaces/scripts/petstoreExample_20.yaml The code I executed was:

from pathlib import Path
import yaml
from bravado_core.spec import Spec

schema_path = Path.cwd() / 'scripts' / 'petstoreExample_20.yaml'
with open(schema_path, 'r') as f:
    raw_spec = yaml.load(f)
spec = Spec.from_dict(raw_spec, origin_url=schema_path.as_uri())

The last line fails with:

Traceback (most recent call last):
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\jsonschema\validators.py", line 380, in resolve_from_url
    document = self.store[url]
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\jsonschema\_utils.py", line 23, in __getitem__
    return self.store[self.normalize(uri)]
KeyError: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\jsonschema\validators.py", line 383, in resolve_from_url
    document = self.resolve_remote(url)
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\jsonschema\validators.py", line 474, in resolve_remote
    result = json.loads(urlopen(uri).read().decode("utf-8"))
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\urllib\request.py", line 511, in open
    req = Request(fullurl, data)
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\urllib\request.py", line 329, in __init__
    self.full_url = url
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\urllib\request.py", line 355, in full_url
    self._parse()
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\urllib\request.py", line 384, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\IPython\core\interactiveshell.py", line 2961, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-10-5f4622581aff>", line 1, in <module>
    spec = Spec.from_dict(raw_spec, origin_url=schema_path.as_uri())
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\bravado_core\spec.py", line 180, in from_dict
    spec.build()
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\bravado_core\spec.py", line 192, in build
    self._validate_spec()
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\bravado_core\spec.py", line 188, in _validate_spec
    http_handlers=build_http_handlers(self.http_client),
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\swagger_spec_validator\validator20.py", line 126, in validate_spec
    validate_apis(apis, bound_deref)
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\swagger_spec_validator\validator20.py", line 343, in validate_apis
    validate_responses(api_name, oper_name, oper_body['responses'], deref)
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\swagger_spec_validator\validator20.py", line 241, in validate_responses
    visited_definitions_ids=set(),
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\swagger_spec_validator\validator20.py", line 417, in validate_definition
    definition = deref(definition)
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\swagger_spec_validator\validator20.py", line 78, in deref
    with resolver.resolving(ref) as target:
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\jsonschema\validators.py", line 366, in resolving
    url, resolved = self.resolve(ref)
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\jsonschema\validators.py", line 375, in resolve
    return url, self._remote_cache(url)
  File "C:\Users\Jan\Miniconda3\envs\adaz\lib\site-packages\jsonschema\validators.py", line 385, in resolve_from_url
    raise RefResolutionError(exc)
jsonschema.exceptions.RefResolutionError: unknown url type: ''
macisamuele commented 5 years ago

The error reported looks odd to me.

I would like to help but I would need 2 debugging prints. Could you print the vaule of raw_spec and schema_path.as_uri()? I tried to reproduce your issue with no success and currently I have no access to a Windows machine.

tscheburaschka commented 5 years ago

schema_path.as_uri() :

'file:///D:/Jan/root/work/Projekte/windkraft/Code/gitWd/AnalyticsKernelPy/scripts/petstoreExample_20.yaml'

raw_spec :

{'swagger': '2.0',
 'info': {'version': '1.0.0',
  'title': 'Swagger Petstore',
  'license': {'name': 'MIT'}},
 'host': 'petstore.swagger.io',
 'basePath': '/v1',
 'schemes': ['http'],
 'consumes': ['application/json'],
 'produces': ['application/json'],
 'paths': {'/pets': {'get': {'summary': 'List all pets',
    'operationId': 'listPets',
    'tags': ['pets'],
    'parameters': [{'name': 'limit',
      'in': 'query',
      'description': 'How many items to return at one time (max 100)',
      'required': False,
      'type': 'integer',
      'format': 'int32'}],
    'responses': {'200': {'description': 'A paged array of pets',
      'headers': {'x-next': {'type': 'string',
        'description': 'A link to the next page of responses'}},
      'schema': {'$ref': '#/definitions/Pets'}},
     'default': {'description': 'unexpected error',
      'schema': {'$ref': '#/definitions/Error'}}}},
   'post': {'summary': 'Create a pet',
    'operationId': 'createPets',
    'tags': ['pets'],
    'responses': {'201': {'description': 'Null response'},
     'default': {'description': 'unexpected error',
      'schema': {'$ref': '#/definitions/Error'}}}}},
  '/pets/{petId}': {'get': {'summary': 'Info for a specific pet',
    'operationId': 'showPetById',
    'tags': ['pets'],
    'parameters': [{'name': 'petId',
      'in': 'path',
      'required': True,
      'description': 'The id of the pet to retrieve',
      'type': 'string'}],
    'responses': {'200': {'description': 'Expected response to a valid request',
      'schema': {'$ref': '#/definitions/Pets'}},
     'default': {'description': 'unexpected error',
      'schema': {'$ref': '#/definitions/Error'}}}}}},
 'definitions': {'Pet': {'required': ['id', 'name'],
   'properties': {'id': {'type': 'integer', 'format': 'int64'},
    'name': {'type': 'string'},
    'tag': {'type': 'string'}}},
  'Pets': {'type': 'array', 'items': {'$ref': '#/definitions/Pet'}},
  'Error': {'required': ['code', 'message'],
   'properties': {'code': {'type': 'integer', 'format': 'int32'},
    'message': {'type': 'string'}}}}}
macisamuele commented 5 years ago

I borrowed a Windows VM from appveyor to provide some support 😄

I was not able to reproduce your issue, check https://gist.github.com/macisamuele/06ca2b90abd873423649aa81025449ae for more details.

If you still have issues my suggestions would be:

tscheburaschka commented 5 years ago

Thank you for your investigation. Unfortunately, neither can I now reproduce the bug. Maybe it was related to some hiccups in the PyCharm-debugger ... Sorry for having wasted your time. Now I am back at issue #310.

macisamuele commented 5 years ago

@tscheburaschka that's not wasted time. Don't worry 😄