Skip to content

Commit 0e8ec67

Browse files
author
Rory Xu
committed
Upgrading Python version to 3.10 and jsonschema to 4.25
1 parent 605c4c1 commit 0e8ec67

File tree

4 files changed

+42
-32
lines changed

4 files changed

+42
-32
lines changed

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ def find_version(*file_paths):
3636
# package_data -> use MANIFEST.in instead
3737
include_package_data=True,
3838
zip_safe=True,
39-
python_requires=">=3.6",
39+
python_requires=">=3.10",
4040
install_requires=[
4141
"boto3>=1.10.20",
4242
"Jinja2>=3.1.2",
4343
"markupsafe>=2.1.0",
4444
"jsonpatch",
45-
"jsonschema>=3.0.0,<=4.17.3",
45+
"jsonschema>=3.0.0,<=4.25",
4646
"pytest>=4.5.0",
4747
"pytest-random-order>=1.0.4",
4848
"pytest-localserver>=0.5.0",

src/rpdk/core/data_loaders.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
import pkg_resources
1010
import yaml
11-
from jsonschema import Draft7Validator, RefResolver
12-
from jsonschema.exceptions import RefResolutionError, ValidationError
11+
from jsonschema import Draft7Validator
12+
from jsonschema.exceptions import ValidationError, RefResolutionError
13+
from referencing import Registry, Resource
14+
from referencing.jsonschema import DRAFT7
15+
import referencing.exceptions
1316
from nested_lookup import nested_lookup
1417

1518
from .exceptions import InternalError, SpecValidationError
@@ -56,30 +59,33 @@ def copy_resource(package_name, resource_name, out_path):
5659
shutil.copyfileobj(fsrc, fdst)
5760

5861

59-
def get_schema_store(schema_search_path):
60-
"""Load all the schemas in schema_search_path and return a dict"""
61-
schema_store = {}
62+
def get_schema_registry(schema_search_path):
63+
"""Load all the schemas in schema_search_path and return a Registry"""
64+
registry = Registry()
65+
6266
schema_fnames = os.listdir(schema_search_path)
6367
for schema_fname in schema_fnames:
6468
schema_path = os.path.join(schema_search_path, schema_fname)
6569
if schema_path.endswith(".json"):
6670
with open(schema_path, "r", encoding="utf-8") as schema_f:
6771
schema = json.load(schema_f)
6872
if "$id" in schema:
69-
schema_store[schema["$id"]] = schema
70-
return schema_store
73+
resource = Resource.from_contents(schema, default_specification=DRAFT7)
74+
registry = registry.with_resource(schema["$id"], resource)
75+
76+
# For JSON Schema Draft 7, also register the HTTPS version
77+
if schema["$id"] == "http://json-schema.org/draft-07/schema#":
78+
https_id = "https://json-schema.org/draft-07/schema#"
79+
registry = registry.with_resource(https_id, resource)
80+
return registry
7181

7282

7383
def make_validator(schema):
7484
schema_search_path = Path(os.path.dirname(os.path.realpath(__file__))).joinpath(
7585
"data/schema/"
7686
)
77-
resolver = RefResolver(
78-
base_uri=Draft7Validator.ID_OF(schema),
79-
store=get_schema_store(schema_search_path),
80-
referrer=schema,
81-
)
82-
return Draft7Validator(schema, resolver=resolver)
87+
registry = get_schema_registry(schema_search_path)
88+
return Draft7Validator(schema, registry=registry)
8389

8490

8591
def make_resource_validator():
@@ -379,7 +385,7 @@ def load_resource_spec(resource_spec_file): # pylint: disable=R # noqa: C901
379385
inliner = RefInliner(base_uri, resource_spec)
380386
try:
381387
inlined = inliner.inline()
382-
except RefResolutionError as e:
388+
except (RefResolutionError, referencing.exceptions.Unresolvable) as e:
383389
LOG.debug("Resource spec validation failed", exc_info=True)
384390
raise SpecValidationError(str(e)) from e
385391

@@ -444,7 +450,7 @@ def load_hook_spec(hook_spec_file): # pylint: disable=R # noqa: C901
444450
inliner = RefInliner(base_uri, hook_spec)
445451
try:
446452
inlined = inliner.inline()
447-
except RefResolutionError as e:
453+
except (RefResolutionError, referencing.exceptions.Unresolvable) as e:
448454
LOG.debug("Hook spec validation failed", exc_info=True)
449455
raise SpecValidationError(str(e)) from e
450456

src/rpdk/core/jsonutils/inliner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import logging
22
from collections.abc import Iterable, Mapping
33

4-
from jsonschema import RefResolutionError, RefResolver
4+
from jsonschema import RefResolver
5+
from jsonschema.exceptions import RefResolutionError
6+
import referencing.exceptions
57

68
from .renamer import RefRenamer
79
from .utils import BASE, rewrite_ref, traverse

tests/test_data_loaders.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010

1111
import pytest
1212
import yaml
13-
from jsonschema.exceptions import RefResolutionError, ValidationError
13+
from jsonschema.exceptions import ValidationError, RefResolutionError
14+
import referencing.exceptions
1415
from pytest_localserver.http import Request, Response, WSGIServer
1516

1617
from rpdk.core.data_loaders import (
1718
STDIN_NAME,
1819
get_file_base_uri,
19-
get_schema_store,
20+
get_schema_registry,
2021
load_hook_spec,
2122
load_resource_spec,
2223
resource_json,
@@ -669,36 +670,37 @@ def test_resource_yaml():
669670
assert result == obj
670671

671672

672-
def test_get_schema_store_schemas_with_id():
673-
schema_store = get_schema_store(
673+
def test_get_schema_registry_schemas_with_id():
674+
schema_registry = get_schema_registry(
674675
BASEDIR.parent / "src" / "rpdk" / "core" / "data" / "schema"
675676
)
676-
assert len(schema_store) == 7
677-
assert "http://json-schema.org/draft-07/schema#" in schema_store
677+
assert len(schema_registry) == 8 # 7 original + HTTPS version of JSON Schema
678+
assert "http://json-schema.org/draft-07/schema#" in schema_registry
679+
assert "https://json-schema.org/draft-07/schema#" in schema_registry # HTTPS version
678680
assert (
679681
"https://schema.cloudformation.us-east-1.amazonaws.com/base.definition.schema.v1.json"
680-
in schema_store
682+
in schema_registry
681683
)
682684
assert (
683685
"https://schema.cloudformation.us-east-1.amazonaws.com/provider.configuration.definition.schema.v1.json"
684-
in schema_store
686+
in schema_registry
685687
)
686688
assert (
687689
"https://schema.cloudformation.us-east-1.amazonaws.com/provider.definition.schema.v1.json"
688-
in schema_store
690+
in schema_registry
689691
)
690692
assert (
691693
"https://schema.cloudformation.us-east-1.amazonaws.com/provider.definition.schema.hooks.v1.json"
692-
in schema_store
694+
in schema_registry
693695
)
694696
assert (
695697
"https://schema.cloudformation.us-east-1.amazonaws.com/provider.configuration.definition.schema.hooks.v1.json"
696-
in schema_store
698+
in schema_registry
697699
)
698700

699701

700-
def test_get_schema_store_schemas_with_out_id():
701-
schema_store = get_schema_store(
702+
def test_get_schema_registry_schemas_with_out_id():
703+
schema_registry = get_schema_registry(
702704
BASEDIR.parent / "src" / "rpdk" / "core" / "data" / "examples" / "resource"
703705
)
704-
assert len(schema_store) == 0
706+
assert len(schema_registry) == 0

0 commit comments

Comments
 (0)