From 4d0cba03db9343c19350ac21b789ff312e755b2f Mon Sep 17 00:00:00 2001 From: Benjamin Pelletier Date: Fri, 11 Aug 2023 14:21:45 +0000 Subject: [PATCH] Fix jsonschema treatment of bool fields (#10) --- src/implicitdict/jsonschema.py | 3 +++ tests/test_jsonschema.py | 1 - tests/test_types.py | 5 +++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/implicitdict/jsonschema.py b/src/implicitdict/jsonschema.py index 422183a..4a1046c 100644 --- a/src/implicitdict/jsonschema.py +++ b/src/implicitdict/jsonschema.py @@ -162,6 +162,9 @@ def _schema_for(value_type: Type, schema_vars_resolver: SchemaVarsResolver, sche make_json_schema(value_type, schema_vars_resolver, schema_repository) return {"$ref": schema_vars.path_to(value_type, context)}, False + if value_type == bool or issubclass(value_type, bool): + return {"type": "boolean"}, False + if value_type == float or issubclass(value_type, float): return {"type": "number"}, False diff --git a/tests/test_jsonschema.py b/tests/test_jsonschema.py index b094d71..d274d27 100644 --- a/tests/test_jsonschema.py +++ b/tests/test_jsonschema.py @@ -1,5 +1,4 @@ import json -import os.path from typing import Type import implicitdict.jsonschema diff --git a/tests/test_types.py b/tests/test_types.py index a5b6a19..3de419f 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -148,10 +148,11 @@ class SpecialTypesData(ImplicitDict): datetime: StringBasedDateTime timedelta: StringBasedTimeDelta yesno: YesNo + boolean: bool @staticmethod def example_value(): - return ImplicitDict.parse({"datetime": datetime.utcnow().isoformat(), "timedelta": "12h", "yesno": "Yes"}, SpecialTypesData) + return ImplicitDict.parse({"datetime": datetime.utcnow().isoformat(), "timedelta": "12h", "yesno": "Yes", "boolean": "true"}, SpecialTypesData) class NestedDefinitionsData(ImplicitDict): @@ -159,4 +160,4 @@ class NestedDefinitionsData(ImplicitDict): @staticmethod def example_value(): - return ImplicitDict.parse({"special_types": {"datetime": datetime.utcnow().isoformat(), "timedelta": "12h", "yesno": "Yes"}}, NestedDefinitionsData) + return ImplicitDict.parse({"special_types": {"datetime": datetime.utcnow().isoformat(), "timedelta": "12h", "yesno": "Yes", "boolean": "true"}}, NestedDefinitionsData)