Skip to content
Snippets Groups Projects
Commit 0d1bd4c3 authored by Megan Henning's avatar Megan Henning Committed by GitHub
Browse files

Merge pull request #399 from scitran/remove-schema-loopback

Remove api loopback request for schema ref resolution
parents 9a616bde 918badaf
No related branches found
No related tags found
No related merge requests found
......@@ -53,7 +53,7 @@ DEFAULT_CONFIG = {
'db_connect_timeout': '2000',
'db_server_selection_timeout': '3000',
'data_path': os.path.join(os.path.dirname(__file__), '../persistent/data'),
'schema_path': 'api/schemas',
'schema_path': os.path.join(os.path.dirname(os.path.abspath(__file__)), 'schemas'),
'elasticsearch_host': 'localhost:9200',
},
}
......
{
"id": "#",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"filterDefinition": {
......@@ -7,7 +6,8 @@
"properties": {
"+": {"$ref": "#/definitions/filterItems"},
"-": {"$ref": "#/definitions/filterItems"}
}
},
"additionalProperties": false
},
"filterItems": {
"type": "array",
......@@ -48,8 +48,8 @@
"items": {
"type": "object",
"properties": {
"tags": {"$ref": "download.json#/definitions/filterDefinition"},
"types": {"$ref": "download.json#/definitions/filterDefinition"}
"tags": {"$ref": "#/definitions/filterDefinition"},
"types": {"$ref": "#/definitions/filterDefinition"}
}
}
}
......
import copy
import json
import jsonschema
import re
import requests
from jsonschema.compat import urlopen, urlsplit
import os
from . import config
......@@ -33,52 +30,26 @@ def validate_data(data, schema_json, schema_type, verb, optional=False):
def _validate_json(json_data, schema, resolver):
jsonschema.validate(json_data, schema, resolver=resolver, format_checker=jsonschema.FormatChecker())
class RefResolver(jsonschema.RefResolver):
def resolve_remote(self, uri):
"""override default resolve_remote
to allow testing when there is no ssl certificate
"""
scheme = urlsplit(uri).scheme
if scheme in self.handlers:
result = self.handlers[scheme](uri)
elif (
scheme in [u"http", u"https"] and
requests and
getattr(requests.Response, "json", None) is not None
):
# Requests has support for detecting the correct encoding of
# json over http
if callable(requests.Response.json):
result = requests.get(uri, verify=False).json()
else:
result = requests.get(uri, verify=False).json
else:
# Otherwise, pass off to urllib and assume utf-8
result = json.loads(urlopen(uri).read().decode("utf-8"))
if self.cache_remote:
self.store[uri] = result
return result
# We store the resolvers for each base_uri we use, so that we reuse the schemas cached by the resolvers.
resolvers = {}
def _resolve_schema(schema_url):
base_uri, schema_name = re.match('(.*/)(.*)', schema_url).groups()
if not resolvers.get(base_uri):
resolvers[base_uri] = RefResolver(base_uri, None)
return resolvers[base_uri].resolve(schema_name)[1], resolvers[base_uri]
def _resolve_schema(schema_file_uri):
if not resolvers.get(schema_file_uri):
with open(schema_file_uri) as schema_file:
base_uri = os.path.dirname(schema_file_uri)
schema = json.load(schema_file)
resolver = jsonschema.RefResolver('file://'+base_uri+'/', schema)
resolvers[schema_file_uri] = (schema, resolver)
return resolvers[schema_file_uri]
def no_op(g, *args): # pylint: disable=unused-argument
return g
def schema_uri(type_, schema_name):
return '/'.join([
config.get_item('site', 'api_url'),
'schemas',
type_, schema_name
])
return os.path.join(
config.get_item('persistent', 'schema_path'),
type_,
schema_name
)
def decorator_from_schema_path(schema_url):
if schema_url is None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment