-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed drf JSONField being interpreted as Any
Drf JSONField is actually 'any' because it can be of any type (list, obj int, bool, etc). Nevertheless, we (and drf-spectacular) always interpret it as being of type 'object'. In v0.27.0, drf-spectacular fixed that on their side, which broke us. Although it is not correct to keep using JSONFields strictly as a object, this commit adds a drf-spectacular extension that enforces that it should always be trated as an 'object' on openapi schema generation. The reason is to provide a safe fix for our users, who need their bindings working the same way as they always did. Closes: #3639
- Loading branch information
Showing
3 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed openapi schema generation by monkeypatching that drf JSONField is always an OA 'object'. |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""drf-spectacular Extensions. | ||
See: | ||
https://drf-spectacular.readthedocs.io/en/latest/customization.html#step-5-extensions | ||
""" | ||
|
||
from drf_spectacular.extensions import OpenApiSerializerFieldExtension | ||
from drf_spectacular.plumbing import build_basic_type | ||
from drf_spectacular.types import OpenApiTypes | ||
|
||
|
||
class RestrictedJsonFieldExtension(OpenApiSerializerFieldExtension): | ||
""" | ||
Workaround to makes drf JSONField to produce only Object Type. | ||
See: | ||
https://github.com/tfranzel/drf-spectacular/issues/1242 | ||
""" | ||
|
||
target_class = "rest_framework.fields.JSONField" | ||
# match_subclasses = True # not needed here but good to know. | ||
|
||
def map_serializer_field(self, auto_schema, direction): | ||
return build_basic_type(OpenApiTypes.OBJECT) |