Skip to content

Commit

Permalink
Allow reusing anchors in YAML
Browse files Browse the repository at this point in the history
YAML Spec permits anchor name reuse, but ruaml.yaml by default errors for them.
https://stackoverflow.com/questions/39013993/parse-a-yaml-with-duplicate-anchors-in-python
  • Loading branch information
innovate-invent authored and Nolan Woods committed Jul 1, 2022
1 parent c143133 commit 3c0a4fc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/check_jsonschema/loaders/instance/yaml.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import typing as t
import warnings

import ruamel.yaml
from ruamel.yaml.error import ReusedAnchorWarning

_yaml = ruamel.yaml.YAML(typ="safe")
warnings.simplefilter("ignore", ReusedAnchorWarning)

_yaml = ruamel.yaml.YAML(typ="safe", pure=True)

# ruamel.yaml parses timestamp values into datetime.datetime values which differs from
# JSON which parses timestamps as strings. Turn off this feature.
Expand Down
6 changes: 5 additions & 1 deletion src/check_jsonschema/loaders/schema/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import json
import typing as t
import warnings

import identify
import ruamel.yaml
from ruamel.yaml.error import ReusedAnchorWarning

from check_jsonschema import utils

from ...cachedownloader import CacheDownloader
from ..errors import SchemaParseError

yaml = ruamel.yaml.YAML(typ="safe")
warnings.simplefilter("ignore", ReusedAnchorWarning)

yaml = ruamel.yaml.YAML(typ="safe", pure=True)


def _json_load_schema(schema_location: str, fp: t.IO) -> dict:
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def test_schemaloader_local_yaml_data(tmp_path, filename):
a:
type: object
properties:
b:
b: &anchor
type: array
items:
type: integer
c:
c: &anchor
type: string
"""
)
Expand Down Expand Up @@ -130,10 +130,10 @@ def test_instanceloader_yaml_data(tmp_path, filename, default_ft):
f.write_text(
"""\
a:
b:
b: &anchor
- 1
- 2
c: d
c: &anchor d
"""
)
loader = InstanceLoader([str(f)], default_filetype=default_ft)
Expand Down

0 comments on commit 3c0a4fc

Please sign in to comment.