cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
403 stars 134 forks source link

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

Closed pillsilly closed 2 months ago

pillsilly commented 1 year ago

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.

@luisfpg

pillsilly commented 1 year ago

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();
         }
luisfpg commented 1 year ago

Can you send a PR for this with a test?

pillsilly commented 1 year ago

I will try to prepare a pr