Skip to content

Commit

Permalink
Switch YAML parser to ruamel.yaml which uses YAML 1.2 (#1042)
Browse files Browse the repository at this point in the history
fixes #1041 
ruamel.yaml understands yaml1.2 which yaml doesn't support.
yaml 1.2 lacks norway issue

---------

Co-authored-by: Dylan Anthony <dbanty@users.noreply.github.com>
  • Loading branch information
rtaycher and dbanty committed May 18, 2024
1 parent 9b55d70 commit a97dad1
Show file tree
Hide file tree
Showing 6 changed files with 434 additions and 464 deletions.
11 changes: 11 additions & 0 deletions .changeset/switch_yaml_parsing_to_12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
default: major
---

# Switch YAML parsing to 1.2

This change switches the YAML parsing library to `ruamel.yaml` which follows the YAML 1.2 specification.
[There are breaking changes](https://yaml.readthedocs.io/en/latest/pyyaml/#defaulting-to-yaml-12-support) from YAML 1.1 to 1.2,
though they will not affect most use cases.

PR #1042 fixes #1041. Thanks @rtaycher!
8 changes: 5 additions & 3 deletions openapi_python_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

import httpcore
import httpx
import yaml
from jinja2 import BaseLoader, ChoiceLoader, Environment, FileSystemLoader, PackageLoader
from ruamel.yaml import YAML
from ruamel.yaml.error import YAMLError

from openapi_python_client import utils

Expand Down Expand Up @@ -350,8 +351,9 @@ def _load_yaml_or_json(data: bytes, content_type: Optional[str]) -> Union[Dict[s
return GeneratorError(header=f"Invalid JSON from provided source: {err}")
else:
try:
return yaml.safe_load(data)
except yaml.YAMLError as err:
yaml = YAML(typ="safe")
return yaml.load(data)
except YAMLError as err:
return GeneratorError(header=f"Invalid YAML from provided source: {err}")


Expand Down
5 changes: 3 additions & 2 deletions openapi_python_client/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from pathlib import Path
from typing import Dict, List, Optional, Union

import yaml
from attr import define
from pydantic import BaseModel
from ruamel.yaml import YAML


class ClassOverride(BaseModel):
Expand Down Expand Up @@ -51,7 +51,8 @@ def load_from_path(path: Path) -> "ConfigFile":
if mime == "application/json":
config_data = json.loads(path.read_text())
else:
config_data = yaml.safe_load(path.read_text())
yaml = YAML(typ="safe")
config_data = yaml.load(path)
config = ConfigFile(**config_data)
return config

Expand Down
Loading

0 comments on commit a97dad1

Please sign in to comment.