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(4058): operation parameter should override pathItem parameters #4084

Merged
merged 7 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,14 @@ export async function generateApi(
).name
);

const parameters = supportDeepObjects([
...apiGen.resolveArray(pathItem.parameters),
...apiGen.resolveArray(operation.parameters),
]).filter(argumentMatches(overrides?.parameterFilter));
const operationParameters = apiGen.resolveArray(operation.parameters);
const pathItemParameters = apiGen
.resolveArray(pathItem.parameters)
.filter((pp) => !operationParameters.some((op) => op.name === pp.name && op.in === pp.in));

const parameters = supportDeepObjects([...pathItemParameters, ...operationParameters]).filter(
argumentMatches(overrides?.parameterFilter)
);

const allNames = parameters.map((p) => p.name);
const queryArg: QueryArgDefinitions = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2524,6 +2524,24 @@ export type ExampleSchemaWrite = {
"
`;

exports[`query parameters > parameters overridden in swagger should also be overridden in the code 1`] = `
"import { api } from "./fixtures/emptyApi";
const injectedRtkApi = api.injectEndpoints({
endpoints: (build) => ({
getUsersById: build.query<GetUsersByIdApiResponse, GetUsersByIdApiArg>({
query: (queryArg) => ({ url: \`/users/\${queryArg.id}\` }),
}),
}),
overrideExisting: false,
});
export { injectedRtkApi as enhancedApi };
export type GetUsersByIdApiResponse = unknown;
export type GetUsersByIdApiArg = {
id: number;
};
"
`;

exports[`should use brackets in a querystring urls arg, when the arg contains full stops 1`] = `
"import { api } from "./fixtures/emptyApi";
const injectedRtkApi = api.injectEndpoints({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
openapi: 3.0.2
info:
title: parameterOverride
version: 1.0.0
paths:
/users/{id}:
parameters:
- in: path
name: id
schema:
type: string
required: true
get:
summary: Gets one or more users by ID.
parameters:
- in: path
name: id
required: true
schema:
type: integer
responses:
'200':
description: OK
10 changes: 10 additions & 0 deletions packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,13 @@ describe('openapi spec', () => {
expect(api).toMatchSnapshot();
});
});

describe('query parameters', () => {
it('parameters overridden in swagger should also be overridden in the code', async () => {
const api = await generateEndpoints({
schemaFile: './test/fixtures/parameterOverride.yaml',
apiFile: './fixtures/emptyApi.ts',
});
expect(api).toMatchSnapshot();
});
});