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

Use all property examples in generated mock responses #124

Merged
merged 3 commits into from
Jun 14, 2024

Conversation

martinsirbe
Copy link
Contributor

@martinsirbe martinsirbe commented May 28, 2024

Currently when the response mock engine generates a response using the MockGenerator, the generated responses contain only examples for individual properties that are marked as required, see tests for more details.

The reason why generated responses contain only examples for required properties is a side-effect of using mock generator for validation. While it makes sense for validating required fields for requests, it shouldn't affect the response generation.

Changes:

  • By default, disables the required check on the response mock engine to ensure that all examples for individual properties are used in generated mock responses.
  • Introduces a new configuration option, enable-all-mock-response-fields, to allow only the use of required field examples in the generated mock responses when set to false.

This fix uses pb33f/libopenapi#295

@martinsirbe martinsirbe changed the title Fix response mock engine generated examples Use all property examples in generated mock responses May 29, 2024
Copy link
Member

@daveshanley daveshanley left a comment

Choose a reason for hiding this comment

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

I think this needs to be a configurable switch, I am OK with the default being set to off, but there needs to be a way to turn it on via the configuration.

@martinsirbe
Copy link
Contributor Author

I think this needs to be a configurable switch, I am OK with the default being set to off, but there needs to be a way to turn it on via the configuration.

I agree that this makes sense. I suggest using the Go variadic Option pattern for configuring the mock engine on initialisation. This allows us to add optional configuration without breaking the NewMockEngine initialisation function signature. Another approach could be to add a new function on MockEngine to enable the required check, but I prefer the first option for its flexibility and non-breaking nature.

Thank you for your input and guidance! If you have other ideas or suggestions, I'd love to hear them too.

@daveshanley
Copy link
Member

As long as the ability to 'enable all mock fields' when in mock mode exists as part of the wiretap config (https://pb33f.io/wiretap/configuring/) then the first option sounds OK to me.

Copy link
Member

@daveshanley daveshanley left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you for your contribution!

@daveshanley daveshanley merged commit 0ac01b6 into pb33f:main Jun 14, 2024
2 checks passed
@martinsirbe martinsirbe deleted the fix-mock-responses branch June 15, 2024 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants