prism: Support colocated json files in externalValue property for response bodies in static mock mode

In general I am not 100% sure if this is a feature request or a bug report, but I guess this could be a matter of perspective.

User story. As a prism user, I can use the externalValue property in my openapi specification, so that I can provide request and response examples through external json files.

Is your feature request related to a problem? We have several example request and response bodies in json files colocated to our openapi specification that we provide to the specification through the externalValue property. When running prism in static mode I would expect that these externalValue examples are also considered for the response bodies that are delivered in the static mock mode.

When we have the following openapi specification api_specification_minimal.yaml:

openapi: 3.0.0
info:
  title: Book Store API
  version: 1.0.0
servers:
  - url: 'https://example-book-store.com'
    description: Test Environment
paths:
  /backend/book:
    get:
      operationId: getBooksCollection
      summary: Returns a list with all books in the system
      responses:
        '200':
          description: A list of all books
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ArrayOfBooks'
              examples:
                default:
                  externalValue: 'examples/backend/books/getBooksCollectionResponse.json'
                empty:
                  externalValue: 'examples/backend/books/getBooksCollectionResponseEmpty.json'
components:
  schemas:
    IdReadOnly:
      type: string
      pattern: '[a-fA-F0-9]{24}'
      readOnly: true
      example: 5ea736d41142bb6d740f2dd2
    ArrayOfBooks:
      type: array
      items:
        $ref: '#/components/schemas/Book'
    Book:
      required:
        - title
      properties:
        id:
          $ref: '#/components/schemas/IdReadOnly'
        title:
          type: string
        author:
          type: string
        price:
          type: number
      type: object

whereas colocated to the api_specification_minimal.yaml we have:

examples/books/getBooksCollectionResponse.json:

[
    {
        "id": "5ea7d0df998cba41316d9ab2",
        "title": "Hamlet",
        "author": "William Shakespeare",
        "price": 123
    }
]

examples/books/getBooksCollectionResponseEmpty.json:

[]

When I now run prism mock api_specification_minimal.yaml : image

and call GET http://localhost:4010/backend/book from e.g. Postman, I am getting a 200 response, but there is no content in the response: image

image

Whereas dynamic mode works perfectly - prism mock api_specification_minimal.yaml -d: image image

Describe the solution you’d like When running prism in the static mock mode with an openapi specification that provides example response bodies through the externalValue property I would expect that those examples are delivered through the mock server

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 18 (10 by maintainers)

Most upvoted comments

Got it. I can’t really give an ETA for this, but it seems like people want this so we will try to figure out something.

@juan-belmonte , no problem. You’re very close! Here’s an example:

issue1155.oas

openapi: 3.1.0
paths:
  /:
    get:
      responses:
        '200':
          description: success
          content:
            "application/json":
              examples:
                red:
                  value:
                    $ref: 'issue1155.json'
                blue:
                  value:
                    color: "blue"

issue1155.json

{
    "color": "red"
}

Running Prism as prism-cli mock issue1155.oas, I then get…

% curl -s localhost:4010/ -H 'Prefer: example=red'
{"color":"red"}

I think I have the same need, I’m getting the following error when I try to use an external PDF file as example:

{
    "type": "https://stoplight.io/prism/errors#UNKNOWN",
    "title": "Cannot find serializer for application/pdf",
    "status": 500,
    "detail": ""
}

The OpenAPI spec looks as follows:

responses:
        '200':
          description: Some description
          content:
            application/pdf:
              examples:
                example1:
                  externalValue: https://some.address.com/my.pdf

$ref doesn’t work too.