mockserver icon indicating copy to clipboard operation
mockserver copied to clipboard

Resusable examples ($ref) not converted by mock server

Open Ellosaur opened this issue 3 years ago • 1 comments

Describe the issue Example responses that reference reusable examples ($ref) do not return the expected response.

What you are trying to do Have reusable examples to enrich the data returned from our mock, while not having to copy/paste lots of similar reponse objects into different endpoints.

MockServer version latest docker tag (5.14.0)

To Reproduce Create a swagger that contains an examples section in components:

  examples: 
    firstTaskExample:
      value:
        attributes:
          taskStatus: 
            code: 2006
            description: "Pending"
          createdTime: "2019-10-10T20:20:20Z"
          lastUpdatedTime: "2019-10-11T20:20:20Z"

    secondTaskExample:
      value:
        attributes:
          taskStatus: 
            code: 1000
            description: "Completed"
          createdTime: "2019-10-10T20:20:20Z"
          lastUpdatedTime: "2019-10-11T21:20:20Z"

Reference them in other objects:

    taskListResponse:
      title: taskListResponse
      type: object
      properties:
        data:
          type: array
          items:
            $ref: "#/components/schemas/taskSingleResponse"
          example:
            - $ref: '#/components/examples/firstTaskExample/value'
            - $ref: '#/components/examples/secondTaskExample/value'

Ultimately reference them as an example response

  /tasks:
    get:
      summary: Get a list of tasks by the tasktype and organisation
      responses:
        "200":    # status code
          description: Success - a list of tasks
          content:
            application/vnd.api+json:
              schema:
                $ref: "#/components/schemas/taskListResponse"

Observe in Swagger Editor the example response correctly rendered:

"data": [
    {
      "attributes": {
        "taskStatus": {
          "code": 2006,
          "description": "Pending"
        },
        "createdTime": "2019-10-10T20:20:20Z",
        "lastUpdatedTime": "2019-10-11T20:20:20Z"
      }
    },
    {
      "attributes": {
        "taskStatus": {
          "code": 1000,
          "description": "Completed"
        },
        "createdTime": "2019-10-10T20:20:20Z",
        "lastUpdatedTime": "2019-10-11T21:20:20Z"
      }
    }
]

Initialise mock server from the same swagger and call the endpoint to get unexpected response

{
  "data" : [ {
    "$ref" : "#/components/examples/firstTaskExample/value"
  }, {
    "$ref" : "#/components/examples/secondTaskExample/value"
  } ]
}

Expected behaviour The mock server response should be the same as in the swagger editor

MockServer Log No clues here

Ellosaur avatar Sep 06 '22 15:09 Ellosaur

Included a bit of a simpler example below to help get to the root of the issue: image

openapi: 3.0.0
info:
  description: Example issue
  version: 1.0.4
  title: Mock Server Issue

paths:
  /example:
    get:
      summary: Get path with reusable example
      responses:
        "200":    # status code
          description: Success
          content:
            application/vnd.api+json:
              schema:
                type: object
                description: a response schema
                properties:
                  data:
                    type: object
                    description: response data
                    properties:
                      attributes:
                        type: object
                        properties:
                          id:
                            type: string
                            example: "Schema example id"
                          name:
                            type: string
                            example: "Schema example name"
                    example: 
                      $ref: '#/components/examples/objectExample/value'

components:
  examples:
    objectExample:
      value:
        id: "Resuable example id"
        name: "Resuable example name"
      summary: A sample resuable example object

Ellosaur avatar Sep 07 '22 08:09 Ellosaur