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: warn for missing response examples #265

Merged
merged 1 commit into from
Mar 19, 2021
Merged

Conversation

barrett-schonefeld
Copy link
Contributor

Purpose:

  • Establish working pattern for writing custom functions such that, when users extend the ibm:oas ruleset, our custom functions are still findable by Spectral.
  • Tooling to generate documentation relies on response examples provided either in the schema or "next to" the schema at response level. Warn about responses that do not provide examples in these locations.

Changes:

  • Create a custom Spectral rule to warn about missing response examples
  • Create a custom Spectral function to check if a response example provided in the locations supported by the code that generates documentation.

Tests:

  • Add tests to ensure no warning given when a response example is provided.
  • Add a test to ensure a warning is given when no response example provided.

Docs:

  • Document the response-example-provided rule in docs/spectral-rules.md

Possible changes:

  • Include more than application/json responses. To do this, we would change the given field of the rule from $.paths[*][*].responses[*].content.application/json to $.paths[*][*].responses[*].content[*].
  • May want to condense the error message. I had writer's block, so I went with a verbose description initially.

Copy link
Member

@dpopp07 dpopp07 left a comment

Choose a reason for hiding this comment

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

Looks good to me!

src/spectral/rulesets/ibm-oas/response-example-provided.js Outdated Show resolved Hide resolved
Copy link
Contributor

@mkistler mkistler left a comment

Choose a reason for hiding this comment

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

This looks good but there's one more detail that you will need to handle. In OpenAPI 3.0 they introduced examples on parameters, request bodies, and response bodies. examples is a slightly more elaborate way to provide an example -- you can include not just the example value but also a description and summary. You can also have multiple named examples.

So in addition to checking for example you should also check for examples and only issue the message if neither is present.

src/spectral/rulesets/ibm-oas/response-example-provided.js Outdated Show resolved Hide resolved
test/spectral/tests/spectral-config.test.js Show resolved Hide resolved
@barrett-schonefeld
Copy link
Contributor Author

barrett-schonefeld commented Mar 18, 2021

This looks good but there's one more detail that you will need to handle. In OpenAPI 3.0 they introduced examples on parameters, request bodies, and response bodies. examples is a slightly more elaborate way to provide an example -- you can include not just the example value but also a description and summary. You can also have multiple named examples.

So in addition to checking for example you should also check for examples and only issue the message if neither is present.

I believe the current rule does allow for examples. In the negative test, the test that defines response examples and expects no warnings, one response uses examples to provide a response example. This links to the response that uses examples.

Purpose:
- Establish working pattern for writing custom functions such that, when users extend the `ibm:oas` ruleset, our custom functions are still findable by Spectral.
- Tooling to generate documenation relies on response examples provided either in the schema or "next to" the schema at response level. Warn about responses that do not provide examples in these locations.

Changes:
- Create a custom Spectral rule to warn about missing response examples
- Create a custom Spectral function to check if a response example provided in the locations supported by the code that generates documentation.

Tests:
- Add tests to ensure no warning given when a response example is provided.
- Add a test to ensure a warning is given when no response example provided.

Docs:
- Document the response-example-provided rule in `docs/spectral-rules.md`
@mkistler
Copy link
Contributor

Thanks Barrett, I missed that. Looks like you have it covered. Nice work!

Copy link
Contributor

@mkistler mkistler left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

@barrett-schonefeld barrett-schonefeld merged commit 0f10376 into main Mar 19, 2021
@barrett-schonefeld barrett-schonefeld deleted the response-example branch March 19, 2021 13:12
dpopp07 pushed a commit that referenced this pull request Mar 19, 2021
# [0.38.0](v0.37.1...v0.38.0) (2021-03-19)

### Features

* warn for missing response examples ([#265](#265)) ([0f10376](0f10376))
@dpopp07
Copy link
Member

dpopp07 commented Mar 19, 2021

🎉 This PR is included in version 0.38.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

None yet

3 participants