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
remove unused components was skipping schemas with a top-level allOf #1220
base: main
Are you sure you want to change the base?
remove unused components was skipping schemas with a top-level allOf #1220
Conversation
🦋 Changeset detectedLatest commit: 9277248 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
805ff4a
to
68b0861
Compare
718aa7b
to
97c2c06
Compare
@tatomyr I've updated the code to deal with using discriminator from a referenced allOf. Still not 100% sure this is case that drew the original concern. I should still add a test for the rule change but I'm less familiar with how rules are tested (vs. decorators) but wanted to push this up to see if this was what you were referring to. |
6d34f79
to
069d3e7
Compare
@tatomyr I've added the rule test. To summarize:
I believe this addresses the original FIXME. |
Open questions:
|
Please use minor (replace the existing one). |
069d3e7
to
a1ba2e7
Compare
Done |
a1ba2e7
to
8d96193
Compare
8d96193
to
9277248
Compare
@tatomyr I'm curious why this is a @jedelson-pagerduty overall great contribution 🎉 . Thank you so much for your contribution. |
@adamaltman perhaps we decided this is more of an improvement rather than a fix. I don’t recall the exact reason though. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jedelson-pagerduty! Sorry that the review took so long 😅
I believe your approach is correct. However, there are some corner cases when we are removing actually used components (which is worse than leaving unused ones). I found one. Do you see anything else? Maybe @lornajane has more to suggest?
location, | ||
'schemas', | ||
key.toString(), | ||
schema.allOf && schema.allOf.some((v) => v.$ref && resolve(v).node?.discriminator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid this won't work correctly with a transient allOf-discriminator pair. Consider this example:
openapi: '3.0.0'
components:
schemas:
BaseVehicle:
type: object
description: Vehicle
discriminator:
propertyName: powerSource
properties:
powerSource:
description: How is the vehicle powered.
type: string
example: pedaling
Vehicle:
allOf:
- $ref: "#/components/schemas/BaseVehicle"
- type: object
properties:
vehicleType:
description: The type of vehicle.
type: string
example: bicycle
PedaledVehicle:
allOf:
- $ref: "#/components/schemas/Vehicle"
- type: object
description: Pedaled Vehicle
properties:
range:
description: The 95th percentile range of a trip in kilometers.
type: integer
example: 100
The PedaledVehicle
will be considered unused despite Vehicle
resolves to BaseVehicle
which actually contains the discriminator
. I'm not sure this is a common use case but technically it looks like a valid one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tatomyr Appreciate the feedback! And yes, I suppose that is valid in the general case.
I'm wondering if this is fruitless -- I am trying to apply a very strict definition of "used" that works for my use case (in this example, I would actually expect all those schemas to be removed) but wouldn't be generally applicable.
I can look at taking another run at this (although it might be a few days) but I am starting to think a different approach is necessary.
location, | ||
'schemas', | ||
key.toString(), | ||
schema.allOf && schema.allOf.some((v) => v.$ref && resolve(v).node?.discriminator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
schema.allOf && schema.allOf.some((v) => v.$ref && resolve(v).node?.discriminator) | |
schema.allOf?.some((v) => v.$ref && resolve(v).node?.discriminator) |
'@redocly/cli': minor | ||
--- | ||
|
||
Fixed an issue where remove-unused-components would not remove schemas which had a top-level allOf and improved detection of unused schemas in no-unused-components rule. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rephrase the description so it emphasises the improvement rather than the fix?
@jedelson-pagerduty do you have any updates on this? FYI we've recently moved the |
fixes #1209
What/Why/How?
Details #1209 but essentially a schema like:
will never be removed
Testing
Added a unit test for this case. Have also tested this change on a real-world document with this situation.
Check yourself
Security