Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drf_yasg doesn't take TYPE_ARRAY as a valid type

the drf_yasg swagger generator doesnt take TYPE_ARRAY as a valid parameter type. the implementation is as follows

from drf_yasg import openapi
param1 = openapi.Parameter('param_name',
                            in_=openapi.IN_QUERY,
                            description='description of param',
                            type=openapi.TYPE_ARRAY,
                            required=True
                            )

whereas the documentation of drf_yasg suggests that it takes openapi.TYPE_ARRAY as valid type.

the error that the generators throws is

File "/usr/local/lib/python3.6/dist-packages/drf_yasg/codecs.py", line 73, in encode
    raise SwaggerValidationError("spec validation failed", errors, spec, self)
drf_yasg.errors.SwaggerValidationError: spec validation failed

is there some configuration that i am missing or something because TYPE_STRING,TYPE_NUMBER works perfectly fine.

like image 629
anubysh Avatar asked Jul 16 '26 07:07

anubysh


1 Answers

A Parameter of TYPE_ARRAY requires the items key:

param1 = openapi.Parameter('param_name',
                            in_=openapi.IN_QUERY,
                            description='description of param',
                            type=openapi.TYPE_ARRAY,
                            items=openapi.Items(type=openapi.TYPE_STRING)  # <------
                            required=True
)

See the OpenAPI 2.0 specification for more details.

like image 197
axnsan Avatar answered Jul 20 '26 14:07

axnsan