You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi @axnsan12. Thanks for the report (and the test-case).
This use-case isn't currently supported. It's not documented that it should work. But it does seem like a good enhancement to be able to customise the schema for the extra action decorators declaratively.
Current workaround will be to subclass AutoSchema, override get_link to check self.view.action and adjust fields for your detail route.
Related #5621 (comment): We should Extract Method on the logic to adjust the generated fields list...
Most class options work when set on a detail_route, so intuition would guide one to think this should also work.
Upon looking closer, this seems to be related to ViewInspector wanting to bind itself as a descriptor. Making this work would require ViewInspector to act as a data descriptor instead of a method descriptor (i.e. also implement __set__, in essence becoming a kind of @property on the view class).
A more concrete description of what I mean:
classViewInspector(object):
""" Descriptor class on APIView. Provide subclass for per-view schema generation """def__get__(self, instance, owner):
""" Enables `ViewInspector` as a Python _Descriptor_. This is how `view.schema` knows about `view`. `__get__` is called when the descriptor is accessed on the owner. (That will be when view.schema is called in our case.) `owner` is always the owner class. (An APIView, or subclass for us.) `instance` is the view instance or `None` if accessed from the class, rather than an instance. See: https://docs.python.org/3/howto/descriptor.html for info on descriptor usage. """real_schema=getattr(instance, '_real_schema', self)
real_schema.view=instancereturnreal_schemadef__set__(self, instance, other):
""" `__set__` is called when the descriptor is set on the owner. (That will be when `view.schema = other` is called in our case.) `other` is always the new value being set (most likely a ViewInspector, or subclass for us.) `instance` is the view instance or `None` if accessed from the class, rather than an instance. If __get__ is defined without defining __set__, this object becomes a `non-data descriptor`, which has lower lookup priority than an object's __dict__. This means that calls to setattr(instance, 'schema', other) would cause the descriptor logic to be bypassed altogether. It is necessary to also implement __set__ to become a `data descriptor` and prevent this. From the python documentation link above: * data descriptors always override instance dictionaries. * non-data descriptors may be overridden by instance dictionaries. This essentially enables the replacing of a view's schema by setting it on an instance, and is necessary to support e.g. setting the schema on a detail_route declaration. """ifisinstance(other, ViewInspector):
instance._real_schema=other
This works and passes the test, but feels a bit hackish and confusing.
Yep. I can see why you'd think this may work. (Agreed it would be a nice enhancement.)
No problem, in theory, adding a __set__. (As ever it'll be complexity vs benefit.)
If you want to expand #5631 to include a more featured test case where the user is setting manual fields via the view's schema attribute as well as using detail_route as per here (as well as similar combinations if you can — the more varied the better) that would be great.
We can then think about an implementation.
Just to note now: it may be that we turn down adding this — sticking with the subclassing approach — depending on whether it turns out suitably nice™.
Similar to that is per-method custom fields, as per #5621. There the current best option is to allow people to handle their own case in a subclass without trying to do everything out-of-the-box. The subclasses are simple; the general case has many edge-cases to consider; thus the cost-benefit is on the side on not providing that.
Obviously all such judgements are open to re-assesment in the face of a nice implementation. 🙂
After further thought I think you may be right that special casing this might not be worth it, but there are arguments both for and against. Here are some of my thoughts on this:
a detail_route in a ViewSet behaves a bit like a nested APIView (or function based api_view), so it would make sense to be able to be able to set all the variables you can set on an APIView
as it is now, to check for a detail_route or list_route from the AutoSchema of a containing viewset you have to switch on both the action and the HTTP method (because list/detail routes can accept more than one method) - I would argue this is a bit of a design flaw in detail/list routes, as they break the general viewset paradigm that 1 action == 1 class method == 1 (path + HTTP method) combination that applies to the default actions
the only reason this does not work is because of the slightly awkward implementation of ViewInspector as a descriptor, but I think it could be made to work
The deeper problem, though, is that a ViewInspector as a whole is responsible solely for generating, in coreapi terminology, Link objects, which correspond to a (path, HTTP method) combination. It makes no sense to me to have a View/ViewSet-wide object which is reponsible for inspecting all its Links, in a perhaps if-else-if-else manner in the get_link method. IMO, it would be cleaner/more consistent to have a sort of get_schema method and schema_class attribute, à la get_serializer/serializer_class. Like that you could return a different AutoSchema subclass/instance for specific actions/methods.
So, overall I feel like making AutoSchema work on detail_route would not solve much, since it simplifies a narrow use case while not solving problems in broader use cases.
Part of this is down to view sets... — you're right that we need to switch on action + method, and that's a bit funcky. However, that is just view sets. (If you pushed hard enough you might find me saying "Don't use them — yeah they're great for getting up and running but..." — part of that is related to this kind of thing...) (Something about explicit is better than... 🙂)
I don't see a barrier to having ViewInspector return a differently configured class for different actions. It's not something we do yet but...
The question for me would be how best to provide the hooks to make that extensible because...
We deliberately avoided adding more API to APIView. The single schema attribute was the smallest footprint we could get away with. Our view — the design decision — was that API on APIView is already complex enough, and we didn't want to add more here. We wanted to keep all the schema related stuff in a separate class.
We'll keep this open. I'll see what I can think up for v3.8 in the new year.
...no sense to me to have a View/ViewSet-wide object which is reponsible for inspecting all its Links, in a perhaps if-else-if-else manner in the get_link method.
I hear where you're coming from: It looks a lot nicer if you're using separate views instead of viewsets (see opening points.)
Prior to v3.7 we had a single API-wide object doing if-else-if-else on the whole API. That's why it was impossible to customise. Bar these points about Viewsets, I think single object inspecting single view is about right. (Again, we really didn't want to bundle the reflection logic intoAPIView.)
IMO, it would be cleaner/more consistent to have a sort of get_schema method and schema_class attribute, à la get_serializer/serializer_class. Like that you could return a different AutoSchema subclass/instance for specific actions/methods.
Do you think that would be too much API footprint? (as in, 1 attribute and 1 method vs just 1 attribute)... for the common case you would not have to override both, just like a lot of the time you can get away with serializer_class
I'll take a stab at this in 5605. In general, that PR aims for greater framework support for extra viewset actions. I'm not incredibly familiar with core-api, but at the very least, 5606 should provide the methods necessary to implement schema support.
The problem with the create_view approach above is that it's not thread-safe, given that you're mutating a class-level variable per-instance. The best bet is probably to change the API schema_class/get_schema and set the schema.view there.
It wouldn't be too difficult to deprecate/change this:
If schema is present (e.g., set by user on a subclass), the inspector/generator warns and uses the provided schema.
Activity
Add test for AutoSchema on detail_route (encode#5630)
carltongibson commentedon Nov 28, 2017
Hi @axnsan12. Thanks for the report (and the test-case).
This use-case isn't currently supported. It's not documented that it should work. But it does seem like a good enhancement to be able to customise the schema for the extra action decorators declaratively.
Current workaround will be to subclass
AutoSchema, overrideget_linkto checkself.view.actionand adjust fields for your detail route.Related #5621 (comment): We should Extract Method on the logic to adjust the generated fields list...
axnsan12 commentedon Nov 28, 2017
Most class options work when set on a
detail_route, so intuition would guide one to think this should also work.Upon looking closer, this seems to be related to
ViewInspectorwanting to bind itself as a descriptor. Making this work would requireViewInspectorto act as a data descriptor instead of a method descriptor (i.e. also implement__set__, in essence becoming a kind of@propertyon the view class).A more concrete description of what I mean:
This works and passes the test, but feels a bit hackish and confusing.
carltongibson commentedon Nov 28, 2017
Hey @axnsan12.
Yep. I can see why you'd think this may work. (Agreed it would be a nice enhancement.)
No problem, in theory, adding a
__set__. (As ever it'll be complexity vs benefit.)If you want to expand #5631 to include a more featured test case where the user is setting manual fields via the view's
schemaattribute as well as usingdetail_routeas per here (as well as similar combinations if you can — the more varied the better) that would be great.We can then think about an implementation.
Just to note now: it may be that we turn down adding this — sticking with the subclassing approach — depending on whether it turns out suitably nice™.
Similar to that is per-method custom fields, as per #5621. There the current best option is to allow people to handle their own case in a subclass without trying to do everything out-of-the-box. The subclasses are simple; the general case has many edge-cases to consider; thus the cost-benefit is on the side on not providing that.
Obviously all such judgements are open to re-assesment in the face of a nice implementation. 🙂
axnsan12 commentedon Nov 30, 2017
Hello,
I see now that this is a bit more complicated than I imagined, from a design point of view.
I will come back to this after I play with views/schema generation and understand it better.
carltongibson commentedon Dec 1, 2017
Note for later: Make sure to see the test case in #5631
axnsan12 commentedon Dec 19, 2017
After further thought I think you may be right that special casing this might not be worth it, but there are arguments both for and against. Here are some of my thoughts on this:
detail_routein a ViewSet behaves a bit like a nestedAPIView(or function basedapi_view), so it would make sense to be able to be able to set all the variables you can set on anAPIViewdetail_routeorlist_routefrom the AutoSchema of a containing viewset you have to switch on both the action and the HTTP method (because list/detail routes can accept more than one method) - I would argue this is a bit of a design flaw in detail/list routes, as they break the general viewset paradigm that 1 action == 1 class method == 1 (path + HTTP method) combination that applies to the default actionsViewInspectoras a descriptor, but I think it could be made to workThe deeper problem, though, is that a
ViewInspectoras a whole is responsible solely for generating, in coreapi terminology, Link objects, which correspond to a (path, HTTP method) combination. It makes no sense to me to have a View/ViewSet-wide object which is reponsible for inspecting all its Links, in a perhaps if-else-if-else manner in theget_linkmethod. IMO, it would be cleaner/more consistent to have a sort ofget_schemamethod andschema_classattribute, à laget_serializer/serializer_class. Like that you could return a different AutoSchema subclass/instance for specific actions/methods.So, overall I feel like making
AutoSchemawork ondetail_routewould not solve much, since it simplifies a narrow use case while not solving problems in broader use cases.carltongibson commentedon Dec 19, 2017
Hi @axnsan12.
Thanks for your comments. Very useful.
Part of this is down to view sets... — you're right that we need to switch on action + method, and that's a bit funcky. However, that is just view sets. (If you pushed hard enough you might find me saying "Don't use them — yeah they're great for getting up and running but..." — part of that is related to this kind of thing...) (Something about explicit is better than... 🙂)
I don't see a barrier to having
ViewInspectorreturn a differently configured class for different actions. It's not something we do yet but...The question for me would be how best to provide the hooks to make that extensible because...
We deliberately avoided adding more API to APIView. The single
schemaattribute was the smallest footprint we could get away with. Our view — the design decision — was that API on APIView is already complex enough, and we didn't want to add more here. We wanted to keep all the schema related stuff in a separate class.We'll keep this open. I'll see what I can think up for v3.8 in the new year.
I hear where you're coming from: It looks a lot nicer if you're using separate views instead of viewsets (see opening points.)
Prior to v3.7 we had a single API-wide object doing if-else-if-else on the whole API. That's why it was impossible to customise. Bar these points about Viewsets, I think single object inspecting single view is about right. (Again, we really didn't want to bundle the reflection logic into
APIView.)axnsan12 commentedon Dec 19, 2017
Hello again,
And what about
Do you think that would be too much API footprint? (as in, 1 attribute and 1 method vs just 1 attribute)... for the common case you would not have to override both, just like a lot of the time you can get away with
serializer_classhzj629206 commentedon Jan 23, 2018
Hello, any update? still get this error.
Currently I have to use this:
carltongibson commentedon Jan 23, 2018
This is currently a known limitation. "PRs welcome" 🙂
rpkilby commentedon Jan 24, 2018
I'll take a stab at this in 5605. In general, that PR aims for greater framework support for extra viewset actions. I'm not incredibly familiar with core-api, but at the very least, 5606 should provide the methods necessary to implement schema support.
rpkilby commentedon May 16, 2018
So this isn't within the scope of 5605, but I did take a few minutes to look at this.
Worth noting, the below fails at the
assert self._view is not Nonecheck:The problem with the
create_viewapproach above is that it's not thread-safe, given that you're mutating a class-level variable per-instance. The best bet is probably to change the APIschema_class/get_schemaand set theschema.viewthere.It wouldn't be too difficult to deprecate/change this:
schemais present (e.g., set by user on a subclass), the inspector/generator warns and uses the provided schema.get_schema.rpkilby commentedon May 16, 2018
btw, I'm happy to whip this together if the
schemadeprecation and move toschema_class/get_schemais okayed.carltongibson commentedon May 16, 2018
OK, we should at least look at it. (The same idea has come up a course le of times.)
Minimal, what does that look like? Followed by a fix for detail route.
carltongibson commentedon May 16, 2018
@rpkilby Thinking about this quickly, I think my favoured option would be to just add a
get_schema(), which by default went like this:(Do we need params? We have
self.request)I'll leave it with you. We can discuss further on a PR. 👍
rpkilby commentedon May 17, 2018
Hi @axnsan12 - you may be interested in #5992. If you have the time, it would be helpful if you could verify that the changes behave as expected.
axnsan12 commentedon May 17, 2018
Hello!
Tried it and yeah, seems to solve the issue. Great to see this implemented! 😄