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

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
132 changes: 65 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,77 @@ 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 = kebab(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 +99,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
10 changes: 10 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,14 @@ export default defineConfig({
target: '../specifications/petstore.yaml',
},
},
multipleTags: {
output: {
target: '../generated/default/multiple-tags/endpoints.ts',
schemas: '../generated/default/multiple-tags/model',
mode: 'tags',
},
input: {
target: '../specifications/multiple-tags.yaml',
},
},
});
29 changes: 29 additions & 0 deletions tests/specifications/multiple-tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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:
- cats
- dogs
responses:
'200':
description: A paged array of pets
content:
application/hal+json:
schema:
$ref: '#/components/schemas/Pets'
components:
schemas:
Pets:
type: array
items:
type: object
Loading