question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In a OpenApiSerializerFieldExtension, how to extend from the default output?

See original GitHub issue

In 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:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tfranzelcommented, Jun 15, 2021

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.

1reaction
foucdegcommented, Jun 10, 2021

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 in OpenApiSerializerFieldExtension. 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found