Reputation: 473
I'm trying to force dry-yasg
to properly parse parameters from path. Let's say we have
path('users/<int:user_id>/', whatever.as_view(...))
In swagger docs it is not treated as int
, but string
instead
I have used
swagger_auto_schema(manual_parameters = [
openapi.Parameter(
name,
openapi.IN_PATH,
description=desc,
type=openapi.TYPE_INTEGER,
required=True
)
]
but it's pretty annoying. I could not find a function/method/class responsible for parsing that. Is there a simple method to change behaviour of this parser based on path, so that if int
occurs then openapi.TYPE_INTEGER
will be returned instead of string
?
Upvotes: 2
Views: 1913
Reputation: 12090
drf-yasg determines the parameter type automatically in some situations, and falls back on string if detection fails.
queryset = get_queryset_from_view(view_cls)
for variable in sorted(uritemplate.variables(path)):
model, model_field = get_queryset_field(queryset, variable)
attrs = get_basic_type_info(model_field) or {'type': openapi.TYPE_STRING}
As you can see, it tries to get type based on column type of view queryset. If your parameter name doesn't match anything in the queryset though, you get a string. So your first choice should be to try use a name it can autodetect.
If that doesn't work however, you will need to subclass EndpointEnumerator
and override get_path_parameters()
, probably easiest to call super().get_path_parameters()
and go though each parameter and replace type based on variable name.
To use this class you will need your own OpenAPISchemaGenerator
.
- use a custom OpenAPISchemaGenerator
- override its endpoint_enumerator_class with your own EndpointEnumerator
Upvotes: 3