Skip to content

Commit

Permalink
Drop openapi-spec-validator for custom ref handler
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeSneyders committed Feb 18, 2022
1 parent a67baa5 commit 20b396c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
57 changes: 53 additions & 4 deletions connexion/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,70 @@
Module containing all code related to json schema validation.
"""

import contextlib
import io
import os
import typing as t
import urllib.parse
import urllib.request
from collections.abc import Mapping
from copy import deepcopy

import requests
import yaml
from jsonschema import Draft4Validator, RefResolver
from jsonschema.exceptions import RefResolutionError, ValidationError # noqa
from jsonschema.validators import extend
from openapi_spec_validator.handlers import UrlHandler

from .utils import deep_get


class ExtendedSafeLoader(yaml.SafeLoader):
"""Extends the yaml SafeLoader to coerce all keys to string so the result is valid json."""

def __init__(self, stream):
self.original_construct_mapping = self.construct_mapping
self.construct_mapping = self.extended_construct_mapping
super().__init__(stream)

def extended_construct_mapping(self, node, deep=False):
data = self.original_construct_mapping(node, deep)
return {str(key): data[key] for key in data}


class FileHandler:
"""Handler to resolve file refs."""

def __call__(self, uri):
filepath = self._uri_to_path(uri)
with open(filepath) as fh:
return yaml.load(fh, ExtendedSafeLoader)

@staticmethod
def _uri_to_path(uri):
parsed = urllib.parse.urlparse(uri)
host = "{0}{0}{mnt}{0}".format(os.path.sep, mnt=parsed.netloc)
return os.path.abspath(
os.path.join(host, urllib.request.url2pathname(parsed.path))
)


class URLHandler:
"""Handler to resolve url refs."""

def __call__(self, uri):
response = requests.get(uri)
response.raise_for_status()

data = io.StringIO(response.text)
with contextlib.closing(data) as fh:
return yaml.load(fh, ExtendedSafeLoader)


default_handlers = {
'http': UrlHandler('http'),
'https': UrlHandler('https'),
'file': UrlHandler('file'),
'http': URLHandler(),
'https': URLHandler(),
'file': FileHandler(),
}


Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def read_version(package):
'PyYAML>=5.1,<6',
'requests>=2.9.1,<3',
'inflection>=0.3.1,<0.6',
'openapi-spec-validator>=0.2.4,<0.4',
'werkzeug>=1.0,<3',
]

Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from connexion import App
from connexion.exceptions import InvalidSpecification
from connexion.http_facts import METHODS
from openapi_spec_validator.loaders import ExtendedSafeLoader
from connexion.json_schema import ExtendedSafeLoader

from conftest import TEST_FOLDER, build_app_from_fixture

Expand Down

0 comments on commit 20b396c

Please sign in to comment.