redocly-cli icon indicating copy to clipboard operation
redocly-cli copied to clipboard

New decorator to remove specific property

Open rmclaughlin-nelnet opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe.

This is a feature request. I would like a filter to remove a property. The current decorators remove entire nodes, but I only want a property of the node removed. For example

properties:
  property1:
    type: boolean
    description: This is a description
    example: false
    x-source: This contains internal data about how we get and use the data
  property2:
    type: string
    format: date
    description: This is a description
    example: '2021-04-22'
    x-source: This contains internal data about how we get and use the data
  property3:
    type: number
    format: float
    description: This is a description
    example: 0
    x-source: This contains internal data about how we get and use the data

We want to only remove x-source, not the entire node.

Describe the solution you'd like

The ability to remove just a property while leaving the node intact.

Describe alternatives you've considered

We have tried the built in decorators filter-out and remove-x-internal but they both remove the entire node.

Additional context

I wrote a custom plugin based on remove-x-internal, but I am sure it could be done better.

/** @type {import('@redocly/openapi-cli').OasDecorator} */
function RemoveInternalSchemaProperties() {
  return {
    SchemaProperties: {
      leave(properties) {
        Object.keys(properties).forEach((propertyName) => {
          if (properties[propertyName]['x-source']) {
            delete properties[propertyName]['x-source'];
          }
        });
      },
    },
  };
}

module.exports = RemoveInternalSchemaProperties;

rmclaughlin-nelnet avatar Sep 13 '22 15:09 rmclaughlin-nelnet

Hi @rmclaughlin-nelnet,

Thanks for the request! BTW, there's nothing wrong with using plugins and custom decorators unless you see some issues with this.

tatomyr avatar Sep 15 '22 09:09 tatomyr

Maybe the decorator could remove a list of properties. This would be common as sometimes people need to mark up their OpenAPI with proxy-related specification extensions: see an example x-amazon-apigateway-....

adamaltman avatar Oct 10 '22 23:10 adamaltman

Maybe the decorator could remove a list of properties. This would be common as sometimes people need to mark up their OpenAPI with proxy-related specification extensions: see an example x-amazon-apigateway-....

Hi, I just made the open-source custom decorator remove-extensions that does precisely that. It takes a string regex or an arraw of regex and delete the corresponding openapi extensions (must start with x-):

https://github.com/CIDgravity/redoc-plugins

I have a few questions:

  • how can I write my future plugins in Typescript rather than Javascript?
  • in your codebase, you unit test your own decorators. Is it possible to export the facility (utility functions) you are using so custom plugin developers can reuse them?

Cheers!

nicobao avatar Dec 26 '22 10:12 nicobao

Hi @nicobao,

You can write your plugin in TS, but you have to transpile the code to JS yourself before importing the plugin.

Regarding testing, you can check this example. But instead of lint, you should use the bundle function.

tatomyr avatar Dec 28 '22 10:12 tatomyr

@nicobao Would you consider sharing your decorator in our new CLI cookbook repository? It would be great to have it! (it's been a while, so if I don't hear from you then I'll add it myself some day!)

lornajane avatar Nov 17 '23 18:11 lornajane

@nicobao Would you consider sharing your decorator in our new CLI cookbook repository? It would be great to have it! (it's been a while, so if I don't hear from you then I'll add it myself some day!)

My pleasure! https://github.com/Redocly/redocly-cli-cookbook/pull/19#issue-2034532015

nicobao avatar Dec 10 '23 21:12 nicobao

Really happy to say that an extended version of this decorator is now available in the cookcook. I'm closing this issue but I wanted to say a big thank you to @nicobao for the suggestion and the new decorator!

lornajane avatar Dec 18 '23 13:12 lornajane