-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Null/blank ModelSerializer fields are not required in responses with COMPONENT_SPLIT_REQUEST=True #1347
Comments
I wonder if this is (also) a bug in DRF. Documentation for fields/required says:
In my understanding, this means that Edit: I even found a test in DRF that seems to contradict the documentation. Here is an optional field not being omitted during serialization: https://github.com/encode/django-rest-framework/blob/master/tests/test_serializer.py#L567C1-L567C96 def test_default_for_allow_null(self):
"""
Without an explicit default, allow_null implies default=None when serializing. #5518 #5708
"""
class Serializer(serializers.Serializer):
foo = serializers.CharField()
bar = serializers.CharField(source='foo.bar', allow_null=True)
optional = serializers.CharField(required=False, allow_null=True)
# allow_null=True should imply default=None when serializing:
assert Serializer({'foo': None}).data == {'foo': None, 'bar': None, 'optional': None, } |
Hi, this is a convoluted issue and easy to misinterpret. Let me give some ground work: We only use what DRF gives us (because we cannot differentiate your choices from DRFs default choices). We are not inventing new logic here. If DRF (or you) mark the serializer field as required, we mark it as required. Also, you have to live with the drf-spectacular/drf_spectacular/openapi.py Lines 1053 to 1058 in 9bf8e92
It is more informative to look at the generated Also, we have added a heuristic with the In a perfect world there would be
We cannot look into the drf-spectacular/drf_spectacular/utils.py Lines 598 to 601 in 9bf8e92
|
2 more things:
|
Describe the bug
When component splitting is turned on, ModelSerializer fields that correspond to
null=True
orblank=True
model fields are not marked as required in the response components, despite DRF's default behavior of always including these fields in the JSON response when serializing model instances, even if the field's value isNone
or empty string.To Reproduce
And
Fails with:
Expected behavior
All ModelSerializer fields should be marked as required. There should however be some way of opting out from this, in case for instance
toRepresentation
is overridden to exclude some fields.The text was updated successfully, but these errors were encountered: