-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: Fix #218 by serializing "null" for "null" types
+ There were two sets of similar JSON serialization logic in place. This unifies them by just making all our custom objects call for_json() which already does the right thing. + The actual bug is fixed by permitting None values to serialize to JSON, iff the type of the object is "null" Aside: I still don't understand why objects with _type_ null exist in the JSONSchema spec, but w/e.
- Loading branch information
Showing
3 changed files
with
78 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import pytest | ||
import json | ||
import python_jsonschema_objects as pjo | ||
|
||
import logging | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
logging.getLogger().setLevel(logging.DEBUG) | ||
|
||
schema = """ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"$id": "schema.json", | ||
"title":"Null Test", | ||
"type": "object", | ||
"$ref": "#/definitions/test", | ||
"definitions": { | ||
"test": { | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"default": "String" | ||
}, | ||
"id": { | ||
"type": "null", | ||
"default": null | ||
} | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
|
||
@pytest.fixture | ||
def schema_json(): | ||
return json.loads(schema) | ||
|
||
|
||
@pytest.fixture | ||
def ns(schema_json): | ||
builder = pjo.ObjectBuilder(schema_json) | ||
ns = builder.build_classes() | ||
return ns | ||
|
||
|
||
def test_defaults_serialize_for_nullable_types(ns): | ||
logging.basicConfig(level=logging.DEBUG) | ||
logging.getLogger().setLevel(logging.DEBUG) | ||
thing1 = ns.NullTest() | ||
|
||
serialized = thing1.as_dict() | ||
print(serialized) | ||
assert json.dumps(serialized) == """{"name": "String", "id": null}""" | ||
serialized = thing1.serialize() | ||
assert serialized == """{"name": "String", "id": null}""" |