prism icon indicating copy to clipboard operation
prism copied to clipboard

Mock response contains random fields

Open zkorhone opened this issue 3 years ago • 2 comments

Context

Generated mock response does not follow schema

Current Behavior

Given OpenAPI doc:

openapi: 3.0.3
info:
  title: Subscription Query API
  version: '1.0'
  description: Unified interface for querying Subscriptions A and B
servers:
  - url: 'http://localhost:3000/subscription-queries'
paths:
  /active_subscriptions:
    get:
      summary: Get active subscriptions
      tags: []
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/ActiveSubscription'
      operationId: get-active_subscriptions
      description: Get list of active subscriptions
      parameters:
        - schema:
            type: boolean
          in: query
          name: module_support
          description: 'When true return subscriptions that could theoretically have modules. When false or undefined, do not filter result set'
components:
  schemas:
    ActiveSubscription:
      title: ActiveSubscription
      type: object
      description: Active Subscription
      x-examples: {}
      x-internal: false
      properties:
        actions:
          type: object
          description: Actions available for caller
          properties:
            availableModules:
              type: string
              description: Allowed to query for available modules

Prims generates response:

[
  {
    "actions": {
      "availableModules": "ex",
      "dolore_3d4": -18659865,
      "in_aa3": false
    }
  },
  {
    "actions": {
      "availableModules": "irure aliqua Ut",
      "irure_f_": 13851100.013715371,
      "in_d3": "exercitation tempor nisi esse et",
      "nullaca3": 24122535.056890324,
      "esseb62": -46474555
    },
    "exercitationec_": 68917320.29432371
  },
  ....

Expected Behavior

Prism generates response that follows specification:

[
  {
    "actions": {
      "availableModules": "ex",
    }
  },

Possible Workaround/Solution

None

Steps to Reproduce

Start Stoplight Studio, open provided OpenAPI document & use Try it against Mock Server

Environment

  • Version used: Stoplight Sutdio 2.8.0-stable.7492.git-69a3957 (please make this easier to copy+paste!)
  • Environment name and version (e.g. Chrome 39, node.js 5.4): -
  • Operating System and version (desktop or mobile): Windows 10 (Desktop)
  • Link to your environment/workspace/project: -

zkorhone avatar Apr 22 '22 12:04 zkorhone

I also got this. this happens when there is only one key in the response. just adding another key will fix the issue. An issue that needs to be fixed

sreenadh-farmface avatar Apr 25 '22 05:04 sreenadh-farmface

Not sure if root cause it this, but it seems like json-schema-sampler is using global variables. If generator at any point is called in parallel (e.g. pipe), it might just generate unexpected result

zkorhone avatar Apr 25 '22 08:04 zkorhone

Could you check that your usage of additionalProperties is configured correctly as mentioned in https://github.com/stoplightio/prism/issues/1157?

The following is something we plan to look into:

this happens when there is only one key in the response. just adding another key will fix the issue.

chohmann avatar Mar 17 '23 16:03 chohmann

Interesting, I didn't know this was a thing! Is there any way I can specifiy additionalProperties = false for all the generated api responses, instead of having to add it to each api doc?

williamj20 avatar Mar 17 '23 17:03 williamj20

Todo: Let's add an additionalProperties override as a CLI parameter so that it doesn't have to be defined in every schema.

ryotrellim avatar Mar 31 '23 19:03 ryotrellim

Hey team! Please add your planning poker estimate with Zenhub @chohmann @EdVinyard @daniel-white @brendarearden

ryotrellim avatar Mar 31 '23 19:03 ryotrellim

Out of curiosity, any estimate when this will be resolved?

DawidOskwarek avatar Jul 13 '23 12:07 DawidOskwarek

@DawidOskwarek There is no estimate available at this time. We do welcome PRs! As a reminder, there is a workaround to set additionalProperties to false in your spec.

chohmann avatar Jul 14 '23 16:07 chohmann

An approach that requires no changes to either Prism or your OpenAPI document is add JSON Schema faker options to your file "just in time" using a tool like jq or yq.

For example, with the spec from the original example in a file named original.yaml you can use yq to add global JSON Schema faker options like this:

% yq '. += {"x-json-schema-faker":{"fill-properties":false}}' original.yaml > prism_input.json
% prism mock -d prism_input.json

Or you can store your faker options in a separate file and combine them just before you start Prism:

% yq -s '.[0] * .[1]' original.yaml faker_options.json > prism_input.json
% prism mock -d prism_input.json

where faker_options.json contains

{
  "x-json-schema-faker": {
    "fill-properties": false
  }
}

And if your OpenAPI documents are always YAML, you can do this even without jq:

% cat original.yaml faker_options.yaml > prism_input.yaml
% prism mock -d prism_input.yaml

where faker_options.yaml contains

x-json-schema-faker:
  fill-properties: false

EdVinyard avatar Aug 10 '23 14:08 EdVinyard

This will be released in a couple weeks, after we get in some more planned bug fixes.

chohmann avatar Aug 15 '23 18:08 chohmann

This has been released in version 5.3.0.

chohmann avatar Aug 17 '23 14:08 chohmann

Are we sure this was fixed? I'm on version 5.3.1 and still seeing the same 🤔 @chohmann Unless I'm doing something silly?? Is there any extra configuration needed on my end to get this working?

EDIT: Was looking at the PR that was merged to fix this, and it mentions that this needs to be set in the openapi spec x-json-schema-faker: fillproperties: false

However, the problem still exists even when the above is set.

EDIT2: I can see this working in the CLI when the flag is passed, but not when the values are set in the open api spec.

Also, could this be set in the createServer function possibly for people that are using an API of prism? It could be very beneficial

DawidOskwarek avatar Sep 11 '23 10:09 DawidOskwarek

I already asked about this in the PR, but probably it wasn't the appropriate place.

@chohmann is it possible to have a new official Docker image with this change — or version 5.3.1 — for those using the official Prism Docker image like I do?

Thank you very much!

juan-belmonte avatar Sep 15 '23 08:09 juan-belmonte

@DawidOskwarek and @juan-belmonte , if you're still seeing a problem with this, or have experienced other problems, please open a new issue. We generally don't monitor closed issues.

EdVinyard avatar Sep 15 '23 17:09 EdVinyard