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(oas): add oas3-unused-component rule which detects all orphaned components #1440

Merged
merged 4 commits into from
Jan 28, 2021

Conversation

mkistler
Copy link
Contributor

@mkistler mkistler commented Jan 2, 2021

This PR creates a new oas3 rule oas3-unused-component which detects and reports any unreferenced component (except securitySchemes, which are not "referenced" in the normal way) in an OpenAPI document.

Checklist

  • Tests added / updated
  • Docs added / updated

Does this PR introduce a breaking change?

  • Yes
  • No

Additional context

I'd like to replace the logic in our validator for finding and reporting unused components -- which is not configurable -- with a Spectral rule so that users can override the severity for their out purposes. We've gotten requests from users for this capability:

IBM/openapi-validator#172

But I can't use oas3-unused-components-schema because this would be a regression -- our logic detects all of types of unused components: schemas, parameters, responses, requestBodies, headers, examples, links, and callbacks.

So this PR creates a new rule that is a superset of oas3-unused-components-schema that detects all unused components (except securitySchemes). It leverages the underlying unreferecedReusableObject function internally, so the logic is (IMHO) very straightforward.

Because this new rule is a superset of oas3-unused-components-schema, I changed that rule to be "recommended: false" and set the new rule to "recommended: true"

@mkistler mkistler force-pushed the mdk/oas3-unused-component branch 2 times, most recently from a728d67 to 1ea8aea Compare January 16, 2021 15:20
@mkistler
Copy link
Contributor Author

@P0lip I think this is now ready for review. I've rebased onto the current develop branch and added documentation for the new rule. I also figured out how to run the test harness so I could fix problems there myself (just one little one this time).

Copy link
Contributor

@P0lip P0lip left a comment

Choose a reason for hiding this comment

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

I like it!
Let's just drop the other obsolete now rule and this should be good to go.

@@ -643,7 +644,7 @@
},
"oas3-unused-components-schema": {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say we can drop the rule at this point.
oas3-unused-component somewhat overlaps with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dropping the rule entirely might cause unexpected results for users with a configuration that starts by disabling all rules and then selectively enables rules by name. Since Spectral does not issue a warning about rules it does not recognize in the configuration file, Spectral will silently stop linting their unused schemas.

But I'm happy to remove the rule if you think this is acceptable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I'd say let's do it, but curious to hear what @philsturgeon thinks about the idea.

Since Spectral does not issue a warning about rules it does not recognize in the configuration file, Spectral will silently stop linting their unused schemas.

We're thinking to change it in the next major version. It's going to throw an error. Failing silently is usually not intended.

Copy link
Contributor

Choose a reason for hiding this comment

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

The next version should be a major release so the impact should not be too unexpected, we'll just have to communicate it clearly.

src/rulesets/oas/functions/oasUnusedComponent.ts Outdated Show resolved Hide resolved
src/rulesets/oas/functions/oasUnusedComponent.ts Outdated Show resolved Hide resolved
@mkistler
Copy link
Contributor Author

I've updated the PR to remove the oas3-unused-components-schema rule and associated tests. The yard test.prod tests all pass on my local system. I see that the circleci tests are failing on Linux but I don't know if that's anything to do with my changes. So I think this PR is ready to review/merge but if there is something else I need to do just let me know.

philsturgeon
philsturgeon previously approved these changes Jan 27, 2021
Copy link
Contributor

@philsturgeon philsturgeon left a comment

Choose a reason for hiding this comment

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

Amazing work, thank you so much @mkistler!

P0lip
P0lip previously approved these changes Jan 27, 2021
Copy link
Contributor

@P0lip P0lip left a comment

Choose a reason for hiding this comment

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

Thanks a bunch! Great job.
Actually, your PR addressed #1073.

test-harness/scenarios/enabled-rules-amount.oas3.scenario Outdated Show resolved Hide resolved
@P0lip P0lip changed the title Add oas3-unused-component rule which detects all orphaned components feat(oas): add oas3-unused-component rule which detects all orphaned components Jan 27, 2021
@P0lip P0lip added the enhancement New feature or request label Jan 27, 2021
Co-authored-by: Jakub Rożek <jakub@rozek.tech>
@mkistler mkistler dismissed stale reviews from P0lip and philsturgeon via 2fd70f8 January 27, 2021 23:13
@P0lip P0lip merged commit 3a59055 into stoplightio:develop Jan 28, 2021
@mkistler mkistler deleted the mdk/oas3-unused-component branch January 28, 2021 13:16
@P0lip P0lip added the breaking Pull requests that introduce a breaking change label May 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Pull requests that introduce a breaking change enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants