Skip to content
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"docs/react-dom": "npm:17.0.2",
"docs/@types/react-dom": "npm:17.0.11",
"docs/@types/react": "npm:17.0.11",
"type-fest": "2.19.0"
"type-fest": "2.19.0",
"oazapfts": "4.8.0"
},
"scripts": {
"build": "yarn build:packages",
Expand Down
4 changes: 2 additions & 2 deletions packages/rtk-query-codegen-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@
"dependencies": {
"@apidevtools/swagger-parser": "^10.0.2",
"commander": "^6.2.0",
"oazapfts": "^4.2.0",
"oazapfts": "^4.8.0",
"prettier": "^2.2.1",
"semver": "^7.3.5",
"swagger2openapi": "^7.0.4",
"typescript": "^4.9.3"
"typescript": "^5.0.0"
},
"husky": {
"hooks": {
Expand Down
7 changes: 4 additions & 3 deletions packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ export async function generateApi(
[
code,
apiGen.resolve(response),
apiGen.getTypeFromResponse(response) || factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
apiGen.getTypeFromResponse(response, 'readOnly') ||
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
] as const
)
.filter(([status, response]) => isDataResponse(status, apiGen.resolve(response), responses || {}))
Expand Down Expand Up @@ -283,7 +284,7 @@ export async function generateApi(
origin: 'param',
name,
originalName: param.name,
type: apiGen.getTypeFromSchema(isReference(param) ? param : param.schema),
type: apiGen.getTypeFromSchema(isReference(param) ? param : param.schema, undefined, 'writeOnly'),
required: param.required,
param,
};
Expand All @@ -300,7 +301,7 @@ export async function generateApi(
origin: 'body',
name,
originalName: schemaName,
type: apiGen.getTypeFromSchema(schema),
type: apiGen.getTypeFromSchema(schema, undefined, 'writeOnly'),
required: true,
body,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,44 @@ export const { useAddPetMutation, useGetPetByIdQuery } = injectedRtkApi;

`;

exports[`openapi spec readOnly / writeOnly are respected 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 */ ExampleSchemaRead;
export type GetExampleApiArg = void;
export type SetExampleApiResponse = /** status 200 OK */ ExampleSchemaRead;
export type SetExampleApiArg = {
exampleSchema: ExampleSchemaWrite;
};
export type ExampleSchema = {
always_present: string;
};
export type ExampleSchemaRead = {
always_present: string;
read_only_prop: string;
};
export type ExampleSchemaWrite = {
always_present: string;
write_only_prop: string;
};

`;

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,51 @@
openapi: 3.0.2
info:
title: readOnlyWriteOnlyAPI
version: 1.0.0
paths:
/example:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleSchema'
description: OK
operationId: getExample
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleSchema'
required: true
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleSchema'
description: OK
operationId: setExample
components:
schemas:
ExampleSchema:
description: ''
required:
- always_present
- read_only_prop
- write_only_prop
type: object
properties:
always_present:
description: ''
type: string
read_only_prop:
description: ''
type: string
readOnly: true
write_only_prop:
writeOnly: true
description: ''
type: string
18 changes: 14 additions & 4 deletions packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { resolve } from 'path';
import { generateEndpoints } from '../src';
import fs from 'fs';
import path from 'path';
import del from 'del';
import fs from 'fs';
import path, { resolve } from 'path';
import { generateEndpoints } from '../src';

const tmpDir = path.resolve(__dirname, 'tmp');

Expand Down Expand Up @@ -351,3 +350,14 @@ describe('tests from issues', () => {
expect(result).toMatchSnapshot();
});
});

describe('openapi spec', () => {
it('readOnly / writeOnly are respected', async () => {
const api = await generateEndpoints({
unionUndefined: true,
schemaFile: './fixtures/readOnlyWriteOnly.yaml',
apiFile: './fixtures/emptyApi.ts',
});
expect(api).toMatchSnapshot();
});
});
34 changes: 17 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6766,15 +6766,15 @@ __metadata:
husky: ^4.3.6
jest: ^27
msw: ^0.40.2
oazapfts: ^4.2.0
oazapfts: ^4.8.0
openapi-types: ^9.1.0
prettier: ^2.2.1
pretty-quick: ^3.1.0
semver: ^7.3.5
swagger2openapi: ^7.0.4
ts-jest: ^27
ts-node: ^10.4.0
typescript: ^4.9.3
typescript: ^5.0.0
yalc: ^1.0.0-pre.47
bin:
rtk-query-codegen-openapi: lib/bin/cli.js
Expand Down Expand Up @@ -19764,7 +19764,7 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"minimist@npm:^1.2.7":
"minimist@npm:^1.2.8":
version: 1.2.8
resolution: "minimist@npm:1.2.8"
checksum: 75a6d645fb122dad29c06a7597bddea977258957ed88d7a6df59b5cd3fe4a527e253e9bbf2e783e4b73657f9098b96a5fe96ab8a113655d4109108577ecf85b0
Expand Down Expand Up @@ -20493,18 +20493,18 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"oazapfts@npm:^4.2.0":
version: 4.5.2
resolution: "oazapfts@npm:4.5.2"
"oazapfts@npm:4.8.0":
version: 4.8.0
resolution: "oazapfts@npm:4.8.0"
dependencies:
"@apidevtools/swagger-parser": ^10.1.0
lodash: ^4.17.21
minimist: ^1.2.7
minimist: ^1.2.8
swagger2openapi: ^7.0.8
typescript: ^4.9.3
typescript: ^5.2.2
bin:
oazapfts: lib/codegen/cli.js
checksum: 3abda3fc9b1cf98512254347b6e3c9eea31f0fd7dd663078e7791cd04ec94aff261fa41feceb552f70fae9863f81f537cac0079f697589faa753ba6520b883c4
checksum: 0f3850767a1c18c02a2c002981f3d89304643ecd425d4723f0d73efebddaa3a13d9e7436a070cdd211b24a5fecd270e03686de91d316b3361de64413d2c2a7ee
languageName: node
linkType: hard

Expand Down Expand Up @@ -27146,13 +27146,13 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"typescript@npm:^4.9.3":
version: 4.9.5
resolution: "typescript@npm:4.9.5"
"typescript@npm:^5.0.0, typescript@npm:^5.2.2":
version: 5.2.2
resolution: "typescript@npm:5.2.2"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: ee000bc26848147ad423b581bd250075662a354d84f0e06eb76d3b892328d8d4440b7487b5a83e851b12b255f55d71835b008a66cbf8f255a11e4400159237db
checksum: 7912821dac4d962d315c36800fe387cdc0a6298dba7ec171b350b4a6e988b51d7b8f051317786db1094bd7431d526b648aba7da8236607febb26cf5b871d2d3c
languageName: node
linkType: hard

Expand Down Expand Up @@ -27206,13 +27206,13 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"typescript@patch:typescript@^4.9.3#~builtin<compat/typescript>":
version: 4.9.5
resolution: "typescript@patch:typescript@npm%3A4.9.5#~builtin<compat/typescript>::version=4.9.5&hash=701156"
"typescript@patch:typescript@^5.0.0#~builtin<compat/typescript>, typescript@patch:typescript@^5.2.2#~builtin<compat/typescript>":
version: 5.2.2
resolution: "typescript@patch:typescript@npm%3A5.2.2#~builtin<compat/typescript>::version=5.2.2&hash=701156"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 2eee5c37cad4390385db5db5a8e81470e42e8f1401b0358d7390095d6f681b410f2c4a0c496c6ff9ebd775423c7785cdace7bcdad76c7bee283df3d9718c0f20
checksum: 07106822b4305de3f22835cbba949a2b35451cad50888759b6818421290ff95d522b38ef7919e70fb381c5fe9c1c643d7dea22c8b31652a717ddbd57b7f4d554
languageName: node
linkType: hard

Expand Down