Skip to content

Commit

Permalink
feat: add mergeReadWriteOnly option (#3860)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliengbt authored Nov 9, 2023
1 parent 07f6e75 commit 908b097
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ export async function generateApi(
unionUndefined,
flattenArg = false,
useEnumType = false,
mergeReadWriteOnly = false,
}: GenerationOptions
) {
const v3Doc = await getV3Doc(spec);

const apiGen = new ApiGenerator(v3Doc, {
unionUndefined,
useEnumType,
mergeReadWriteOnly,
});

// temporary workaround for https://github.com/oazapfts/oazapfts/issues/491
Expand Down
5 changes: 5 additions & 0 deletions packages/rtk-query-codegen-openapi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export interface CommonOptions {
* `true` will "flatten" the arg so that you can do things like `useGetEntityById(1)` instead of `useGetEntityById({ entityId: 1 })`
*/
flattenArg?: boolean;
/**
* default to false
* `true` will not generate separate types for read-only and write-only properties.
*/
mergeReadWriteOnly?: boolean;
}

export type TextMatcher = string | RegExp | (string | RegExp)[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,38 @@ export const { useAddPetMutation, useGetPetByIdQuery } = injectedRtkApi;

`;

exports[`openapi spec readOnly / writeOnly are merged 1`] = `
import { api } from './fixtures/emptyApi';
const injectedRtkApi = api.injectEndpoints({
endpoints: (build) => ({
getExample: build.query<GetExampleApiResponse, GetExampleApiArg>({
query: () => ({ url: \`/example\` }),
}),
setExample: build.mutation<SetExampleApiResponse, SetExampleApiArg>({
query: (queryArg) => ({
url: \`/example\`,
method: 'POST',
body: queryArg.exampleSchema,
}),
}),
}),
overrideExisting: false,
});
export { injectedRtkApi as enhancedApi };
export type GetExampleApiResponse = /** status 200 OK */ ExampleSchema;
export type GetExampleApiArg = void;
export type SetExampleApiResponse = /** status 200 OK */ ExampleSchema;
export type SetExampleApiArg = {
exampleSchema: ExampleSchema;
};
export type ExampleSchema = {
always_present: string;
read_only_prop: string;
write_only_prop: string;
};

`;

exports[`openapi spec readOnly / writeOnly are respected 1`] = `
import { api } from './fixtures/emptyApi';
const injectedRtkApi = api.injectEndpoints({
Expand Down
12 changes: 12 additions & 0 deletions packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,15 @@ describe('openapi spec', () => {
expect(api).toMatchSnapshot();
});
});

describe('openapi spec', () => {
it('readOnly / writeOnly are merged', async () => {
const api = await generateEndpoints({
unionUndefined: true,
schemaFile: './fixtures/readOnlyWriteOnly.yaml',
apiFile: './fixtures/emptyApi.ts',
mergeReadWriteOnly: true
});
expect(api).toMatchSnapshot();
});
});

0 comments on commit 908b097

Please sign in to comment.