OpenAPI schema omits zero numeric bounds for ListField child fields #9976
-
SummaryOpenAPI schema generation appears to omit zero-valued numeric bounds for While investigating OpenAPI schema generation, I noticed that Reproductionfrom rest_framework import serializers
from rest_framework.schemas.openapi import AutoSchema
class ExampleSerializer(serializers.Serializer):
values = serializers.ListField(
child=serializers.IntegerField(min_value=0)
)
schema = AutoSchema()
result = schema.map_serializer(ExampleSerializer())
print(result)Expected behavior{
"type": "array",
"items": {
"type": "integer",
"minimum": 0
}
}Actual behavior{
"type": "array",
"items": {
"type": "integer"
}
}The zero-valued constraint is omitted. InvestigationAfter tracing the OpenAPI schema generation flow, it appears the numeric bound mapping relies on truthiness checks, causing Top-level serializer fields appear to be corrected later through validator mapping, but nested/list child schemas do not receive the same correction. ImpactGenerated OpenAPI schemas may under-document validation constraints for list item values. This affects schema accuracy rather than runtime validation. Suggested fixUse explicit For example: if field.min_value is not None:
...
if field.max_value is not None:
...Suggested tests
I would be happy to prepare a PR with tests and a minimal fix if this behavior is considered a bug. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
I see, yes that sounds like a bug. Assuming this is in DRF and not in drf-spectacular, fix would be appreciated, yes. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for confirming. @browniebroke I verified this against DRF's OpenAPI schema generation and the issue appears to be caused by truthiness checks when mapping numeric bounds, which causes 0 and 0.0 values to be omitted for nested/list child fields. I'll prepare a small PR with a minimal fix and regression tests covering IntegerField and FloatField min/max values set to zero. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for confirming the issue. @browniebroke I've opened a PR with a minimal fix and regression tests: The change updates the numeric bound mapping to use explicit The PR also includes regression coverage for:
Feedback is welcome. Thanks for taking a look. |
Beta Was this translation helpful? Give feedback.
I see, yes that sounds like a bug. Assuming this is in DRF and not in drf-spectacular, fix would be appreciated, yes.