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.15k stars 340 forks source link

Prism mock dynamic response generation missing required fields on oneOf/allOf schemas #570

Closed yunsim closed 4 years ago

yunsim commented 4 years ago

Describe the bug I used prism mock api.yaml -d to spin up a mock server for my openAPI spec, the dynamically generated responses always miss some required fields.

To Reproduce

  1. Sample API spec:
    openapi: 3.0.0
    info:
    title: Sample API
    version: v1
    paths:
    /services/resources/{resource_id}:
    get:
      summary: Get a resource detail.
      description: Returns a single resource.
      operationId: getV1Resource
      responses:
        200:
          description: get a resource
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Resource'
        404:
          description: not found
    components:
    schemas:
    Resource:
      type: object
      discriminator:
        propertyName: resourceType
      oneOf:
        - $ref: '#/components/schemas/A'
        - $ref: '#/components/schemas/B'
        - $ref: '#/components/schemas/C'
        - $ref: '#/components/schemas/D'
    A:
      required:
        - name
        - resourceType
      type: object
      properties:
        resourceType:
          type: string
        name:
          type: string
        color:
          type: string
        length:
          type: integer
    B:
      required:
        - name
        - resourceType
      type: object
      properties:
        resourceType:
          type: string
        name:
          type: string
        description:
          type: string
        age:
          type: integer
    C:
      required:
        - data
        - resourceType
      type: object
      properties:
        resourceType:
          type: string
        data:
          type: string
        password:
          type: string
        hint:
          type: string
    D:
      required:
        - value
        - resourceType
      type: object
      properties:
        resourceType:
          type: string
        value:
          type: string
        note:
          type: string
  2. Run this CLI command prism mock api.yaml -d
  3. Use postman to get sample response

Expected behavior The expected response should have field 'resourceType' for all, and should have 'name' field for A and B, but the actual responses never have those fields

Some actual responses;

{ "color": "elit irure voluptate sunt", "length": 80304934 }

{ "description": "deserunt ut in", "age": -65249860 }

{ "data": "exercitation", "password": "nisi", "hint": "magna enim ullamco Ut consequat" }

Environment (remove any that are not applicable):

Additional context if I use 'static' generation without '-d' in the command, I can see all the field but it will always return schema A.

e.g. { "resourceType": "string", "name": "string", "color": "string", "length": 0 }

XVincentX commented 4 years ago

Note: after an initial investigation this appear to be happening only for schemas using oneOf/allOf. It works correctly with plain schemas. I'm digging into the source code to understand what's wrong.


Ok apparently this is done on purpose by the library, so there's something going on we're missing.

image

Note 2: It works correctly if the oneOf has only one element:

image

Note 3: Effectively when there's more than a schema in a oneOf the derived target schema is missing the required properties:

image
XVincentX commented 4 years ago

@yunsim We have identified the bug and it's in the underlying library we're using to generate the data.

We're working with the author to fix this; there's not really any action point from our side at this point.

https://github.com/json-schema-faker/json-schema-faker/commit/16286c217336a84e5c7128a981f6ec1374371297#r35110880

XVincentX commented 4 years ago

@yunsim https://github.com/json-schema-faker/json-schema-faker/pull/524 — we're now at their mercy :)

yunsim commented 4 years ago

Thanks!

andreipaz commented 1 year ago

as das