Skip to content

Commit 908b097

Browse files
authored
feat: add mergeReadWriteOnly option (#3860)
1 parent 07f6e75 commit 908b097

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

packages/rtk-query-codegen-openapi/src/generate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ export async function generateApi(
9494
unionUndefined,
9595
flattenArg = false,
9696
useEnumType = false,
97+
mergeReadWriteOnly = false,
9798
}: GenerationOptions
9899
) {
99100
const v3Doc = await getV3Doc(spec);
100101

101102
const apiGen = new ApiGenerator(v3Doc, {
102103
unionUndefined,
103104
useEnumType,
105+
mergeReadWriteOnly,
104106
});
105107

106108
// temporary workaround for https://github.com/oazapfts/oazapfts/issues/491

packages/rtk-query-codegen-openapi/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ export interface CommonOptions {
6767
* `true` will "flatten" the arg so that you can do things like `useGetEntityById(1)` instead of `useGetEntityById({ entityId: 1 })`
6868
*/
6969
flattenArg?: boolean;
70+
/**
71+
* default to false
72+
* `true` will not generate separate types for read-only and write-only properties.
73+
*/
74+
mergeReadWriteOnly?: boolean;
7075
}
7176

7277
export type TextMatcher = string | RegExp | (string | RegExp)[];

packages/rtk-query-codegen-openapi/test/__snapshots__/generateEndpoints.test.ts.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,38 @@ export const { useAddPetMutation, useGetPetByIdQuery } = injectedRtkApi;
491491

492492
`;
493493

494+
exports[`openapi spec readOnly / writeOnly are merged 1`] = `
495+
import { api } from './fixtures/emptyApi';
496+
const injectedRtkApi = api.injectEndpoints({
497+
endpoints: (build) => ({
498+
getExample: build.query<GetExampleApiResponse, GetExampleApiArg>({
499+
query: () => ({ url: \`/example\` }),
500+
}),
501+
setExample: build.mutation<SetExampleApiResponse, SetExampleApiArg>({
502+
query: (queryArg) => ({
503+
url: \`/example\`,
504+
method: 'POST',
505+
body: queryArg.exampleSchema,
506+
}),
507+
}),
508+
}),
509+
overrideExisting: false,
510+
});
511+
export { injectedRtkApi as enhancedApi };
512+
export type GetExampleApiResponse = /** status 200 OK */ ExampleSchema;
513+
export type GetExampleApiArg = void;
514+
export type SetExampleApiResponse = /** status 200 OK */ ExampleSchema;
515+
export type SetExampleApiArg = {
516+
exampleSchema: ExampleSchema;
517+
};
518+
export type ExampleSchema = {
519+
always_present: string;
520+
read_only_prop: string;
521+
write_only_prop: string;
522+
};
523+
524+
`;
525+
494526
exports[`openapi spec readOnly / writeOnly are respected 1`] = `
495527
import { api } from './fixtures/emptyApi';
496528
const injectedRtkApi = api.injectEndpoints({

packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,15 @@ describe('openapi spec', () => {
372372
expect(api).toMatchSnapshot();
373373
});
374374
});
375+
376+
describe('openapi spec', () => {
377+
it('readOnly / writeOnly are merged', async () => {
378+
const api = await generateEndpoints({
379+
unionUndefined: true,
380+
schemaFile: './fixtures/readOnlyWriteOnly.yaml',
381+
apiFile: './fixtures/emptyApi.ts',
382+
mergeReadWriteOnly: true
383+
});
384+
expect(api).toMatchSnapshot();
385+
});
386+
});

0 commit comments

Comments
 (0)