Skip to content

Commit f0bb516

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 f0bb516

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Diff for: openapi_python_client/config.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
import mimetypes
13
from pathlib import Path
24
from typing import Dict, List, Optional
35

@@ -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

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import json
12
import pathlib
23

4+
import pytest
5+
import yaml
6+
37
from openapi_python_client.config import Config
48

59

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

0 commit comments

Comments
 (0)