prism icon indicating copy to clipboard operation
prism copied to clipboard

feat: add --seed CLI flag for deterministic generation of dynamic examples

Open ilanashapiro opened this issue 1 year ago • 9 comments

Summary

Addresses issue #2590

We introduce a new CLI flag --seed that the user sets to a string. This string is feed as a seed into json-schema-faker as described in https://github.com/json-schema-faker/json-schema-faker/issues/111, and allows for reproducible dynamic examples, which is useful in testing. The user can also set the seed in the Prefer header.

NOTE: if you are using --ignoreExamples for Prism to statically generate examples conforming to a schema, you do NOT need to use the --seed flag as this will have no effect. --ignoreExamples uses json-schema-sampler to statically generate examples, not json-schema-faker:

https://github.com/stoplightio/prism/blob/0d12d25b1907385a6f84a3634c8aedd56f113ea4/packages/http/src/mocker/generator/JSONSchema.ts#L107

and json-schema-sampler is already deterministic: https://github.com/stoplightio/json-schema-sampler#:~:text=Deterministic%20(given%20a%20particular%20input%2C%20will%20always%20produce%20the%20same%20output).

Checklist

  • The basics
    • [x] I tested these changes manually in my local or dev environment
  • Tests
    • [x] Added or updated
    • [ ] N/A
  • Event Tracking
    • [ ] I added event tracking and followed the event tracking guidelines
    • [x] N/A
  • Error Reporting
    • [ ] I reported errors and followed the error reporting guidelines
    • [x] N/A

Screenshots We mock the following .yaml spec:

openapi: 3.0.2
paths:
  /pets:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                '$ref': '#/schemas/Pet'
              examples:
                cat:
                  summary: An example of a cat
                  value:
                    id: "string_id"
                    name: 123
                    photoUrls: [[]]
                    status: 135

schemas:
  Pet:
      type: object
      required:
      - name
      - photoUrls
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
          example: doggie
        photoUrls:
          type: array
          xml:
            name: photoUrl
            wrapped: true
          items:
            type: string
        status:
          type: string
          description: pet status in the store
          enum:
          - available
          - pending
          - sold

Mocking with --dynamic and --seed gives us reproducible dynamic responses: image image

Vs mocking with just --dynamic gives us random/variable dynamic responses: image image

Finally, if you mock in dynamic mode but without a seed specified, you can still add the seed in the Prefer header. Note how in the following example, the first curl request doesn't add a Prefer header, but the second and third do and specify the same seed, and the second and third responses are identical to one another. image

image

ilanashapiro avatar Aug 22 '24 07:08 ilanashapiro

@dgellow

ilanashapiro avatar Aug 22 '24 18:08 ilanashapiro

Hi @brendarearden! Could we request a review on this from someone on your team? Thank you!

ilanashapiro avatar Sep 06 '24 21:09 ilanashapiro

Hi @ilanashapiro! I am no longer employed here but I think @lukaszzazulak might be able to help you out!

brendarearden avatar Sep 16 '24 18:09 brendarearden

@lukaszzazulak Can we do something to help speed up a review of this PR?

dgellow avatar Sep 21 '24 11:09 dgellow

Following up -- could @lukaszm-sb or @lukaszzazulak please help us get a review on this either by you or someone else on the Prism team? Thanks!

ilanashapiro avatar Oct 01 '24 18:10 ilanashapiro

@dgellow Thanks for your commitment here. We are looking into some issues we have with the automation process for deployment for some of the dependencies. As soon as we have some free time we will try to get to reviewing this PR.

tesanchez avatar Oct 04 '24 13:10 tesanchez

this is awesome, thanks!

jack-bliss avatar Oct 07 '24 14:10 jack-bliss

@ilanashapiro sorry for the delay, could you resolve conflicts ?

lukaszzazulak avatar Oct 18 '24 11:10 lukaszzazulak

@lukaszzazulak conflicts are resolved! thank you for approving the changes!

ilanashapiro avatar Oct 20 '24 07:10 ilanashapiro