Skip to content

Commit 8822b4c

Browse files
author
Rory Xu
committed
Upgrading jsonschema to use up to 4.25
1 parent 605c4c1 commit 8822b4c

File tree

4 files changed

+48
-30
lines changed

4 files changed

+48
-30
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def find_version(*file_paths):
4242
"Jinja2>=3.1.2",
4343
"markupsafe>=2.1.0",
4444
"jsonpatch",
45-
"jsonschema>=3.0.0,<=4.17.3",
45+
"jsonschema<=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: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
import pkg_resources
1010
import yaml
11-
from jsonschema import Draft7Validator, RefResolver
11+
from jsonschema import Draft7Validator
1212
from jsonschema.exceptions import RefResolutionError, ValidationError
13+
import referencing
14+
import referencing.exceptions
1315
from nested_lookup import nested_lookup
1416

1517
from .exceptions import InternalError, SpecValidationError
@@ -56,30 +58,42 @@ def copy_resource(package_name, resource_name, out_path):
5658
shutil.copyfileobj(fsrc, fdst)
5759

5860

59-
def get_schema_store(schema_search_path):
60-
"""Load all the schemas in schema_search_path and return a dict"""
61-
schema_store = {}
61+
def get_schema_registry(schema_search_path):
62+
"""Load all the schemas in schema_search_path and return a registry"""
63+
schemas = {}
6264
schema_fnames = os.listdir(schema_search_path)
6365
for schema_fname in schema_fnames:
6466
schema_path = os.path.join(schema_search_path, schema_fname)
6567
if schema_path.endswith(".json"):
6668
with open(schema_path, "r", encoding="utf-8") as schema_f:
6769
schema = json.load(schema_f)
6870
if "$id" in schema:
69-
schema_store[schema["$id"]] = schema
70-
return schema_store
71+
schemas[schema["$id"]] = schema
72+
73+
# Add HTTPS version of JSON Schema for compatibility
74+
if "http://json-schema.org/draft-07/schema#" in schemas:
75+
schemas["https://json-schema.org/draft-07/schema#"] = schemas["http://json-schema.org/draft-07/schema#"]
76+
77+
# Create resources with proper specification
78+
resources = []
79+
for uri, schema in schemas.items():
80+
try:
81+
resource = referencing.Resource.from_contents(schema)
82+
resources.append((uri, resource))
83+
except Exception:
84+
# Fallback for schemas without proper $schema
85+
resource = referencing.Resource.from_contents(schema, default_specification=referencing.jsonschema.DRAFT7)
86+
resources.append((uri, resource))
87+
88+
return referencing.Registry().with_resources(resources)
7189

7290

7391
def make_validator(schema):
7492
schema_search_path = Path(os.path.dirname(os.path.realpath(__file__))).joinpath(
7593
"data/schema/"
7694
)
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)
95+
registry = get_schema_registry(schema_search_path)
96+
return Draft7Validator(schema, registry=registry)
8397

8498

8599
def make_resource_validator():
@@ -379,7 +393,7 @@ def load_resource_spec(resource_spec_file): # pylint: disable=R # noqa: C901
379393
inliner = RefInliner(base_uri, resource_spec)
380394
try:
381395
inlined = inliner.inline()
382-
except RefResolutionError as e:
396+
except (RefResolutionError, referencing.exceptions.Unresolvable) as e:
383397
LOG.debug("Resource spec validation failed", exc_info=True)
384398
raise SpecValidationError(str(e)) from e
385399

@@ -444,7 +458,7 @@ def load_hook_spec(hook_spec_file): # pylint: disable=R # noqa: C901
444458
inliner = RefInliner(base_uri, hook_spec)
445459
try:
446460
inlined = inliner.inline()
447-
except RefResolutionError as e:
461+
except (RefResolutionError, referencing.exceptions.Unresolvable) as e:
448462
LOG.debug("Hook spec validation failed", exc_info=True)
449463
raise SpecValidationError(str(e)) from e
450464

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)