Looking for a straight-forward (more convenient) way to programmatically delete named components of a path
See original GitHub issueI have an OpenAPI definition where we are using multi-file layout for requestBodies, responses, and schemas; And also using openapi-cli to do some transformations via custom plugin/decorator;
I have a custom vendor extension i’m applying at path level, to determine whether to delete that path or not from the build. Example path object:
x-delete-path: true
post:
operationId: blah
summary: blah
description: blah
requestBody:
$ref: ../components/requestBodies/blah.yaml
responses:
'200':
$ref: ../components/responses/blah.yaml
The problem is that when writing the decorator, in the PathItem
object, if I delete a path item, the related named requestBodies, responses, and schemas are not deleted, so they end up in the resulting build.
I discovered how to use the ctx.resolve() function to obtain the related requestBody from the $ref, but am not seeing a way to delete the named requestBody from that object that is returned.
It appears that openapi-cli is using the referenced filename to determine the name of the named object.
Therefore, I suppose I could try to parse and store that $ref’d filename in the PathItem
object’s enter
or leave
method, in order to delete that property in NamedRequestBodies
, etc…
Any other thoughts or suggestions? Thanks for this awesome product.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (6 by maintainers)
For what it’s worth: we have solved this problem by using reference counting in a preprocessor:
ref
visitor increments counts for the#/components/....
paths in a mapPathItem
visitor with nestedOperation
visitor handles filtering of operations (and theleave
hook onPathItem
deletes paths if all operations are dropped from the output). As long as you clear all properties in theOperation
object you can prevent traversal to any references, and so prevent their count going up.DefinitionRoot.leave
, clear out components with no references. Forschemas
, we repeatedly process each item and decrement reference counts with a recursive utility function whenever a schema item is dropped, to catch transitive references.Here is a rough outline of how we implemented this strategy; perhaps this can inform more specific support in openapi-core for this type of document processing. Any project specific parts are ‘templated’ by putting descriptive text in square brackets (e.g. [this is a placeholder for your own logic]):
Depending on your OpenAPI document structure, you may have to expand the recursive component processing to headers responses, request bodies, etc too. I can imagine that in a more complex document, that the multi-pass approach for
schemas
would have to be extended to loop over the flattenedComponent
collections. It just was not an issue for our project however.I found a much cleaner (for us) approach.
Instead of programmatically deleting paths, and then trying to clean up all the related named components, we are adding paths into the build using a plugin.
The benefit to this strategy, is that redoc doesn’t wire all the unnecessary named components in, because the linked path items are never included.
Prior to this change (for example), we were flagging a path for deletion with a flag like
x-delete-path: true
Using the new strategy, the
paths:
object is completely empty by default. Then, based on criteria, we are adding paths in:That way, the only named components that redoc pulls into the resulting build will be what is linked to in that path.