Skip to content

Commit 46fce48

Browse files
committed
config Support json containing tabs
json is not technically a subset of yaml because yaml don't support tabs while json does Signed-off-by: Pierre Tardy <[email protected]>
1 parent a46c05a commit 46fce48

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Diff for: openapi_python_client/config.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from pathlib import Path
22
from typing import Dict, List, Optional
33

4+
import json
45
import yaml
6+
import mimetypes
57
from pydantic import BaseModel
68

79

@@ -35,6 +37,10 @@ class Config(BaseModel):
3537
@staticmethod
3638
def load_from_path(path: Path) -> "Config":
3739
"""Creates a Config from provided JSON or YAML file and sets a bunch of globals from it"""
38-
config_data = yaml.safe_load(path.read_text())
40+
mime = mimetypes.guess_type(path.as_uri(), strict=True)[0]
41+
if mime == "application/json":
42+
config_data = json.loads(path.read_text())
43+
else:
44+
config_data = yaml.safe_load(path.read_text())
3945
config = Config(**config_data)
4046
return config

Diff for: tests/test_config.py

+28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import pathlib
2+
import pytest
23

34
from openapi_python_client.config import Config
5+
import yaml
6+
import json
47

58

69
def test_load_from_path(mocker):
@@ -28,3 +31,28 @@ def test_load_from_path(mocker):
2831
assert config.project_name_override == "project-name"
2932
assert config.package_name_override == "package_name"
3033
assert config.package_version_override == "package_version"
34+
35+
36+
DATA = {"class_overrides": {"Class1": {"class_name": "ExampleClass", "module_name": "example_module"}}}
37+
38+
39+
def json_with_tabs(d):
40+
return json.dumps(d, indent=4).replace(" ", "\t")
41+
42+
43+
@pytest.mark.parametrize(
44+
"filename,dump",
45+
[
46+
("example.yml", yaml.dump),
47+
("example.json", json.dumps),
48+
("example.yaml", yaml.dump),
49+
("example.json", json_with_tabs),
50+
],
51+
)
52+
def test_load_filenames(tmp_path, filename, dump):
53+
yml_file = tmp_path.joinpath(filename)
54+
with open(yml_file, "w") as f:
55+
f.write(dump(DATA))
56+
57+
config = Config.load_from_path(yml_file)
58+
assert config.class_overrides == DATA["class_overrides"]

0 commit comments

Comments
 (0)