From cbeb4c4e673dd8fc64c134763f921ca7ba87dced Mon Sep 17 00:00:00 2001 From: Tom Date: Sun, 29 Nov 2020 23:40:46 +0200 Subject: [PATCH 1/4] Accept non str values as default json parser value --- environs/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/environs/__init__.py b/environs/__init__.py index 2d02372..24bb368 100644 --- a/environs/__init__.py +++ b/environs/__init__.py @@ -173,8 +173,10 @@ def _preprocess_dict( } -def _preprocess_json(value: str, **kwargs): - return pyjson.loads(value) +def _preprocess_json(value: typing.Union[str, typing.Mapping, typing.List], **kwargs): + if isinstance(value, str): + return pyjson.loads(value) + return value _EnumT = typing.TypeVar("_EnumT", bound=Enum) From 3963e46b7bd9b68903b73fd17feba58980bd06bf Mon Sep 17 00:00:00 2001 From: Brunno Vanelli Date: Wed, 15 Dec 2021 03:25:49 +0100 Subject: [PATCH 2/4] Add type checking when importing default json. --- environs/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/environs/__init__.py b/environs/__init__.py index cc8690c..b9631da 100644 --- a/environs/__init__.py +++ b/environs/__init__.py @@ -209,7 +209,10 @@ def _preprocess_json(value: typing.Union[str, typing.Mapping, typing.List], **kw try: if isinstance(value, str): return pyjson.loads(value) - return value + elif isinstance(value, dict) or isinstance(value, list) or value is None: + return value + else: + raise ma.ValidationError("Not valid JSON.") except pyjson.JSONDecodeError as error: raise ma.ValidationError("Not valid JSON.") from error From b9a74fea7909e37a928b616d10e687837e6cd84a Mon Sep 17 00:00:00 2001 From: Brunno Vanelli Date: Wed, 15 Dec 2021 03:28:11 +0100 Subject: [PATCH 3/4] Add tests to default json validation. --- tests/test_environs.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_environs.py b/tests/test_environs.py index ba44423..49d93ae 100644 --- a/tests/test_environs.py +++ b/tests/test_environs.py @@ -173,6 +173,13 @@ def test_invalid_json_raises_error(self, set_env, env): env.json("JSON") assert "Not valid JSON." in exc.value.args[0] + def test_json_default(self, set_env, env): + assert env.json("JSON", {"foo": "bar"}) == {"foo": "bar"} + assert env.json("JSON", ["foo", "bar"]) == ["foo", "bar"] + with pytest.raises(environs.EnvError) as exc: + env.json("JSON", int) # a builtin is not a valid json + assert "Not valid JSON." in exc.value.args[0] + def test_datetime_cast(self, set_env, env): dtime = dt.datetime.utcnow() set_env({"DTIME": dtime.isoformat()}) From 28ab0ff761659acbbb4dddd68b167aa10786750b Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Mon, 8 Jan 2024 17:46:54 -0500 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bd4c6e..e191392 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 10.1.0 (unreleased) + +Features: + +- Allow `default` for `env.json` to be a `dict` or `list` ([#240](https://github.com/sloria/environs/pull/240)). + Thanks [tomgrin10](https://github.com/tomgrin10) and [bvanelli](https://github.com/bvanelli) for the PRs. + ## 10.0.0 (2023-12-15) Features: