-
Notifications
You must be signed in to change notification settings - Fork 354
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
Command-line flag to dynamically generate default valid examples in place of invalid static examples #2383
Command-line flag to dynamically generate default valid examples in place of invalid static examples #2383
Conversation
@ilanashapiro Thank you for your continued efforts for Prism! We appreciate that this is a difficult situation to run into, but do not feel that spec validation on its own is something that belongs in Prism. Prism's wheelhouse is validating requests against the spec people give it, not ensuring the spec provided is valid. We feel that spec validation on its own is better suited for other tools, such as our other open sourced tool, Spectral. We're going to close this PR, but thank you again for always putting effort into improving Prism! |
Hey @chohmann, thanks for your consideration here. I work with Ilana and asked her to take this on; I'd like to put in an appeal to reconsider. The purpose of this PR is not to perform spec validation, but to allow Prism to only return mock results that are valid, which as a Prism user very much feels like something I should be able to ask Prism to do, since it's quite painful to ask Prism to mock a server and then get invalid results, which can throw errors in many clients. As a user, my only alternative to this would be to rewrite the OpenAPI spec to remove invalid results prior to passing it to Prism, but that's fairly onerous in contexts where the OpenAPI spec isn't mine. In fact, our team has actually done this work – we have a tool that removes invalid examples from OpenAPI specs – but we felt that it should be Prism's responsibility to be able to always return correct results. As an alternative, a command-line flag to ignore all examples in responses could work for us too, but there's an obvious tradeoff there in that you throw out the baby (valid examples) with the bathwater (invalid examples). Does that make sense? Thoughts? |
@rattrayalex thank your for your response!
This may be true if you want to only mock in static mode (having prism return the examples defined in the spec). However, if you don't trust the validity of the spec, you can always mock in dynamic mode, and have prism generate a response that matches the schema, which would ensure the response would always be valid. As a user, if I've requested Prism to mock in static mode, and then Prism decides to mock in dynamic mode for some reason, unbeknownst to me, then I would think that was a bug. We would be more inclined for Prism to just notify the user in some way (i.e. via a log message) that the requested static example is not in compliance with the schema. |
Ah, thank you so much Chelsea! I don't think we knew about dynamic mode, or didn't realize it'd help with this case. Unfortunately, we seem to hit errors with it: #2081 (comment) and #2391 |
Actually, the docs on dynamic mode are a bit unclear – will it always generate different things on the fly, unlike static mode which will always use the same data? I don't actually want the data returned to change on each run of Perhaps what I really want is static mode, with an Apologies for my poor understanding of the current options & behavior! |
@chohmann Hi Chelsea, just wanted to follow up, would you accept a PR for the |
The team has decided that the
Have you considered updating your team's tool to use a tool like json-schema-faker/json-schema-sampler to generate a valid example to replace the invalid one it is removing? |
I'm not sure I understand? Empirically, adding properties does not break our tests (this happens regularly).
I'm not sure you're quite understanding our use-case. We are mocking third party OpenAPI specs - not specs we control. We want to avoid editing them as much as possible. Generating a prism-specific version of third-party specs is not great for a variety of reasons - happy to elaborate if it'd be helpful. My assumption was that mocking third-party specs is a common use-case for Prism users – is that incorrect? |
Hi @rattrayalex, I'm the product manager on @chohmann's team and helping to shepherd Prism. I'd like to better understand your use case and will be reaching out via email with a calendly link so we can connect. Here's my 2 cents until we chat:
|
Thanks @ryotrellim , I'll look out for an email! You can reach me at [email protected] I may misunderstand the nature/design of static vs dynamic mode – I was surprised to see dynamic differ so strongly from static, since static mode also invents fake/mock data, but we encountered several bugs with dynamic mode. Assuming dynamic mode tries to send different data every time (eg, 1, 0, -MaxInt, +MaxInt, etc for an int field), you're correct that the tests would get more coverage. However, I prefer to avoid "coverage across runs" in favor of "coverage within runs", so to speak. When I want to fuzz something, I do it in a dedicated fashion so that I can handle the edge-cases that are surfaced head-on. In the realities of a software team, test failures that arise from randomness (and happen rarely) are just likely to get ignored or ultimately swallowed by an auto-retry, and may be a pain to repro. Does that make sense? |
@rattrayalex after the meeting you had with @ryotrellim and company on Monday, we have a better understanding of your ask
We would accept a PR for this approach. And just to be specific this PR should:
|
Thank you Chelsea! @ilanashapiro is excited to get to work on those items soon! |
PR #2408 is out! |
Addresses #2384
Summary
This PR introduces a command line flag,
--defaultExamples
or--de
that, when mocking a schema, indicates that if the static example to be returned in the response body is invalid (i.e. the example results in validation errors), a valid example is dynamically generated instead.Currently, Prism's behavior is to log validation errors without returning errors if the --errors flag is omitted. In this case, the request "flows to the actual API and returns what it returns." Valid examples can be dynamically generated by including the --dynamic flag, which will ignore all provided static examples and skip the validation process (the --dynamic flag overrides the --errors flag). However, there is currently no option to use the static example only if it is valid, and to otherwise log the validation errors and dynamically generate a valid example in place of the invalid one. This command line flag introduces this behavior.
Checklist
Screenshots
Consider the following schema:
Note that the first two examples (cat, dog) are invalid, and third (bird) is valid.
By mocking this schema with the
--defaultExamples
command line flag, we successfully get the following valid dynamically generated example in place of the invalid "cat" example that would be returned otherwise:Without the
--defaultExamples
flag, we see how the invalid "cat" example gets returned:With the
--defaultExamples
flag, the same behavior of dynamically generating valid examples holds for when we manually select an invalid example using the Prefer header:However, if we select a valid example, it still gets returned successfully: