stoplightio / prism

Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.
https://stoplight.io/open-source/prism
Apache License 2.0
4.22k stars 344 forks source link

Invalid response object is generated for cyclical schemas #2291

Closed RobertCraigie closed 1 year ago

RobertCraigie commented 1 year ago

Context

Given this OpenAPI spec which contains a circular schema definition:

openapi: 3.0.2
paths:
  /pet:
    get:
      responses:
        200:
          description: ok
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/return'
components:
  schemas:
    return:
      type: object
      properties:
        child:
          $ref: '#/components/schemas/return'
          nullable: true
      required:
        - child

Current Behavior

Prism responds with

{
  "child": {
    "$ref": null
  }
}

Expected Behavior

It should instead respond with

{
  "child": null
}

Possible Workaround/Solution

Explicitly setting an example for the child property to null

Steps to Reproduce

  1. Start a mock server for the given spec
  2. Run curl -sXGET http://127.0.0.1:4010/pet

Environment

chohmann commented 1 year ago

@RobertCraigie the spec you provided is invalid. $ref cannot have any other sibling properties. When you remove the invalid nullable: true from the spec you provided, Prism correctly mocks the response.

To represent the child property either being the return schema or null, you would need to use a oneOf.