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

fix(core): only generate one operation if it has multiple tags #1812

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,36 @@ Type: `Object`.

Exactly the same as the `override.operations` but this time you can do it by <a href="https://swagger.io/docs/specification/grouping-operations-with-tags/" target="_blank">tags</a>

#### multiTagResolver

Type: `Function`.

```ts
// type signature
(tags: string[]) => string;
```

When using mode `tags` or `tags-split` and an operation have multiple tags, only one of them will be used when generating.
This setting allows you to decide which tag to use. Default is the first tag.

Example:

```js
module.exports = {
petstore: {
output: {
override: {
multiTagResolver: (tags) => {
const indexOfFavoriteTag = tags.include('myFavoriteTag');
if (indexOfFavoriteTag >= 0) return tags[indexOfFavoriteTag];
return tags[0];
},
},
},
},
};
```

#### operationName

Type: `Function`.
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type NormalizedOverrideOutput = {
mutator?: NormalizedMutator;
operations: { [key: string]: NormalizedOperationOptions };
tags: { [key: string]: NormalizedOperationOptions };
multiTagResolver?: (tags: string[]) => string;
mock?: OverrideMockOptions;
contentType?: OverrideOutputContentType;
header: false | ((info: InfoObject) => string[] | string);
Expand Down Expand Up @@ -360,6 +361,7 @@ export type OverrideOutput = {
mutator?: Mutator;
operations?: { [key: string]: OperationOptions };
tags?: { [key: string]: OperationOptions };
multiTagResolver?: (tags: string[]) => string;
mock?: OverrideMockOptions;
contentType?: OverrideOutputContentType;
header?: boolean | ((info: InfoObject) => string[] | string);
Expand Down
134 changes: 67 additions & 67 deletions packages/core/src/writers/target-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,79 @@ const addDefaultTagIfEmpty = (operation: GeneratorOperation) => ({
const generateTargetTags = (
currentAcc: { [key: string]: GeneratorTargetFull },
operation: GeneratorOperation,
options: NormalizedOutputOptions,
): {
[key: string]: GeneratorTargetFull;
} => {
return operation.tags.map(kebab).reduce((acc, tag) => {
const currentOperation = acc[tag];

if (!currentOperation) {
acc[tag] = {
imports: operation.imports,
importsMock: operation.importsMock,
mutators: operation.mutator ? [operation.mutator] : [],
clientMutators: operation.clientMutators ?? [],
formData: operation.formData ? [operation.formData] : [],
formUrlEncoded: operation.formUrlEncoded
? [operation.formUrlEncoded]
: [],
paramsSerializer: operation.paramsSerializer
? [operation.paramsSerializer]
: [],
implementation: operation.implementation,
implementationMock: {
function: operation.implementationMock.function,
handler: operation.implementationMock.handler,
handlerName: ' ' + operation.implementationMock.handlerName + '()',
},
};

return acc;
}

acc[tag] = {
implementation:
currentOperation.implementation + operation.implementation,
imports: [...currentOperation.imports, ...operation.imports],
importsMock: [...currentOperation.importsMock, ...operation.importsMock],
implementationMock: {
function:
currentOperation.implementationMock.function +
operation.implementationMock.function,
handler:
currentOperation.implementationMock.handler +
operation.implementationMock.handler,
handlerName:
currentOperation.implementationMock.handlerName +
',\n ' +
operation.implementationMock.handlerName +
'()',
},
mutators: operation.mutator
? [...(currentOperation.mutators ?? []), operation.mutator]
: currentOperation.mutators,
clientMutators: operation.clientMutators
? [
...(currentOperation.clientMutators ?? []),
...operation.clientMutators,
]
: currentOperation.clientMutators,
formData: operation.formData
? [...(currentOperation.formData ?? []), operation.formData]
: currentOperation.formData,
const tag = options.override.multiTagResolver
? options.override.multiTagResolver(operation.tags)
: operation.tags[0];
const currentOperation = currentAcc[tag];

if (!currentOperation) {
currentAcc[tag] = {
imports: operation.imports,
importsMock: operation.importsMock,
mutators: operation.mutator ? [operation.mutator] : [],
clientMutators: operation.clientMutators ?? [],
formData: operation.formData ? [operation.formData] : [],
formUrlEncoded: operation.formUrlEncoded
? [...(currentOperation.formUrlEncoded ?? []), operation.formUrlEncoded]
: currentOperation.formUrlEncoded,
? [operation.formUrlEncoded]
: [],
paramsSerializer: operation.paramsSerializer
? [
...(currentOperation.paramsSerializer ?? []),
operation.paramsSerializer,
]
: currentOperation.paramsSerializer,
? [operation.paramsSerializer]
: [],
implementation: operation.implementation,
implementationMock: {
function: operation.implementationMock.function,
handler: operation.implementationMock.handler,
handlerName: ' ' + operation.implementationMock.handlerName + '()',
},
};

return acc;
}, currentAcc);
return currentAcc;
}

currentAcc[tag] = {
implementation: currentOperation.implementation + operation.implementation,
imports: [...currentOperation.imports, ...operation.imports],
importsMock: [...currentOperation.importsMock, ...operation.importsMock],
implementationMock: {
function:
currentOperation.implementationMock.function +
operation.implementationMock.function,
handler:
currentOperation.implementationMock.handler +
operation.implementationMock.handler,
handlerName:
currentOperation.implementationMock.handlerName +
',\n ' +
operation.implementationMock.handlerName +
'()',
},
mutators: operation.mutator
? [...(currentOperation.mutators ?? []), operation.mutator]
: currentOperation.mutators,
clientMutators: operation.clientMutators
? [
...(currentOperation.clientMutators ?? []),
...operation.clientMutators,
]
: currentOperation.clientMutators,
formData: operation.formData
? [...(currentOperation.formData ?? []), operation.formData]
: currentOperation.formData,
formUrlEncoded: operation.formUrlEncoded
? [...(currentOperation.formUrlEncoded ?? []), operation.formUrlEncoded]
: currentOperation.formUrlEncoded,
paramsSerializer: operation.paramsSerializer
? [
...(currentOperation.paramsSerializer ?? []),
operation.paramsSerializer,
]
: currentOperation.paramsSerializer,
};
return currentAcc;
};

export const generateTargetForTags = (
Expand All @@ -101,7 +101,7 @@ export const generateTargetForTags = (
.map(addDefaultTagIfEmpty)
.reduce(
(acc, operation, index, arr) => {
const targetTags = generateTargetTags(acc, operation);
const targetTags = generateTargetTags(acc, operation, options);

if (index === arr.length - 1) {
return Object.entries(targetTags).reduce<
Expand Down
15 changes: 15 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,19 @@ export default defineConfig({
target: '../specifications/petstore.yaml',
},
},
multipleTags: {
output: {
target: '../generated/default/multiple-tags/endpoints.ts',
schemas: '../generated/default/multiple-tags/model',
mode: 'tags',
override: {
multiTagResolver: (tags) => {
return tags[tags.length - 1];
},
},
},
input: {
target: '../specifications/multiple-tags.yaml',
},
},
});
44 changes: 44 additions & 0 deletions tests/specifications/multiple-tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- dogs
- cats
responses:
'200':
description: A paged array of pets
content:
application/hal+json:
schema:
$ref: '#/components/schemas/Pets'
components:
schemas:
Pet:
type: object
oneOf:
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Cat'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also an unnecessary definition 👍

Dog:
type: object
properties:
barksPerMinute:
type: integer
Cat:
type: object
properties:
petsRequested:
type: integer
Pets:
type: array
items:
$ref: '#/components/schemas/Pet'
Loading