Skip to content

Switch YAML parser to ruamel.yaml which uses YAML 1.2 #1042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading