Skip to content
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

Inverse filtered documents do not include main level security definitions #181

Open
duuri-mollinen opened this issue Sep 21, 2023 · 2 comments · May be fixed by #191
Open

Inverse filtered documents do not include main level security definitions #181

duuri-mollinen opened this issue Sep 21, 2023 · 2 comments · May be fixed by #191

Comments

@duuri-mollinen
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Inverse filtered paths with following flags produce an non valid api document that does not include the securitySchemes under to components when there are active security definitions under the paths->path->method structure

-i --valid --strip --servers

Here is the diff that solved my problem:

diff --git a/node_modules/openapi-filter/index.js b/node_modules/openapi-filter/index.js
index dd1f690..d342663 100644
--- a/node_modules/openapi-filter/index.js
+++ b/node_modules/openapi-filter/index.js
@@ -139,10 +139,40 @@ function filter(obj,options) {
         if (options.servers && !filtered.servers && Array.isArray(src.servers)) {
             filtered.servers = src.servers;
         }
+        const activeSecuritySchemes= 
+        (src.paths?Object.keys(src.paths):[])
+            .flatMap(pathUrl =>src.paths[pathUrl])
+            .flatMap(pathElement => Object.keys(pathElement)
+            .flatMap(method =>pathElement[method]))
+            .filter(path => Object.keys(path).filter(value => options.flags.includes(value)))
+            .flatMap(path =>{
+                if (!filtered.security && Array.isArray(path.security)) {
+                    return path.security.flatMap(securityItem => Object.keys(securityItem));
+                }else{
+                    return [];
+                }
+            })
+            .filter(filterUnique)
+
+        // OAS2
+        if (src.securityDefinitions) {
+            filtered.securityDefinitions = Object.fromEntries(Object.entries(src.securityDefinitions).filter(([key]) => activeSecuritySchemes.includes(key)));
+        }
+        // OAS3
+        if (src.components && src.components.securitySchemes) {
+            if(!filtered.components){
+                filtered.components ={};
+            }
+            filtered.components.securitySchemes = Object.fromEntries(Object.entries(src.components.securitySchemes).filter(([key]) => activeSecuritySchemes.includes(key)));
+        }
     }
     return (options.inverse ? filtered : src);
 }
 
+function filterUnique(value, index, array) {
+    return array.indexOf(value) === index;
+  }
+
 module.exports = {
     filter : filter
 };

This issue body was partially generated by patch-package.

@MikeRalphson
Copy link
Contributor

Thanks, can patch-package produce PRs?

PJHRobles pushed a commit to PJHRobles/openapi-filter that referenced this issue Jul 31, 2024
@PJHRobles
Copy link

Seeing as this hasn't moved in a while and I do need the feature, I've opted to create an MR #191

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants