This repository was archived by the owner on Jul 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add apply-root-security decorator #54
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ffd3ed2
feat: add spread-security-to-operations decorator
Daryna-del 5dd896c
chore: address to comments
Daryna-del 8723d55
fix: apply securitySchema
Daryna-del cfc6070
fix: fix override root security and formatting
Daryna-del 5529a31
chore: address to comments and add oas2
Daryna-del 307e3fe
fix: add empty line at the end
Daryna-del 261cde1
chore: remove oas2 and address to comments
Daryna-del e65e84c
chore: improve logic
Daryna-del 79aca50
chore: adjust summary
Daryna-del File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
custom-plugin-decorators/spread-root-security/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # Spread root-level security to operations after join | ||
|
|
||
| Authors: | ||
|
|
||
| - [`@Daryna-del`](https://github.com/Daryna-del), Daryna Pastushenko (Redocly) | ||
|
|
||
| ## What this does and why | ||
|
|
||
| When you use `redocly join` to combine multiple API descriptions into one, root-level `security` is not automatically inherited across the joined specs. This is by design — silently applying security requirements from one file to operations defined in another would change their behavior without an explicit declaration. | ||
|
|
||
| A common scenario is when one spec (for example, `foo.yaml`) defines shared infrastructure — security schemes and root-level `security` — but has no paths of its own, while another spec (`bar.yaml`) defines all the paths but has no `security` at all. After joining, the operations from `bar.yaml` end up with no security applied. | ||
|
|
||
| This decorator (`apply-root-security`) solves that: it reads the root-level `security` from a specified source file (for example `foo.yaml`) and sets it as root-level `security` on the document you are bundling when that document does not already define its own. It runs as a `bundle` step, giving you full control over which file supplies the requirement. | ||
|
|
||
| ## Code | ||
|
|
||
| The following code snippet shows the decorator, in a file named `plugin.js`: | ||
|
|
||
| ```javascript | ||
| export default function plugin() { | ||
| return { | ||
| id: "security-plugin", | ||
| decorators: { | ||
| oas3: { | ||
| 'apply-root-security': ({ pathSecurityFile } = {}) => { | ||
| return { | ||
| Root: { | ||
| leave(root, { config }) { | ||
| const doc = resolvePath(pathSecurityFile, config); | ||
|
|
||
| if (doc?.security !== undefined || root.security === undefined){ | ||
| root.security = doc?.security; | ||
| } | ||
|
|
||
| if (doc.components?.securitySchemes !== undefined) { | ||
| if (!root.components) { | ||
| root.components = {}; | ||
| } | ||
| root.components.securitySchemes = { | ||
| ...root.components.securitySchemes, | ||
| ...doc.components.securitySchemes, | ||
| }; | ||
| } | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Put this file alongside your `redocly.yaml` file, and add the following configuration to `redocly.yaml`: | ||
|
|
||
| ```yaml | ||
| plugins: | ||
| - './plugin.js' | ||
|
|
||
| decorators: | ||
| security-plugin/spread-root-security: | ||
| pathSecurityFile: ./foo.yaml | ||
| ``` | ||
|
|
||
| The `pathSecurityFile` parameter is the path to the spec file that contains the root-level `security` you want to spread. | ||
|
|
||
| ## Examples | ||
|
|
||
| Given two specs: | ||
|
|
||
| **foo.yaml** — defines root-level security, no paths: | ||
| ```yaml | ||
| openapi: 3.1.0 | ||
| info: | ||
| title: Foo | ||
| version: 1.0.0 | ||
| security: | ||
| - oauth2: [] | ||
| components: | ||
| securitySchemes: | ||
| oauth2: | ||
| type: oauth2 | ||
| flows: | ||
| authorizationCode: | ||
| authorizationUrl: https://example.com/oauth/authorize | ||
| tokenUrl: https://example.com/oauth/token | ||
| scopes: {} | ||
| paths: {} | ||
| ``` | ||
|
|
||
| **bar.yaml** — defines paths, no security: | ||
| ```yaml | ||
| openapi: 3.1.0 | ||
| info: | ||
| title: Bar | ||
| version: 1.0.0 | ||
| paths: | ||
| /pets: | ||
| get: | ||
| summary: Get pets example | ||
| operationId: getPetsExample | ||
| responses: | ||
| '200': | ||
| description: OK | ||
| '400': | ||
| description: Bad request | ||
| ``` | ||
|
|
||
| Run: | ||
|
|
||
| ```bash | ||
| redocly bundle bar.yaml -o result.yaml | ||
| ``` | ||
|
|
||
| The resulting `result.yaml` will have `security: [oauth2: []]` and `securitySchema` applied. | ||
|
|
||
| ## References | ||
|
|
||
| - [Redocly join command](https://redocly.com/docs/cli/commands/join) | ||
| - [Custom decorators in plugins](https://redocly.com/docs/cli/custom-plugins/custom-decorators) | ||
| - [Security requirement object (OpenAPI)](https://spec.openapis.org/oas/v3.1.0#security-requirement-object) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| export default function plugin() { | ||
| return { | ||
| id: "security-plugin", | ||
| decorators: { | ||
| oas3: { | ||
| 'apply-root-security': ({ pathSecurityFile } = {}) => { | ||
| return { | ||
| Root: { | ||
| leave(root, { config }) { | ||
| const doc = resolvePath(pathSecurityFile, config); | ||
|
|
||
| if (doc?.security !== undefined || root.security === undefined){ | ||
| root.security = doc?.security; | ||
| } | ||
|
|
||
| if (doc.components?.securitySchemes !== undefined) { | ||
| if (!root.components) { | ||
| root.components = {}; | ||
| } | ||
| root.components.securitySchemes = { | ||
| ...root.components.securitySchemes, | ||
| ...doc.components.securitySchemes, | ||
| }; | ||
| } | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| plugins: | ||
| - './plugin.js' | ||
|
|
||
| decorators: | ||
| security-plugin/apply-root-security: | ||
| pathSecurityFile: ./foo.yaml |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What if the openapi already has security defined? Will this decorator override the existing 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.
BTW, let's apply some formatting.
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.
It shouldn't. That was a typo, condition here should be AND not OR, fixed that.
Also, fixed formatting.