Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Oct 22, 2024

Conversation

ilanashapiro
Copy link
Contributor

@ilanashapiro ilanashapiro commented Aug 22, 2024

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 json-schema-faker/json-schema-faker#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:

tryCatch(() => sampler.sample(source, { ticks: 2500 }, operation), toError),

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
    • I tested these changes manually in my local or dev environment
  • Tests
    • Added or updated
    • N/A
  • Event Tracking
    • I added event tracking and followed the event tracking guidelines
    • N/A
  • Error Reporting
    • I reported errors and followed the error reporting guidelines
    • 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 ilanashapiro changed the title Feat: add --seed CLI flag for deterministic generation of dynamic examples feat: add --seed CLI flag for deterministic generation of dynamic examples Aug 22, 2024
@ilanashapiro ilanashapiro marked this pull request as ready for review August 22, 2024 18:12
@ilanashapiro ilanashapiro requested a review from a team as a code owner August 22, 2024 18:12
@ilanashapiro
Copy link
Contributor Author

@dgellow

@ilanashapiro
Copy link
Contributor Author

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

@brendarearden
Copy link
Contributor

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

Copy link

@dgellow dgellow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks a lot Ilana 👍

@dgellow
Copy link

dgellow commented Sep 21, 2024

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

@ilanashapiro
Copy link
Contributor Author

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!

@tesanchez
Copy link

@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.

@jack-bliss
Copy link

this is awesome, thanks!

@lukaszzazulak
Copy link
Contributor

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

@ilanashapiro
Copy link
Contributor Author

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

yarn.lock Outdated Show resolved Hide resolved
@lukaszzazulak lukaszzazulak merged commit 8edc1cc into stoplightio:master Oct 22, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants