ng-openapi-gen icon indicating copy to clipboard operation
ng-openapi-gen copied to clipboard

Proposal: Exclude API definitions with a [1]more agile way in [2]earlier phase

Open pillsilly opened this issue 1 year ago • 3 comments

Hi My working project is across multiple teams and the work-flow above REST defs can be quite heavy.

Each domain might use different technology, different gen-tools for tolerancing same input yaml can be different.

e.g some of the definitions(though the swagger standard are well followed) works for team A(who use technology X) , but it might not work for team B(who use technology Y)

team B might get affected(as all defs are in the same files) by this even though team B doesn't care about definitions serves for Team A.

ng-openapi-gen does provide some feature that to exclude certain API that client doesn't care,

but it requires

  1. the tag has to be existed in the yaml, instead of having the control out side of the definition files.
  2. even tags are all defined, ng-openapi-gen still tries to read/parse the whole yaml,json when processing it, and if some part of the content are not understandable by ng-openapi-gen the execution goes failed.

so my rough ideas are about 1.give ng-openapi-gen capability(e.g in the config file) that to exclude contents without having have to actually write tags in REST yml, every client can then freely select things to generate/not-generate with more agile way. 2.perform the exclusion logic's earlier during the process so that the no.2 problem can be avoided.

  • I actually implemented(almost) mentioned proposal locally
  • I'm here to hear from community about the idea so that I can decide whether to create a PR.

@luisfpg

pillsilly avatar Jul 21 '23 10:07 pillsilly

draft implementation

diff --git a/node_modules/ng-openapi-gen/lib/ng-openapi-gen.js b/node_modules/ng-openapi-gen/lib/ng-openapi-gen.js
index aca9882..38c6cfe 100644
--- a/node_modules/ng-openapi-gen/lib/ng-openapi-gen.js
+++ b/node_modules/ng-openapi-gen/lib/ng-openapi-gen.js
@@ -286,6 +286,24 @@ function runNgOpenApiGen() {
                     }
                 }
             });
+
+            function filterPaths(paths, excludeTags, excludePaths) {
+                const filteredPaths = {};
+                for (const key in paths) {
+                    const tags = paths[key].get?.tags || [];
+
+                    if (excludePaths.includes(key) || tags.some(tag => excludeTags.includes(tag))) {
+                        console.log(`Path ${key} is excluded`);
+                        continue;
+                    }
+
+                    filteredPaths[key] = paths[key];
+                }
+                return filteredPaths;
+            }
+            const {excludetags = [], excludePaths = []} = options;
+            const filteredPaths = filterPaths(openApi.paths, excludetags, excludePaths);
+            openApi.paths = filteredPaths;
             const gen = new NgOpenApiGen(openApi, options);
             gen.generate();
         }


pillsilly avatar Jul 22 '23 09:07 pillsilly

Can you send a PR for this with a test?

luisfpg avatar Oct 09 '23 11:10 luisfpg

I will try to prepare a pr

pillsilly avatar Nov 06 '23 15:11 pillsilly