Skip to content

Commit f9229a2

Browse files
authored
Fix anyOf transformation (openapi-ts#895)
1 parent 0b42acd commit f9229a2

18 files changed

+17397
-16413
lines changed

src/transform/schema.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function transformAnyOf(anyOf: any, options: TransformSchemaObjOptions):
7878
if (schemas.length === 0) {
7979
return "";
8080
}
81-
return tsIntersectionOf(schemas.map((s: any) => tsPartial(transformSchemaObj(s, options))));
81+
return tsUnionOf(schemas.map((s: any) => transformSchemaObj(s, options)));
8282
}
8383

8484
/** transform oneOf */

test/core/schema.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ describe("SchemaObject", () => {
304304
{ ...defaults }
305305
)
306306
).to.equal(
307-
`(Partial<components["schemas"]["StringType"]>) & (Partial<components["schemas"]["NumberType"]>) & (Partial<components["schemas"]["BooleanType"]>)`
307+
`(components["schemas"]["StringType"]) | (components["schemas"]["NumberType"]) | (components["schemas"]["BooleanType"])`
308308
);
309309
});
310310

@@ -374,13 +374,13 @@ describe("SchemaObject", () => {
374374
},
375375
{ ...defaults }
376376
)
377-
).to.equal(`((Partial<{
377+
).to.equal(`(({
378378
"b": string;
379379
380-
}>) & (Partial<{
380+
}) | ({
381381
"c": string;
382382
383-
}>)) & ({
383+
})) & ({
384384
"a"?: string;
385385
386386
})`);

test/v3/expected/any-of.additional.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
'/cats': {
8+
post: {
9+
responses: {
10+
200: {
11+
content: {
12+
'application/json': {
13+
id: string
14+
name: string
15+
colors: (
16+
| string[]
17+
| {
18+
id: string
19+
name: string
20+
}[]
21+
) & { [key: string]: unknown }
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
export interface components {}
31+
32+
export interface operations {}
33+
34+
export interface external {}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export type paths = {
7+
'/cats': {
8+
post: {
9+
responses: {
10+
200: {
11+
content: {
12+
'application/json': {
13+
id: string
14+
name: string
15+
colors:
16+
| string[]
17+
| {
18+
id: string
19+
name: string
20+
}[]
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
29+
export type components = {}
30+
31+
export type operations = {}
32+
33+
export type external = {}

test/v3/expected/any-of.immutable.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
readonly '/cats': {
8+
readonly post: {
9+
readonly responses: {
10+
readonly 200: {
11+
readonly content: {
12+
readonly 'application/json': {
13+
readonly id: string
14+
readonly name: string
15+
readonly colors:
16+
| readonly string[]
17+
| readonly {
18+
readonly id: string
19+
readonly name: string
20+
}[]
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
29+
export interface components {}
30+
31+
export interface operations {}
32+
33+
export interface external {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
'/cats': {
8+
post: {
9+
responses: {
10+
200: {
11+
content: {
12+
'application/json': {
13+
id: string
14+
name: string
15+
colors:
16+
| string[]
17+
| {
18+
id: string
19+
name: string
20+
}[]
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
29+
export interface components {}
30+
31+
export interface operations {}
32+
33+
export interface external {}

test/v3/expected/any-of.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* This file was auto-generated by openapi-typescript.
3+
* Do not make direct changes to the file.
4+
*/
5+
6+
export interface paths {
7+
'/cats': {
8+
post: {
9+
responses: {
10+
200: {
11+
content: {
12+
'application/json': {
13+
id: string
14+
name: string
15+
colors:
16+
| string[]
17+
| {
18+
id: string
19+
name: string
20+
}[]
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
29+
export interface components {}
30+
31+
export interface operations {}
32+
33+
export interface external {}

0 commit comments

Comments
 (0)