-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Response schema from a TypedDict? #1350
Comments
Q1: TemplateViews are not DRF views, so no. Q2: So this is a bit tricky. We have the tooling for Here is a working extensions that add support. TypedDicts are fickle so this won't get native support. I had to add a hack because Put this extension somewhere the interpreter sees it: https://drf-spectacular.readthedocs.io/en/latest/customization.html#step-5-extensions or just next to the TypedDict from drf_spectacular.extensions import OpenApiSerializerExtension
from drf_spectacular.plumbing import _resolve_typeddict
class TypedDictFix(OpenApiSerializerExtension):
target_class = "typing._TypedDictMeta"
match_subclasses = True
def get_name(self, auto_schema, direction):
return self.target.__name__
@classmethod
def _matches(cls, target) -> bool:
"""
super-ugly hack because
issubclass(SomeTypedDict, typing._TypedDictMeta) == False
isinstance(SomeTypedDict, typing._TypedDictMeta) == True
even though SomeTypedDict LOOKS LIKE CLASS
"""
super()._matches(target)
return isinstance(target, typing._TypedDictMeta)
def map_serializer(self, auto_schema: 'AutoSchema', direction):
return _resolve_typeddict(self.target) and then this will work: class XAPIView(APIView):
@extend_schema(responses=LocalizedDict)
def get(self, request):
pass # pragma: no cover However note that |
Thank you, that seems to do the trick! Would it be worth adding this to the extension blueprints in the documentation? |
Hello!
I'm in the process of augmenting my DRF api with drf-spectacular, and I've run into a problem - or actually two, but they are somewhat related.
I have a
Thing
model, and instances of it contain configuration for a Javascript blob that I can embed into a website:Next I have a ThingLoader which is built on top of Django's TemplateView:
And my
loader.js
looks like this:The end result is that I can embed my thing in a website with
<script src="URL-to-my-ThingLoader"></script>
First question: Is there a way to get this endpoint to show up in the generated schema? It's in my urlpatterns in the same way as APIViews and ViewSets are but it doesn't show up. I tried to add
@extend_schema_view(get=extend_schema(responses={(200, 'text/javascript'): bytes}))
toThingLoader
but it errors out withAttributeError: type object 'ThingLoader' has no attribute 'schema'
.Next up I also have a
TypedDict
that is shaped after Thing's config:And a somewhat hacked together view that returns the config as plain JSON:
Since this also inherits from
APIView
it shows up in my schema, but the response body shows up as empty. So that brings us to my second question: I can't figure out if there is a way to use theTypedDict
I already have to tell drf-spectacular about the response?Now the obvious answer would be to refactor the JSON view to use a serializer or define an
inline_serializer
, but it feels like repeating information that I already have in theTypedDict
- especially since in reality there are way more fields than in my example.Thanks in advance.
The text was updated successfully, but these errors were encountered: