We are building a new experience for our SmartBear Community.
This site is now read-only. You can still browse and search as usual. Learn More

Forum Discussion

PawelSwaggerHub's avatar
PawelSwaggerHub
New Contributor
6 years ago
Solved

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: [email protected]
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

  • 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

2 Replies

  • 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

    • PawelSwaggerHub's avatar
      PawelSwaggerHub
      New Contributor

      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.