Skip to content

Commit f64b4c5

Browse files
authored
Merge pull request #2058 from hey-api/fix/read-write-only-ref
fix(typescript): exclude in readable/writable schemas when referenced schemas don't contain any readable/writable fields
2 parents 7a49eca + e9fda0b commit f64b4c5

File tree

14 files changed

+142
-2
lines changed

14 files changed

+142
-2
lines changed

.changeset/empty-trains-repair.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix(typescript): exclude $refs in readable/writable schemas when referenced schemas don't contain any readable/writable fields

packages/openapi-ts-tests/test/__snapshots__/2.0.x/plugins/@hey-api/typescript/read-write-only-custom-name/types.gen.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ export type Baz = {
1616
baz?: string;
1717
};
1818

19+
export type QuxAllRead = {
20+
readonly baz?: string;
21+
};
22+
23+
export type ReadableQuux = {
24+
baz?: Array<Baz>;
25+
qux?: QuxAllRead;
26+
};
27+
28+
export type WritableQuux = {
29+
baz?: Array<Baz>;
30+
};
31+
1932
export type PostFooReadData = {
2033
body: WritableFooRead;
2134
path?: never;

packages/openapi-ts-tests/test/__snapshots__/2.0.x/plugins/@hey-api/typescript/read-write-only-ignore/types.gen.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ export type Baz = {
1212
baz?: string;
1313
};
1414

15+
export type QuxAllRead = {
16+
readonly baz?: string;
17+
};
18+
19+
export type Quux = {
20+
baz?: Array<Baz>;
21+
qux?: QuxAllRead;
22+
};
23+
1524
export type PostFooReadData = {
1625
body: FooRead;
1726
path?: never;

packages/openapi-ts-tests/test/__snapshots__/2.0.x/read-write-only/types.gen.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ export type Baz = {
1616
baz?: string;
1717
};
1818

19+
export type QuxAllRead = {
20+
readonly baz?: string;
21+
};
22+
23+
export type QuuxReadable = {
24+
baz?: Array<Baz>;
25+
qux?: QuxAllRead;
26+
};
27+
28+
export type QuuxWritable = {
29+
baz?: Array<Baz>;
30+
};
31+
1932
export type PostFooReadData = {
2033
body: FooReadWritable;
2134
path?: never;

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/typescript/read-write-only-custom-name/types.gen.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ export type Baz = {
3434
baz?: string;
3535
};
3636

37+
export type QuxAllRead = {
38+
readonly baz?: string;
39+
};
40+
41+
export type ReadableQuux = {
42+
baz?: Array<Baz>;
43+
qux?: QuxAllRead;
44+
};
45+
46+
export type WritableQuux = {
47+
baz?: Array<Baz>;
48+
};
49+
3750
export type PostFooReadWriteData = {
3851
body: WritableFooReadWrite;
3952
path?: never;

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/typescript/read-write-only-ignore/types.gen.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ export type Baz = {
2424
baz?: string;
2525
};
2626

27+
export type QuxAllRead = {
28+
readonly baz?: string;
29+
};
30+
31+
export type Quux = {
32+
baz?: Array<Baz>;
33+
qux?: QuxAllRead;
34+
};
35+
2736
export type PostFooReadWriteData = {
2837
body: FooReadWrite;
2938
path?: never;

packages/openapi-ts-tests/test/__snapshots__/3.0.x/read-write-only/types.gen.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ export type Baz = {
3434
baz?: string;
3535
};
3636

37+
export type QuxAllRead = {
38+
readonly baz?: string;
39+
};
40+
41+
export type QuuxReadable = {
42+
baz?: Array<Baz>;
43+
qux?: QuxAllRead;
44+
};
45+
46+
export type QuuxWritable = {
47+
baz?: Array<Baz>;
48+
};
49+
3750
export type PostFooReadWriteData = {
3851
body: FooReadWriteWritable;
3952
path?: never;

packages/openapi-ts-tests/test/__snapshots__/3.1.x/plugins/@hey-api/typescript/read-write-only-custom-name/types.gen.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ export type QuxAllRead = {
4242
readonly baz?: string;
4343
};
4444

45+
export type ReadableQuux = {
46+
baz?: Array<Baz>;
47+
qux?: QuxAllRead;
48+
};
49+
50+
export type WritableQuux = {
51+
baz?: Array<Baz>;
52+
};
53+
4554
export type PostFooReadWriteData = {
4655
body: WritableFooReadWrite;
4756
path?: never;

packages/openapi-ts-tests/test/__snapshots__/3.1.x/plugins/@hey-api/typescript/read-write-only-ignore/types.gen.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export type QuxAllRead = {
3232
readonly baz?: string;
3333
};
3434

35+
export type Quux = {
36+
baz?: Array<Baz>;
37+
qux?: QuxAllRead;
38+
};
39+
3540
export type PostFooReadWriteData = {
3641
body: FooReadWrite;
3742
path?: never;

packages/openapi-ts-tests/test/__snapshots__/3.1.x/read-write-only/types.gen.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ export type QuxAllRead = {
4242
readonly baz?: string;
4343
};
4444

45+
export type QuuxReadable = {
46+
baz?: Array<Baz>;
47+
qux?: QuxAllRead;
48+
};
49+
50+
export type QuuxWritable = {
51+
baz?: Array<Baz>;
52+
};
53+
4554
export type PostFooReadWriteData = {
4655
body: FooReadWriteWritable;
4756
path?: never;

packages/openapi-ts-tests/test/spec/2.0.x/read-write-only.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,18 @@ definitions:
4242
properties:
4343
baz:
4444
type: string
45+
QuxAllRead:
46+
type: object
47+
properties:
48+
baz:
49+
readOnly: true
50+
type: string
51+
Quux:
52+
type: object
53+
properties:
54+
baz:
55+
type: array
56+
items:
57+
$ref: '#/definitions/Baz'
58+
qux:
59+
$ref: '#/definitions/QuxAllRead'

packages/openapi-ts-tests/test/spec/3.0.x/read-write-only.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,18 @@ components:
9595
properties:
9696
baz:
9797
type: string
98+
QuxAllRead:
99+
type: object
100+
properties:
101+
baz:
102+
readOnly: true
103+
type: string
104+
Quux:
105+
type: object
106+
properties:
107+
baz:
108+
type: array
109+
items:
110+
$ref: '#/components/schemas/Baz'
111+
qux:
112+
$ref: '#/components/schemas/QuxAllRead'

packages/openapi-ts-tests/test/spec/3.1.x/read-write-only.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,12 @@ components:
111111
baz:
112112
readOnly: true
113113
type: string
114+
Quux:
115+
type: object
116+
properties:
117+
baz:
118+
type: array
119+
items:
120+
$ref: '#/components/schemas/Baz'
121+
qux:
122+
$ref: '#/components/schemas/QuxAllRead'

packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ const shouldSkipSchema = ({
8989
}) =>
9090
Boolean(
9191
state?.accessScope &&
92-
schema.accessScope &&
93-
state.accessScope !== schema.accessScope,
92+
((schema.accessScope && state.accessScope !== schema.accessScope) ||
93+
(schema.$ref &&
94+
schema.accessScopes &&
95+
!schema.accessScopes.includes(state.accessScope) &&
96+
!schema.accessScopes.includes('both'))),
9497
);
9598

9699
const addJavaScriptEnum = ({

0 commit comments

Comments
 (0)