Drf-yasg: Using ReferenceResolver in custom APIView

Created on 3 Mar 2019  路  5Comments  路  Source: axnsan12/drf-yasg

I want to use references like $ref: "#/definitions/MyResponse" in the Swagger file.

My code looks like this:

resolver = ReferenceResolver(SCHEMA_DEFINITIONS)
resolver.with_scope(SCHEMA_DEFINITIONS).set(
    'MyResponse', Schema(type=TYPE_STRING)
)

And the view decorator:

@swagger_auto_schema(
    responses = {200: Response('response description', SchemaRef(resolver, 'MyResponse'))},
)

As a result, the path object itself gets described correctly including the $ref reference, but the definitions: section in the Swagger file keeps empty and does not list MyResponse there, yielding an illegal Swagger file.

Is this an issue in the library or did I use it in a wrong way?

This may be related to #211?

Something more:
The code example given in the Documentation at ReferenceResolver did not answer this and also seems to be syntactical wrong here: definitions = ReferenceResolver.with_scope('definitions')

Most helpful comment

I'd love a more fleshed out version of this approach.

All 5 comments

You are right, this is most certainly not what you want. There isn't currently a simple way to add a reference objectat "compile time" - as said in #211, you'll have to do it dynamically.

Thanks for the feedback. Let me tell you why this is needed for me:
Some Swagger code generators (at least TypeScript fetch, what I am using) use the definition settings to automatically create data types (or Interfaces, in TypeScript).

I'm aware of that. You'll just have to insert the reference in code that runs at request time (SwaggerAutoSchema, inspectors, views, etc) instead of with a decorator at initialization time.

I'd love a more fleshed out version of this approach.

What do you mean by dynamically adding this? Is there an example somewhere?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

phihag picture phihag  路  5Comments

beaugunderson picture beaugunderson  路  5Comments

hnykda picture hnykda  路  3Comments

nicholasgcoles picture nicholasgcoles  路  4Comments

iamhssingh picture iamhssingh  路  3Comments