Skip to content

Commit

Permalink
Fix jsonschema treatment of bool fields (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminPelletier authored Aug 11, 2023
1 parent e83bd9a commit 4d0cba0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/implicitdict/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion tests/test_jsonschema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import os.path
from typing import Type

import implicitdict.jsonschema
Expand Down
5 changes: 3 additions & 2 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,16 @@ 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):
special_types: SpecialTypesData

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

0 comments on commit 4d0cba0

Please sign in to comment.