Forum Discussion

PawelSwaggerHub's avatar
PawelSwaggerHub
New Contributor
5 years ago

How to mock multiple examples with swaggerhub API Mocking tool - OpenAPI 3

Hi all,

As per OpenAPI 3 we can have multiple examples. Swaggerhub provides auto-mocking which by default returns the first example (Jessica). I can't find a way to return the second example so in the below I'd like Ron to be returned.

Can anyone suggest how to achieve that with Swaggerhub?

 

 

 

openapi: 3.0.0
servers: 
  - url: https://localhost/account
info:
  description: This is a simple API
  version: "1.0.0"
  title: Simple Inventory API
  contact:
    email: you@your-company.com
paths:
  /users:
    post:
      summary: Adds a new user
      requestBody:
        content:
          application/json:     # Media type
            schema:             # Request body contents
              $ref: '#/components/schemas/User'   # Reference to an object
            examples:    # Child of media type
              Jessica:   # Example 1
                value:
                  id: 10
                  name: Jessica Smith
              Ron:       # Example 2
                value:
                  id: 11
                  name: Ron Stewart
      responses:
        200:
          description: A user object.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'   # Reference to an object
              examples:
                Jessica:
                  value:
                    id: 10
                    name: Jessica Smith
                Ron:
                  value:
                    id: 20
                    name: Ron Stewart
                    
components:
  schemas:
    User:    # Schema name
      type: object
      properties:
        id:
          type: integer
          format: int64

 

 

 

 

 

Mocked api example in swaggerhub as per above example.

POST

https://virtserver.swaggerhub.com/myapimock/testExamplesUsers/1.0.0/users

 

In prism/stoplight which implements OpenAPI v 3 you can do this by adding Prefer key to the header. It would be strange for SwaggerHub to no leverage this functionality so I'm assuming I missed something.

 

Many thanks.

 

References:

https://swagger.io/docs/specification/adding-examples/ 

https://app.swaggerhub.com/help/integrations/api-auto-mocking