Skip to content

Commit 067b6d4

Browse files
cvereterracarlos-vereterra-stemyphryneas
authored
Support OpenAPI readOnly/writeOnly properties (#3430)
* Update oazapfts to support readOnly and writeOnly * Update ts api and fix test snapshot * Repair dependency * Working!! * Add working oazapfts * Unify typescript versions * Remove unnecesary api generation call * Working test * Remove typescript version check * Delete unused file * bump TS & oazapts versions --------- Co-authored-by: Carlos Vereterra <[email protected]> Co-authored-by: Lenz Weber-Tronic <[email protected]>
1 parent 81dd5b4 commit 067b6d4

File tree

7 files changed

+128
-27
lines changed

7 files changed

+128
-27
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
"docs/react-dom": "npm:17.0.2",
6464
"docs/@types/react-dom": "npm:17.0.11",
6565
"docs/@types/react": "npm:17.0.11",
66-
"type-fest": "2.19.0"
66+
"type-fest": "2.19.0",
67+
"oazapfts": "4.8.0"
6768
},
6869
"scripts": {
6970
"build": "yarn build:packages",

packages/rtk-query-codegen-openapi/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858
"dependencies": {
5959
"@apidevtools/swagger-parser": "^10.0.2",
6060
"commander": "^6.2.0",
61-
"oazapfts": "^4.2.0",
61+
"oazapfts": "^4.8.0",
6262
"prettier": "^2.2.1",
6363
"semver": "^7.3.5",
6464
"swagger2openapi": "^7.0.4",
65-
"typescript": "^4.9.3"
65+
"typescript": "^5.0.0"
6666
},
6767
"husky": {
6868
"hooks": {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ export async function generateApi(
222222
[
223223
code,
224224
apiGen.resolve(response),
225-
apiGen.getTypeFromResponse(response) || factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
225+
apiGen.getTypeFromResponse(response, 'readOnly') ||
226+
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
226227
] as const
227228
)
228229
.filter(([status, response]) => isDataResponse(status, apiGen.resolve(response), responses || {}))
@@ -283,7 +284,7 @@ export async function generateApi(
283284
origin: 'param',
284285
name,
285286
originalName: param.name,
286-
type: apiGen.getTypeFromSchema(isReference(param) ? param : param.schema),
287+
type: apiGen.getTypeFromSchema(isReference(param) ? param : param.schema, undefined, 'writeOnly'),
287288
required: param.required,
288289
param,
289290
};
@@ -300,7 +301,7 @@ export async function generateApi(
300301
origin: 'body',
301302
name,
302303
originalName: schemaName,
303-
type: apiGen.getTypeFromSchema(schema),
304+
type: apiGen.getTypeFromSchema(schema, undefined, 'writeOnly'),
304305
required: true,
305306
body,
306307
};

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

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

458458
`;
459459

460+
exports[`openapi spec readOnly / writeOnly are respected 1`] = `
461+
import { api } from './fixtures/emptyApi';
462+
const injectedRtkApi = api.injectEndpoints({
463+
endpoints: (build) => ({
464+
getExample: build.query<GetExampleApiResponse, GetExampleApiArg>({
465+
query: () => ({ url: \`/example\` }),
466+
}),
467+
setExample: build.mutation<SetExampleApiResponse, SetExampleApiArg>({
468+
query: (queryArg) => ({
469+
url: \`/example\`,
470+
method: 'POST',
471+
body: queryArg.exampleSchema,
472+
}),
473+
}),
474+
}),
475+
overrideExisting: false,
476+
});
477+
export { injectedRtkApi as enhancedApi };
478+
export type GetExampleApiResponse = /** status 200 OK */ ExampleSchemaRead;
479+
export type GetExampleApiArg = void;
480+
export type SetExampleApiResponse = /** status 200 OK */ ExampleSchemaRead;
481+
export type SetExampleApiArg = {
482+
exampleSchema: ExampleSchemaWrite;
483+
};
484+
export type ExampleSchema = {
485+
always_present: string;
486+
};
487+
export type ExampleSchemaRead = {
488+
always_present: string;
489+
read_only_prop: string;
490+
};
491+
export type ExampleSchemaWrite = {
492+
always_present: string;
493+
write_only_prop: string;
494+
};
495+
496+
`;
497+
460498
exports[`should use brackets in a querystring urls arg, when the arg contains full stops 1`] = `
461499
import { api } from './fixtures/emptyApi';
462500
const injectedRtkApi = api.injectEndpoints({
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
openapi: 3.0.2
2+
info:
3+
title: readOnlyWriteOnlyAPI
4+
version: 1.0.0
5+
paths:
6+
/example:
7+
get:
8+
responses:
9+
'200':
10+
content:
11+
application/json:
12+
schema:
13+
$ref: '#/components/schemas/ExampleSchema'
14+
description: OK
15+
operationId: getExample
16+
post:
17+
requestBody:
18+
content:
19+
application/json:
20+
schema:
21+
$ref: '#/components/schemas/ExampleSchema'
22+
required: true
23+
responses:
24+
'200':
25+
content:
26+
application/json:
27+
schema:
28+
$ref: '#/components/schemas/ExampleSchema'
29+
description: OK
30+
operationId: setExample
31+
components:
32+
schemas:
33+
ExampleSchema:
34+
description: ''
35+
required:
36+
- always_present
37+
- read_only_prop
38+
- write_only_prop
39+
type: object
40+
properties:
41+
always_present:
42+
description: ''
43+
type: string
44+
read_only_prop:
45+
description: ''
46+
type: string
47+
readOnly: true
48+
write_only_prop:
49+
writeOnly: true
50+
description: ''
51+
type: string

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { resolve } from 'path';
2-
import { generateEndpoints } from '../src';
3-
import fs from 'fs';
4-
import path from 'path';
51
import del from 'del';
2+
import fs from 'fs';
3+
import path, { resolve } from 'path';
4+
import { generateEndpoints } from '../src';
65

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

@@ -351,3 +350,14 @@ describe('tests from issues', () => {
351350
expect(result).toMatchSnapshot();
352351
});
353352
});
353+
354+
describe('openapi spec', () => {
355+
it('readOnly / writeOnly are respected', async () => {
356+
const api = await generateEndpoints({
357+
unionUndefined: true,
358+
schemaFile: './fixtures/readOnlyWriteOnly.yaml',
359+
apiFile: './fixtures/emptyApi.ts',
360+
});
361+
expect(api).toMatchSnapshot();
362+
});
363+
});

yarn.lock

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6766,15 +6766,15 @@ __metadata:
67666766
husky: ^4.3.6
67676767
jest: ^27
67686768
msw: ^0.40.2
6769-
oazapfts: ^4.2.0
6769+
oazapfts: ^4.8.0
67706770
openapi-types: ^9.1.0
67716771
prettier: ^2.2.1
67726772
pretty-quick: ^3.1.0
67736773
semver: ^7.3.5
67746774
swagger2openapi: ^7.0.4
67756775
ts-jest: ^27
67766776
ts-node: ^10.4.0
6777-
typescript: ^4.9.3
6777+
typescript: ^5.0.0
67786778
yalc: ^1.0.0-pre.47
67796779
bin:
67806780
rtk-query-codegen-openapi: lib/bin/cli.js
@@ -19764,7 +19764,7 @@ fsevents@^1.2.7:
1976419764
languageName: node
1976519765
linkType: hard
1976619766

19767-
"minimist@npm:^1.2.7":
19767+
"minimist@npm:^1.2.8":
1976819768
version: 1.2.8
1976919769
resolution: "minimist@npm:1.2.8"
1977019770
checksum: 75a6d645fb122dad29c06a7597bddea977258957ed88d7a6df59b5cd3fe4a527e253e9bbf2e783e4b73657f9098b96a5fe96ab8a113655d4109108577ecf85b0
@@ -20493,18 +20493,18 @@ fsevents@^1.2.7:
2049320493
languageName: node
2049420494
linkType: hard
2049520495

20496-
"oazapfts@npm:^4.2.0":
20497-
version: 4.5.2
20498-
resolution: "oazapfts@npm:4.5.2"
20496+
"oazapfts@npm:4.8.0":
20497+
version: 4.8.0
20498+
resolution: "oazapfts@npm:4.8.0"
2049920499
dependencies:
2050020500
"@apidevtools/swagger-parser": ^10.1.0
2050120501
lodash: ^4.17.21
20502-
minimist: ^1.2.7
20502+
minimist: ^1.2.8
2050320503
swagger2openapi: ^7.0.8
20504-
typescript: ^4.9.3
20504+
typescript: ^5.2.2
2050520505
bin:
2050620506
oazapfts: lib/codegen/cli.js
20507-
checksum: 3abda3fc9b1cf98512254347b6e3c9eea31f0fd7dd663078e7791cd04ec94aff261fa41feceb552f70fae9863f81f537cac0079f697589faa753ba6520b883c4
20507+
checksum: 0f3850767a1c18c02a2c002981f3d89304643ecd425d4723f0d73efebddaa3a13d9e7436a070cdd211b24a5fecd270e03686de91d316b3361de64413d2c2a7ee
2050820508
languageName: node
2050920509
linkType: hard
2051020510

@@ -27146,13 +27146,13 @@ fsevents@^1.2.7:
2714627146
languageName: node
2714727147
linkType: hard
2714827148

27149-
"typescript@npm:^4.9.3":
27150-
version: 4.9.5
27151-
resolution: "typescript@npm:4.9.5"
27149+
"typescript@npm:^5.0.0, typescript@npm:^5.2.2":
27150+
version: 5.2.2
27151+
resolution: "typescript@npm:5.2.2"
2715227152
bin:
2715327153
tsc: bin/tsc
2715427154
tsserver: bin/tsserver
27155-
checksum: ee000bc26848147ad423b581bd250075662a354d84f0e06eb76d3b892328d8d4440b7487b5a83e851b12b255f55d71835b008a66cbf8f255a11e4400159237db
27155+
checksum: 7912821dac4d962d315c36800fe387cdc0a6298dba7ec171b350b4a6e988b51d7b8f051317786db1094bd7431d526b648aba7da8236607febb26cf5b871d2d3c
2715627156
languageName: node
2715727157
linkType: hard
2715827158

@@ -27206,13 +27206,13 @@ fsevents@^1.2.7:
2720627206
languageName: node
2720727207
linkType: hard
2720827208

27209-
"typescript@patch:typescript@^4.9.3#~builtin<compat/typescript>":
27210-
version: 4.9.5
27211-
resolution: "typescript@patch:typescript@npm%3A4.9.5#~builtin<compat/typescript>::version=4.9.5&hash=701156"
27209+
"typescript@patch:typescript@^5.0.0#~builtin<compat/typescript>, typescript@patch:typescript@^5.2.2#~builtin<compat/typescript>":
27210+
version: 5.2.2
27211+
resolution: "typescript@patch:typescript@npm%3A5.2.2#~builtin<compat/typescript>::version=5.2.2&hash=701156"
2721227212
bin:
2721327213
tsc: bin/tsc
2721427214
tsserver: bin/tsserver
27215-
checksum: 2eee5c37cad4390385db5db5a8e81470e42e8f1401b0358d7390095d6f681b410f2c4a0c496c6ff9ebd775423c7785cdace7bcdad76c7bee283df3d9718c0f20
27215+
checksum: 07106822b4305de3f22835cbba949a2b35451cad50888759b6818421290ff95d522b38ef7919e70fb381c5fe9c1c643d7dea22c8b31652a717ddbd57b7f4d554
2721627216
languageName: node
2721727217
linkType: hard
2721827218

0 commit comments

Comments
 (0)