-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and expose JSONDictField and JSONListField on the plugin API
DRF serializers.JSONField can be any json entity, but we want more precise types for better schema/bindings representation. New fields that are supposed to be dict or list structures should use the new JSONDictField or JSONListField field. Some context: <pulp/pulp_rpm#3639>
- Loading branch information
Showing
5 changed files
with
102 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Exposed JSONDictField and JSONListField on the plugin API. | ||
|
||
DRF serializers.JSONField can be any json entity, but we want more precise types | ||
for better schema/bindings representation. New fields that are supposed to be dict | ||
or list structures should use the new JSONDictField or JSONListField field. | ||
|
||
Some context: <https://github.com/pulp/pulp_rpm/issues/3639> |
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
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,49 @@ | ||
import pytest | ||
from rest_framework import serializers | ||
|
||
from pulpcore.app.serializers import fields | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"field_and_data", | ||
[ | ||
(fields.JSONDictField, '{"foo": 123, "bar": [1,2,3]}'), | ||
(fields.JSONListField, '[{"foo": 123}, {"bar": 456}]'), | ||
], | ||
) | ||
@pytest.mark.parametrize("binary_arg", [True, False]) | ||
def test_custom_json_dict_field(field_and_data, binary_arg): | ||
""" | ||
On the happy overlap case, | ||
pulpcore JSONDictField and JSONListField should be compatible with drf JSONField. | ||
""" | ||
custom_field, data = field_and_data | ||
drf_json_field = serializers.JSONField(binary=binary_arg) | ||
custom_field = custom_field(binary=binary_arg) | ||
custom_field_result = custom_field.to_internal_value(data) | ||
drf_field_result = drf_json_field.to_internal_value(data) | ||
assert custom_field_result == drf_field_result | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"field_and_data", | ||
[ | ||
(fields.JSONDictField, '[{"foo": 123}, {"bar": 456}]'), | ||
(fields.JSONDictField, "123"), | ||
(fields.JSONDictField, "false"), | ||
(fields.JSONListField, '{"foo": 123, "bar": [1,2,3]}'), | ||
(fields.JSONListField, "123"), | ||
(fields.JSONListField, "false"), | ||
], | ||
) | ||
@pytest.mark.parametrize("binary_arg", [True, False]) | ||
def test_custom_json_dict_field_raises(field_and_data, binary_arg): | ||
""" | ||
On the invalid data case, | ||
pulpcore JSONDictField and JSONListField should raise appropriately. | ||
""" | ||
custom_field, data = field_and_data | ||
custom_field = custom_field(binary=binary_arg) | ||
error_msg = "Invalid type" | ||
with pytest.raises(serializers.ValidationError, match=error_msg): | ||
custom_field.to_internal_value(data) |