open-api-mocker icon indicating copy to clipboard operation
open-api-mocker copied to clipboard

Match Example Request to Example Response by name

Open joelbyler opened this issue 1 year ago • 1 comments

I'm not sure if this is a common use case but wanted to ask to see if this would be a useful feature. In my OpenAPI spec I have multiple example requests and corresponding responses for them. Would it make sense to match these by name so that a request which matches an example in the spec will look for a corresponding example response and return that? From my perspective it would be a better option than passing a Prefer header to specify the desired response. Looking for feedback on this idea and if its popular I can attempt a PR.

joelbyler avatar Apr 14 '23 14:04 joelbyler

If you need an example, something like this is what I had in mind

openapi: 3.0.0
info:
  version: 1.0.0
  title: Example API
  description: Example API for GH Issue

paths:
  /widgets:
    post:
      description: Create a New Widget
      requestBody:
        description: Creates and persists a new widget
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                color:
                  type: string
              required:
                - name
            examples:
              successful:
                value:
                  bin: Fred
                  pcn: green
              failure:
                value:
                  color: blue
      responses:
        '200':
          description: Successfully predicted
          content:
            application/json; charset=UTF-8:
              schema:
                type: object
                properties:
                  id:
                    type: number
                  name:
                    type: number
                  color:
                    type: number
                  success:
                    type: boolean
                  message:
                    type: string
              examples:
                successful:
                  value:
                    id: 42
                    cost: 99.99
                    name: Fred
                    color: green
                    success: true
                failure:
                  value:
                    success: false,
                    message: name is a required property of a new widget

So if I send a request with params that match the successful example params it would be great for the successful example response to be sent back. And same for the failure examples.

joelbyler avatar Apr 14 '23 14:04 joelbyler