Skip to content

Commit

Permalink
Merge pull request #112 from proactiveops/fix-yaml-parser
Browse files Browse the repository at this point in the history
Fix parsing yaml spec files containing + as value
  • Loading branch information
skwashd authored Jun 17, 2024
2 parents 668d832 + 93fc9c9 commit 6ee98fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
20 changes: 19 additions & 1 deletion picofun/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ def parse(self, content: str) -> dict:
class YAMLSpecParser(SpecParser):
"""Parses an OpenAPI spec file in YAML format."""

def patch_loader(self, loader: yaml.SafeLoader) -> yaml.SafeLoader:
"""
Patch the loader to allow = as a value.
Handles being call multiple times. It only patches the loader on the first call.
See https://github.com/yaml/pyyaml/issues/89 for more info.
:return: The patched YAML loader.
"""
if "=" in loader.yaml_implicit_resolvers:
loader.yaml_implicit_resolvers.pop("=")
return loader

def parse(self, content: str) -> dict:
"""
Parse the contents of the spec file.
Expand All @@ -108,8 +122,12 @@ def parse(self, content: str) -> dict:
:raises InvalidSpecError: If the spec file is not valid YAML.
:return: The contents of the spec file as a dict.
"""
loader = yaml.SafeLoader
patched_loader = self.patch_loader(loader)
try:
return yaml.safe_load(content)
return yaml.load(
content, Loader=patched_loader # noqa: S506 Uses a patched safe loader
)
except yaml.YAMLError as e:
raise picofun.errors.InvalidSpecError() from e

Expand Down
6 changes: 6 additions & 0 deletions tests/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def test_yaml_spec_parser_invalid_yaml() -> None:
parser.parse("foo: b: a: r")


def test_yaml_spec_parser_value_equals() -> None:
"""Test the YAMLSpecParser class with a plus symbol as the value."""
parser = picofun.spec.YAMLSpecParser()
assert parser.parse("key: +") == {"key": "+"}


def test_spec_json() -> None:
"""Test the Spec class with JSON."""
spec = picofun.spec.Spec("tests/data/petstore.json")
Expand Down

0 comments on commit 6ee98fa

Please sign in to comment.