Skip to content

Commit

Permalink
fix(tags): only setup used tags
Browse files Browse the repository at this point in the history
  • Loading branch information
thib3113 committed Nov 28, 2023
1 parent 35dbd7a commit cca21b4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ Remember, the journey of mastering any tool involves experimentation, learning f

- allow to add custom mappers
- allow easier securitySchemes setup
- remove unused tags
- $$oa
- allow to define a ref, and use the ref instead of creating a new one
- allow to define a "to ref", and create the ref with this name
Expand Down
21 changes: 19 additions & 2 deletions src/OpenApiGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export class OpenApiGenerator {
}

public generate(openApiVersion: openApiVersionsSupported, aliases: Array<Alias>): OA3_1.Document {
const tagsMap: Map<string, OA3_1.TagObject> = new Map<string, OA3_1.TagObject>();

if ((this.document as { openapi?: string }).openapi) {
this.logger.warn(`setting manually the openapi version is not supported`);
delete (this.document as { openapi?: string }).openapi;
Expand Down Expand Up @@ -107,7 +109,7 @@ export class OpenApiGenerator {

cachePathActions.set(cacheKeyName, pathAction.action?.name);

const openApi = OpenApiMerger.merge(document, openApiService, apiService, route, alias, pathAction.action);
const openApi = OpenApiMerger.merge(tagsMap, openApiService, apiService, route, alias, pathAction.action);

this.components = this.mergeComponents(this.components, this.cleanComponents(openApi.components));

Expand All @@ -117,7 +119,7 @@ export class OpenApiGenerator {
operationId: openApi?.operationId,
externalDocs: openApi?.externalDocs,
security: openApi?.security,
tags: Array.from(new Set(openApi?.tags ?? [])),
tags: this.handleTags(document, tagsMap, openApi?.tags),
parameters,
requestBody,
responses: openApi?.responses
Expand Down Expand Up @@ -150,6 +152,8 @@ export class OpenApiGenerator {
document.paths[openapiPath] = currentPath;
});

document.tags?.sort(getAlphabeticSorter('name'));

document.components = this.mergeComponents(document.components, this.components);

return this.removeExtensions(document);
Expand Down Expand Up @@ -570,4 +574,17 @@ export class OpenApiGenerator {
])
);
}

private handleTags(document: OA3_1.Document, tagsMap: Map<string, OA3_1.TagObject>, tags: Array<string> = []): Array<string> {
const uniqTags = Array.from(new Set(tags));

uniqTags.forEach((tag) => {
const tagObject: OA3_1.TagObject | undefined = tagsMap.get(tag);
if (!document.tags?.some(({ name }) => name === tag) && tagObject) {
document.tags.push(tagObject);
}
});

return uniqTags;
}
}
18 changes: 3 additions & 15 deletions src/OpenApiMerger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ActionOpenApi, actionOpenApiResponse, ApiSettingsSchemaOpenApi, commonO
import { OpenAPIV3_1 as OA3_1 } from 'openapi-types';
import { Route } from './objects/Route.js';
import { Alias } from './objects/Alias.js';
import { DEFAULT_CONTENT_TYPE, getAlphabeticSorter } from './commons.js';
import { DEFAULT_CONTENT_TYPE } from './commons.js';

type actionOpenApiMerged = Omit<ActionOpenApi, 'tags'> & { tags: Array<string> };

Expand Down Expand Up @@ -128,20 +128,14 @@ export class OpenApiMerger {
}

static merge(
document: OA3_1.Document,
tagsMap: Map<string, OA3_1.TagObject>,
openApiService: Service<OpenApiMixinSettings>,
apiService: Service<ApiSettingsSchemaOpenApi>,
route: Route,
alias: Alias,
action: ActionSchema
): Omit<ActionOpenApi, 'tags'> & { tags: Array<string> } {
const tags = document.tags ?? [];
const tagsMap: Map<string, OA3_1.TagObject> = new Map<string, OA3_1.TagObject>();
tags.forEach((tag) => {
tagsMap.set(tag.name, tag);
});

const openApi = [
return [
alias?.openapi,
alias?.service?.name ? { tags: [alias.service.name] } : undefined,
alias?.service?.settings?.openapi,
Expand All @@ -167,11 +161,5 @@ export class OpenApiMerger {
},
this.mergeCommons(tagsMap, [openApiService.settings.openapi, apiService.settings.openapi, route.openapi])
) as actionOpenApiMerged;

const outputTags = Array.from(tagsMap.values());
outputTags.sort(getAlphabeticSorter('name'));
document.tags = outputTags;

return openApi;
}
}

0 comments on commit cca21b4

Please sign in to comment.