Fix OpenAPI schema for TokenObtainPairView#954
Open
Shrikantgiri25 wants to merge 5 commits intojazzband:masterfrom
Open
Fix OpenAPI schema for TokenObtainPairView#954Shrikantgiri25 wants to merge 5 commits intojazzband:masterfrom
Shrikantgiri25 wants to merge 5 commits intojazzband:masterfrom
Conversation
f33a5c8 to
99db04c
Compare
…r schema accuracy Explicitly declare 'access' and 'refresh' as read-only fields so schema generators correctly reflect the actual response payload.
…r schema accuracy - Explicitly declare 'access' and 'refresh' fields as read-only - Ensures schema generators (drf-yasg, DRF core) reflect the actual response - No runtime behavior change
99db04c to
f0d2c4f
Compare
Member
Andrew-Chen-Wang
left a comment
There was a problem hiding this comment.
ty
should this be done for any other serializer?
Author
Yes, TokenObtainSlidingSerializer needs the same fix as TokenObtainPairSerializer. I’ve added: |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The OpenAPI / schema generated for
TokenObtainPairViewcan be misleading.TokenObtainPairSerializerdynamically adds theaccessandrefreshfields during
validate(), but those fields are not declared on the serializeritself. Schema generators that rely on serializer field introspection (for
example, drf-yasg or DRF core schema generation) may therefore produce an
incomplete or incorrect response schema.
In practice, the token obtain endpoint returns a JSON response containing both
tokens::
{
"access": "",
"refresh": ""
}
However, this response shape is not always reflected in the generated schema.
This issue has been reported previously:
Solution
Explicitly declare the
accessandrefreshfields onTokenObtainPairSerializeras read-only serializer fields.These values are already returned by the serializer at runtime; this change
simply makes the serializer definition accurately reflect the real response
payload for schema generation purposes.
Why this change is safe
Tests
A test was added to ensure that:
accessandrefreshfields exist on the serializerread_only=TrueThis helps prevent regressions and ensures the serializer remains compatible
with schema generators.
Example schema output (after change)
Example OpenAPI schema generated after this change (drf-yasg / DRF schema)::
Summary
This change aligns the
TokenObtainPairSerializerdefinition with its actualresponse payload, improving schema correctness while keeping runtime behavior
unchanged.