OpenAPI schema omits int64 format for negative values outside int32 range #9980
Replies: 2 comments 1 reply
-
|
Interesting... We also have a django-rest-framework/rest_framework/fields.py Lines 925 to 944 in cf582fb One of the reason was that some languages don't handle int64 well The suggested fix will break if |
Beta Was this translation helpful? Give feedback.
-
|
@browniebroke I checked the current
Current behavior: serializers.BigIntegerField()
# {'type': 'integer'}
serializers.BigIntegerField(min_value=-2147483649)
# {'type': 'integer', 'minimum': -2147483649}
serializers.BigIntegerField(max_value=2147483648)
# {'type': 'integer', 'maximum': 2147483648, 'format': 'int64'}
Given that, I agree it would be better not to change The narrow scope I would propose is only to fix the signed int32 boundary detection in the existing integer mapping logic, while preserving the existing positive minimum = content.get('minimum')
maximum = content.get('maximum')
if (
(maximum is not None and maximum > 2147483647) or
(minimum is not None and (minimum > 2147483647 or minimum < -2147483648))
):
content['format'] = 'int64'That would fix the negative lower-bound case without changing unconditional |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
OpenAPI schema generation appears to omit
format: int64forIntegerFieldvalues below the signed int32 minimum.Positive integer values above the signed int32 maximum are handled correctly, but negative values below
-2147483648do not appear to triggerformat: int64.Reproduction
Actual behavior
{'type': 'integer', 'minimum': -2147483649} {'type': 'integer', 'minimum': -2147483648} {'type': 'integer', 'maximum': 2147483648, 'format': 'int64'} {'type': 'integer', 'maximum': 2147483647}Expected behavior
min_value=-2147483649should includeformat: int64because the value is outside the signed int32 range:{'type': 'integer', 'minimum': -2147483649, 'format': 'int64'}The boundary values should remain unchanged:
{'type': 'integer', 'minimum': -2147483648} {'type': 'integer', 'maximum': 2147483647}Investigation
The current int64 detection appears to compare both bounds only against the signed int32 upper limit:
A negative minimum value can never satisfy
minimum > 2147483647, so values below the signed int32 lower bound are not detected.Impact
Generated OpenAPI schemas may under-specify integer size requirements for APIs that allow values below the signed int32 range.
This affects schema accuracy only. Runtime validation behavior is unchanged.
Suggested fix
Add the missing lower-bound comparison:
Suggested tests
Add OpenAPI field mapping cases for:
IntegerField(min_value=-2147483649)emitsformat: int64IntegerField(min_value=-2147483648)does not emitformat: int64IntegerField(max_value=2147483648)continues to emitformat: int64IntegerField(max_value=2147483647)does not emitformat: int64I would be happy to prepare a small PR with regression tests if this behavior is considered a bug.
Beta Was this translation helpful? Give feedback.
All reactions