Skip to content

Commit

Permalink
fix: #9
Browse files Browse the repository at this point in the history
default '0' did not work as intended anymore
  • Loading branch information
mnboos committed Jan 31, 2023
1 parent 6a783c4 commit 48d95aa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# 1.4.1
- fix regression and assure code works as intended (see #9)
# 1.4.0
- 'const' support added
# 1.3.1
Expand Down
6 changes: 5 additions & 1 deletion jsonschema_default/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def _get_default(name: str, prop: dict, schema: dict, from_ref: bool = False) ->
:return:
"""

default = prop.get("default") or prop.get("const")
default = None
if "default" in prop:
default = prop.get("default")
elif "const" in prop:
default = prop.get("const")
ref = prop.get("$ref")
prop_type = prop.get("type", None)
one_of = prop.get("oneOf", None)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jsonschema-default"
version = "1.4.0"
version = "1.4.1"
description = "Create default objects from a JSON schema"
authors = ["Martin Boos <[email protected]>"]
maintainers = ["Martin Boos <[email protected]>"]
Expand Down
9 changes: 9 additions & 0 deletions schemas/default/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"number": {
"type": "number",
"default": 0
}
}
}
10 changes: 10 additions & 0 deletions schemas/default/default_and_const.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"number": {
"type": "number",
"default": 0,
"const": 123
}
}
}
19 changes: 19 additions & 0 deletions tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json
import jsonschema_default as js
from pathlib import Path
import re


def test_default():
obj = js.create_from("./schemas/default/default.json")
assert obj == {"number": 0}


def test_default_and_const():
"""
If both 'default' and 'const' are defined, 'default' has higher precedence
:return:
"""

obj = js.create_from("./schemas/default/default_and_const.json")
assert obj == {"number": 0}

0 comments on commit 48d95aa

Please sign in to comment.