Migrate dynamic schema for extra fields from drf-yasg
See original GitHub issueNot really a bug, but looking for the “best” (i.e. easiest) way to migrate a somewhat more complext construct from drf-yasg
.
There is some existing code in this project that adds two Mixins
to add a new parameter and a new response field to the list
and read
methods.
I am linking to the code as it’s not so easy to isolate everything here.
The idea is that all models there is a parameter prefetch
that can be popluated with a comma seperated list of relationship names. So for a child, the parameter could contain parent. And then if a list of childs is being retrieved, the mixin will collect all the pk’s of the parents. All these parent models are retrieved from the database and added to a new field prefetch
in the response. The idea is that in one API call the children can be retrieved, including all relationships, in this example parents.
So the schema is dynamic as it depends on the relationships a model has. All the code for this with drf-yasg is in place and working, but it’s quite a bit a boiler plat code with “Composable” schema’s, Lazy references etc. So I am pondering what the easier way would be to achieve the same in drf-spectacular
.
Anyone any thoughts? My first thought would be to write a post processor that just adds the parameter and response field + their schema’s.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Managed to implement it using a PostProcessor: https://github.com/DefectDojo/django-DefectDojo/pull/4541/commits/97d47552e00bd8144ce2db710f8016534ad6a61a Maybe not the cleanest method, but quite portable as it almost doesn’t use any
drf-spectacular
code.I am trying to do more or less this:
prefetch
parameterFor example if a model has 2 relationships (members and product_manager), the prefetch parameter and prefetch responses will have this schema:
In my current implementation I just went over all the paths in the
result
parameter in the post processor. But then I miss information to do step 3 as the result doesn’t have the serializers/views. But maybe the generator has it somewhere, for example in_get_paths_and_endpoints
? I looked at the registry but that just contains the components and not the views/parameters/serializers.