Fix OpenAPI schema for TokenObtainPairView #954
+47
−0
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.