In a OpenApiSerializerFieldExtension, how to extend from the default output?
See original GitHub issueIn the example provided for OpenApiSerializerFieldExtension, the override simply returns a basic type.
The customization we would like to do is extend from the default output for the field, keeping description
which is derived from the Django model field’s help_text
but adding an example
.
It would look like this:
class DateFieldFix(OpenApiSerializerFieldExtension):
target_class = "rest_framework.fields.DateField"
def map_serializer_field(self, auto_schema, direction):
base = self.get_default_output() # invented method here, does something like that exist?
base["example"] = "2021-06-10"
return base
Sorry for asking a lot of questions in a short time, but your quick replies have encouraged me to ask more questions 😃
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Workflow & schema customization - drf-spectacular
Decorate your view functions with the @extend_schema decorator. ... you can use @extend_schema_view to conveniently annotate the default implementations.
Read more >Define component schema with drf-spectacular for django API
As I am not using serializers, I am defining everything in the extend_schema decorator. Now my question is, if it is possible to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
so i took your comment into consideration, but decided against having both. i only implemented
bypass_extensions
and added this to the documentation. in any case, you should now be able to achieve what you wanted.the reason is that in nearly half of the cases this will not work anyway due warnings in the regular codepath. also
get_default_output
would be the only convenience method in all extensions. it would also only contain that one line that is documented in the docstring. so basically fort consistency and brevity i decided against it.Thanks for the answer.
I assure you that I have spent already one full working day setting up this library and converting all of our existing customizations to it. Maybe I’m dumb, maybe the task was a bit ambitious, but there is no lack of effort. And I’m getting close.
If you’re interested in my attempts before opening the issue, I did try
super().map_serializer_field(auto_schema, direction)
but there was no such method inOpenApiSerializerFieldExtension
. In the code of that class which I did read, the extension mechanism wasn’t clear enough for me to guess that there would be a similarly named method on AutoSchema.I would humbly suggest updating the doc for OpenApiSerializerFieldExtension, with this more full-featured example.