question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OAS3 Resolving external schema containing allOf with $ref

See original GitHub issue

When resolving a $ref to an external schema containing a $ref withing an allOf that internal $ref is not resolved.

The file to resolve:

openapi: 3.0.0
info:
  version: 0.0.0
  title: oneOf and anyOf

paths:
  /oneOf:
    get:
      responses:
        '200':
          description: One of Cat or Dog
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: 'http://timotej.se/smartbear/pets.yaml#/components/schemas/Cat'
                  - $ref: 'http://timotej.se/smartbear/pets.yaml#/components/schemas/Dog'
  /anyOf:
    get:
      responses:
        '200':
          description: Any of Cat or Dog
          content:
            application/json:
              schema:
                anyOf:
                  - $ref: 'http://timotej.se/smartbear/pets.yaml#/components/schemas/Cat'
                  - $ref: 'http://timotej.se/smartbear/pets.yaml#/components/schemas/Dog'

Content of the external file:

openapi: 3.0.0
info:
  version: 0.0.0
  title: apa

components:
  schemas:
    Pet:
      required:
        - name
        - petType
      properties:
        name:
          type: string
        petType:
          type: string
    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            huntingSkill:
              type: string
              default: lazy
              enum:
                - lazy
                - aggressive
          required:
            - huntingSkill
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            packSize:
              description: The size of the pack the dog is from
              type: integer
          required:
            - packSize

Resolve using this code:

ParseOptions options = new ParseOptions();
options.setResolve(true);
OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml, null, options).getOpenAPI();
Yaml.pretty().writeValue(System.out, openAPI);

I would expect the output schema to contain Pet, Cat and Dog (since Pet is $ref:d from Cat and Dog). The actual value is only Cat and Dog. The $ref to Pet is now broken.

I have tested this with the currently latest version of 2.0 branch (1292a4e1a77fd4fdc3c4b1dfe29b3d15ac88fa7b)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gracekarinacommented, Jan 17, 2018

Yeah, it’s enough, I just added two more schemas 👍

0reactions
joeljonscommented, Jan 17, 2018

I see 😃 But that example you created is enough for you, or did you want me to create something?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using $ref - Swagger
OAS 3 This guide is for OpenAPI 3.0. Using $ref. When you document an API, it is common to have some features which...
Read more >
OpenAPI Specification v3.1.0 | Introduction, Definitions, & More
The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs.
Read more >
External Services OpenAPI 3.0 Schema - Salesforce Help
Here's an example of an API spec that contains a supported JSON schema for OpenAPI 3.0. ... Example 2: Named Object Schema Reference...
Read more >
json - Resolver error when referencing external schema files
Resolver error at paths./pet.get.responses.200.content.application/json.schema.allOf.1.properties.pets.items.allOf.0.$ref Could not resolve ...
Read more >
How to use JSON references ($refs) - Redocly
re-use of schemas or other content. Reference structure. $ref key. The JSON Reference uses a $ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found