cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
New Contributor

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

1 ACCEPTED SOLUTION

Accepted Solutions
Moderator

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

Hi Pawel,

As you've correctly noted, SwaggerHub always returns the first example if multiple examples are provided. SwaggerHub mock server does not support looping through responses or examples, but this is supported in our service virtualization product ServiceV.

https://smartbear.com/product/ready-api/servicev/overview/

https://support.smartbear.com/readyapi/docs/servicev/configure/dispatch/index.html


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

2 REPLIES 2
Moderator

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

Hi Pawel,

As you've correctly noted, SwaggerHub always returns the first example if multiple examples are provided. SwaggerHub mock server does not support looping through responses or examples, but this is supported in our service virtualization product ServiceV.

https://smartbear.com/product/ready-api/servicev/overview/

https://support.smartbear.com/readyapi/docs/servicev/configure/dispatch/index.html


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

New Contributor

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

Thanks Helen,

 

That's great to know, I finally realised that this is the same as the Swagger tool here https://swagger.io/solutions/mocking-and-virtualization/ thanks for clarifying.

 

New Here?
Join us and watch the welcome video:
Announcements