Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to do partial references in examples sections? #1656

Closed
JehandadK opened this issue Aug 6, 2018 · 4 comments
Closed

Is it possible to do partial references in examples sections? #1656

JehandadK opened this issue Aug 6, 2018 · 4 comments
Labels

Comments

@JehandadK
Copy link

JehandadK commented Aug 6, 2018

Regarding OAS3:

  • Can we use $ref in array of examples?
  • Can we use example as well examples object together?
  • Can we partially edit examples like in schema properties.

Sorry had to ask, had been reading specs but coudn't understand. Would be willing to create and add this to examples since this is very important to keep your specs DRY.

  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
              examples:
                all_pets:
                  $ref: "#/components/examples/AllPets"
                all_pets1:
                  $ref: "#/components/examples/AllPets1"
                cats:
                  $ref: "#/components/examples/CatPets"
components:
  schemas:
    Pet:
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      items:
        $ref: "#/components/schemas/Pet"
  examples:
    Dog:
      value:
          id: 1
          name: "Dog"
          tag: "dog"
    Cat:
      value:
          id: 2
          name: "Cat"
          tag: "cat"   
    AllPets:
      summary: "Array of pets"
      value:
        - $ref: "#/components/examples/Dog"
        - $ref: "#/components/examples/Cat"
    CatPets:
      summary: "Only Cats"
      value:
        - $ref: "#/components/examples/Cat"  
    AllPets1:
      - id: 1
        name: "Dog"
        tag: "dog"
      - id: 2
        name: "Cat"
        tag: "cat"
@JehandadK
Copy link
Author

Swagger UI omits examples and hence I cannot validate above spec is correct or not. Ideally I guess many people would like to reference examples to create other examples, or overriding them.

@djbrown
Copy link

djbrown commented Aug 27, 2018

@handrews
Copy link
Member

Yes, you can reference examples, example (singular) is for use inside schemas, while examples (plural) is for use elsewhere in the OAS document, and no you can't use both eno you cannot edit the examples in the process of referencing them.

I think this covers everything, and given the age of the question I'm going to go ahead and close this. Please re-open or open a new issue if you have further questions.

@msageryd
Copy link

I stumbled on a nice blog post just after reading this thread. I know this issue is closed, but I ended up here anyway after searching so I thought I'd drop a link to the blog post for anyone else who comes here for the same info.

Still no info on partial references, but it explains the example/examples mess.
https://phil.tech/2020/openapi-examples/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants