Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.

feat: add apply-root-security decorator - #54

Merged
Daryna-del merged 9 commits into
mainfrom
feat/spread-security-to-operation-decorator
Jun 12, 2026
Merged

feat: add apply-root-security decorator#54
Daryna-del merged 9 commits into
mainfrom
feat/spread-security-to-operation-decorator

Conversation

@Daryna-del

@Daryna-del Daryna-del commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Adds a custom decorator that apply root-level security from one spec defined in another after using redocly bundle.

Reference: #1409

@Daryna-del Daryna-del self-assigned this Jun 3, 2026
@@ -0,0 +1,12 @@
import spreadSecurityToOperations from "./decorator";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
import spreadSecurityToOperations from "./decorator";
import spreadSecurityToOperations from "./decorator.js";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After all, it's better to define the decorator in place. No need to import it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed


```bash
# Step 1: join the specs
redocly join foo.yaml bar.yaml -o joined.yaml

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's no point in joining them as it won't change anything. Unless there are other multiple docs with paths and operations that get joined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated

@@ -0,0 +1,15 @@
export default function spreadSecurityToOperations({ pathSecurityFile } = {}) {
return {
Operation: {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is too complicated. I'd simply apply the referenced file's root security to the bundled description's root. That should be enough.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated

id: "security-plugin",
decorators: {
oas3: {
"spread-root-security": ({ pathSecurityFile }) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
"spread-root-security": ({ pathSecurityFile }) => {
"apply-root-security": ({ pathSecurityFile }) => {

It's not spread.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Will this decorator apply securitySchemes to the bundled document too? Otherwise we'll get a wrong security defined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, It didn't. Fixed that

@Daryna-del Daryna-del changed the title feat: add spread-security-to-operations decorator feat: add apply-root-security decorator Jun 9, 2026
const doc = resolvePath(pathSecurityFile, config);

if (doc?.security !== undefined || root.security === undefined){
root.security = doc?.security;

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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.

leave(root, { config }) {
const doc = resolvePath(pathSecurityFile, config);

if (doc?.security !== undefined && root.security === undefined){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we can merge the doc and root security somehow? Does it make sense?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, good point


validateOpenapiSpecification(pathSecurityFile, doc, specVersion);

if (specVersion === 'oas2') {

@tatomyr tatomyr Jun 12, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's not overcomplicate it. The purpose of the Cookbook is to demonstrate the possibility, not provide the ultimate solution. Let's focus on OAS3 first.


function mergeSecurityRequirements(root, doc) {
if (!Array.isArray(doc?.security)) return;
root.security = [...(root.security || []), ...doc.security];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If there's already such a security requirement, will it duplicate it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, It will duplicate. Added a check for that

root.security = [...(root.security || []), ...doc.security];
};

function mergeSecuritySchemes(root, doc) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you use names like source and targed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I can. Renamed

@@ -0,0 +1,11 @@
import applyRootSecurity from "./decorator.js";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
import applyRootSecurity from "./decorator.js";
import { applyRootSecurity } from "./decorator.js";

@@ -0,0 +1,54 @@
const applyRootSecurity = ({ pathSecurityFile } = {}) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
const applyRootSecurity = ({ pathSecurityFile } = {}) => {
export const applyRootSecurity = ({ pathSecurityFile } = {}) => {

@Daryna-del
Daryna-del merged commit 991b270 into main Jun 12, 2026
2 checks passed
@Daryna-del
Daryna-del deleted the feat/spread-security-to-operation-decorator branch June 12, 2026 13:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants