OpenAPI.NET icon indicating copy to clipboard operation
OpenAPI.NET copied to clipboard

Question about `OpenApiWalker` and references

Open peteraritchie opened this issue 4 years ago • 0 comments

I'm confused walker and how it deals with references. In particular, referenced schemas. For example, if I have a spec that has no references, for each referenceable element the walker calls Visit(OpenApiSchema) in the context of the element. For example, if I have a response like this:

paths:
  '/resource/resource/{id}'
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/resourceBody'

Visit(OpenApiSchema) is called with a path of (#/paths/~1resources~1resource~1{id}/get/responses/200/content/application~1json/schema) With a Path, Operation, Response, Content context objects.

if I have a spec where everything is a reference, like this:

paths:
  '/resource/resource/{id}'
      responses:
        '200':
          $ref: '#/components/responses/success-response'
components:
  responses:
    success-response:
      description: Success
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/resourceBody'

Visit(OpenApiSchema) is never called with a context and the path refers into components. This means I'd have to either return to paths/responses/parameters/requestBodies/etc. with the schema details. OR, I'd have to traverse all the references in paths and resolve each at Visit(OpenApiDocument). I've tried calling OpenApiDocument.ResolveReferences before walking the document, but the result is the same.

Digging into Walk(OpenApiSchema, bool, there's a line in there that processes the schema as a referenceable if the Reference property is not null; even though ResolveReferences may have resolved it. ProcessAsReferenceable passes along the schema to Visit(OpenApiReferenceable) but then doesn't continue as a schema, bypassing schema visits into allOf, oneOf, items, anyOf, and properties.

This leaves me with manually traversing referenced schemas, which isn't what I'd expect within a visitor.

What am I missing?

peteraritchie avatar Jul 16 '21 20:07 peteraritchie