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.

A library serializer `__init__` calls `kwargs['context']['request']` - how to skip/ignore?

See original GitHub issue

Describe the bug We use a package called django_comments_xtd. In it, django_comments_xtd.api.serializers.ReadCommentSerializer.__init__ calls self.request = kwargs['context']['request']. How can I work round this or simply skip introspection of this class?

from drf_spectacular.plumbing import get_lib_doc_excludes as original_get_lib_doc_excludes

def get_lib_doc_excludes():
    from django_comments_xtd.api import serializers as comment_serializers
    from django_comments_xtd.api import views as comment_views

    return (
        original_get_lib_doc_excludes()
        + [getattr(comment_serializers, c) for c in dir(comment_serializers) if isclass(c)]
        + [getattr(comment_views, c) for c in dir(comment_views) if isclass(c)]
    )

SPECTACULAR_SETTINGS = {
    "GET_LIB_DOC_EXCLUDES": "path.to.above.build_docs.get_lib_doc_excludes",
}

To Reproduce pip install django_comments_xtd ./manage.py spectacular --file openapi.yml

Expected behavior I need a way of skipping this serializer. I have tried returning the class from a custom get_lib_doc_excludes, or all of the package’s view classes. I have tried to figure out a way with OpenApiSerializerExtension. I have even tried monkey-patching. Apologies if I missed something.

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
chrispreeecommented, Nov 15, 2022

Amazing thank you. Have updated my pip requirement to last commit on master:

git+https://github.com/tfranzel/drf-spectacular.git@3292d9db957aaa60ccfeb8e14d91ac9ab42d4110

Will post back here once this is released (will be version of drf-spectacular after 0.24.2).

I gather the extension isn’t a wholesale replacement of the class then, given even with the init fix it still raises the context KeyError on 0.24.2.

I can break packages as we only ever use drf-spectacular as a management command (as our API is too large to introspect live), and I only apply these fixes then, but forgot this is not standard usage.

Enormous thanks for sticking with this, your help is very much appreciated, and I’ve learned several useful new things.

1reaction
tfranzelcommented, Nov 15, 2022

you also may want to use

SPECTACULAR_SETTINGS = {
    'PREPROCESSING_HOOKS': ['drf_spectacular.hooks.preprocess_exclude_path_format'],
}

to exclude the duplicated format endpoints.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exclude a path (so that it won't be visible in Swagger),about ...
(inaccessible) library code: extension and/or hooks ... A library serializer `__init__` calls `kwargs['context']['request']` - how to skip/ignore?
Read more >
Tfranzel Drf-Spectacular Issues - IssueHint
A library serializer `__init__` calls `kwargs['context']['request']` - how to skip/ignore? open, 15, 2022-11-12, 2022-12-07.
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