44 */
55package de .tum .cit .aet .openapi ;
66
7+ import io .swagger .v3 .oas .models .OpenAPI ;
8+ import io .swagger .v3 .oas .models .Operation ;
79import org .openapitools .codegen .*;
810import org .openapitools .codegen .languages .TypeScriptAngularClientCodegen ;
911import org .openapitools .codegen .model .ModelMap ;
@@ -107,8 +109,6 @@ public void processOpts() {
107109
108110 // Replace base generator supporting files with our template set only.
109111 supportingFiles .clear ();
110- supportingFiles .add (new SupportingFile ("index.mustache" , "" , "index.ts" ));
111- supportingFiles .add (new SupportingFile ("models-index.mustache" , "models" , "index.ts" ));
112112
113113 // Process custom options
114114 if (additionalProperties .containsKey (USE_HTTP_RESOURCE )) {
@@ -143,6 +143,43 @@ public void processOpts() {
143143 useHttpResource , useInjectFunction , separateResources , readonlyModels );
144144 }
145145
146+ @ Override
147+ public void processOpenAPI (OpenAPI openAPI ) {
148+ super .processOpenAPI (openAPI );
149+
150+ if (openapiGeneratorIgnoreList == null ) {
151+ openapiGeneratorIgnoreList = new HashSet <>();
152+ }
153+
154+ Map <String , TagUsage > usageByTag = new HashMap <>();
155+ if (openAPI != null && openAPI .getPaths () != null ) {
156+ openAPI .getPaths ().forEach ((path , pathItem ) -> {
157+ if (pathItem == null ) {
158+ return ;
159+ }
160+ addOperationUsage (pathItem .getGet (), true , usageByTag );
161+ addOperationUsage (pathItem .getPost (), false , usageByTag );
162+ addOperationUsage (pathItem .getPut (), false , usageByTag );
163+ addOperationUsage (pathItem .getDelete (), false , usageByTag );
164+ addOperationUsage (pathItem .getPatch (), false , usageByTag );
165+ addOperationUsage (pathItem .getHead (), false , usageByTag );
166+ addOperationUsage (pathItem .getOptions (), false , usageByTag );
167+ addOperationUsage (pathItem .getTrace (), false , usageByTag );
168+ });
169+ }
170+
171+ for (Map .Entry <String , TagUsage > entry : usageByTag .entrySet ()) {
172+ String apiFilename = toApiFilename (entry .getKey ());
173+ TagUsage usage = entry .getValue ();
174+ if (!usage .hasMutation ) {
175+ openapiGeneratorIgnoreList .add ("api/" + apiFilename + "-api.ts" );
176+ }
177+ if (useHttpResource && separateResources && !usage .hasGet ) {
178+ openapiGeneratorIgnoreList .add ("api/" + apiFilename + "-resources.ts" );
179+ }
180+ }
181+ }
182+
146183 @ Override
147184 public String toModelFilename (String name ) {
148185 // Use kebab-case for filenames without .model suffix (new Angular style guide)
@@ -160,6 +197,42 @@ public String toApiName(String name) {
160197 return StringUtils .camelize (name ) + "Api" ;
161198 }
162199
200+ @ Override
201+ public String toOperationId (String operationId ) {
202+ String name = super .toOperationId (operationId );
203+ String normalized = name .replaceFirst ("^_+" , "" );
204+ normalized = normalized .replaceFirst ("\\ d+$" , "" );
205+ if (normalized .isBlank ()) {
206+ normalized = "operation" ;
207+ }
208+ return normalized ;
209+ }
210+
211+ private void addOperationUsage (Operation operation , boolean isGet , Map <String , TagUsage > usageByTag ) {
212+ if (operation == null ) {
213+ return ;
214+ }
215+
216+ List <String > tags = operation .getTags ();
217+ if (tags == null || tags .isEmpty ()) {
218+ tags = Collections .singletonList ("default" );
219+ }
220+ for (String tag : tags ) {
221+ String sanitizedTag = sanitizeTag (tag );
222+ TagUsage usage = usageByTag .computeIfAbsent (sanitizedTag , key -> new TagUsage ());
223+ if (isGet ) {
224+ usage .hasGet = true ;
225+ } else {
226+ usage .hasMutation = true ;
227+ }
228+ }
229+ }
230+
231+ private static final class TagUsage {
232+ private boolean hasGet ;
233+ private boolean hasMutation ;
234+ }
235+
163236 @ Override
164237 public Map <String , ModelsMap > postProcessAllModels (Map <String , ModelsMap > objs ) {
165238 Map <String , ModelsMap > result = super .postProcessAllModels (objs );
0 commit comments