Skip to content

Commit b147ca0

Browse files
gr2mdrwpow
andauthored
fix: properties + anyOf with only required properties (#643)
* test: properties + anyOf with only required properties * fix: properties + anyOf with only required properties * style: prettier `expected/github*.ts` * test: update `expected/github*ts` fixtures * Update snapshots Co-authored-by: Drew Powers <[email protected]>
1 parent 65a54b4 commit b147ca0

File tree

6 files changed

+62
-37
lines changed

6 files changed

+62
-37
lines changed

src/transform/schema.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,19 @@ export function transformSchemaObjMap(obj: Record<string, any>, options: Transfo
3939

4040
/** transform anyOf */
4141
export function transformAnyOf(anyOf: any, options: TransformSchemaObjOptions): string {
42-
return tsIntersectionOf(anyOf.map((s: any) => tsPartial(transformSchemaObj(s, options))));
42+
// filter out anyOf keys that only have a `required` key. #642
43+
const schemas = anyOf.filter((s: any) => {
44+
if (Object.keys(s).length > 1) return true;
45+
46+
if (s.required) return false;
47+
48+
return true;
49+
});
50+
51+
if (schemas.length === 0) {
52+
return "";
53+
}
54+
return tsIntersectionOf(schemas.map((s: any) => tsPartial(transformSchemaObj(s, options))));
4355
}
4456

4557
/** transform oneOf */

src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ export function tsTupleOf(types: string[]): string {
119119

120120
/** Convert T, U into T & U; */
121121
export function tsIntersectionOf(types: string[]): string {
122-
if (types.length === 1) return types[0]; // don’t add parentheses around one thing
123-
return `(${types.join(") & (")})`;
122+
const typesWithValues = types.filter(Boolean);
123+
124+
if (typesWithValues.length === 1) return typesWithValues[0]; // don’t add parentheses around one thing
125+
return `(${typesWithValues.join(") & (")})`;
124126
}
125127

126128
/** Convert T into Partial<T> */

tests/schema.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,34 @@ describe("SchemaObject", () => {
340340
341341
})`);
342342
});
343+
344+
it("properties + anyOf with only required properties", () => {
345+
expect(
346+
transform(
347+
{
348+
properties: {
349+
a: {
350+
type: "string",
351+
},
352+
353+
b: {
354+
type: "string",
355+
},
356+
},
357+
358+
anyOf: [{ required: ["a"] }, { required: ["b"] }],
359+
},
360+
361+
{ ...defaults }
362+
)
363+
).toMatchInlineSnapshot(`
364+
"{
365+
\\"a\\"?: string;
366+
\\"b\\"?: string;
367+
368+
}"
369+
`);
370+
});
343371
});
344372

345373
describe("comments", () => {

tests/v3/expected/github.additional.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12235,15 +12235,11 @@ export interface operations {
1223512235
requestBody: {
1223612236
content: {
1223712237
"application/json":
12238-
| ((Partial<{ [key: string]: any }> & Partial<{ [key: string]: any }>) & {
12238+
| ({
1223912239
/** Description of the gist */
1224012240
description?: string;
1224112241
/** Names of files to be updated */
12242-
files?: {
12243-
[key: string]: Partial<{ [key: string]: any }> &
12244-
Partial<{ [key: string]: any }> &
12245-
Partial<{ [key: string]: any }>;
12246-
};
12242+
files?: { [key: string]: Partial<{ [key: string]: any }> };
1224712243
} & { [key: string]: any })
1224812244
| null;
1224912245
};
@@ -25505,7 +25501,7 @@ export interface operations {
2550525501
};
2550625502
requestBody: {
2550725503
content: {
25508-
"application/json": (Partial<{ [key: string]: any }> & Partial<{ [key: string]: any }>) & {
25504+
"application/json": {
2550925505
/** An array of user `login`s that will be requested. */
2551025506
reviewers?: string[];
2551125507
/** An array of team `slug`s that will be requested. */

tests/v3/expected/github.immutable.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12199,18 +12199,12 @@ export interface operations {
1219912199
};
1220012200
readonly requestBody: {
1220112201
readonly content: {
12202-
readonly "application/json":
12203-
| ((Partial<{ readonly [key: string]: any }> & Partial<{ readonly [key: string]: any }>) & {
12204-
/** Description of the gist */
12205-
readonly description?: string;
12206-
/** Names of files to be updated */
12207-
readonly files?: {
12208-
readonly [key: string]: Partial<{ readonly [key: string]: any }> &
12209-
Partial<{ readonly [key: string]: any }> &
12210-
Partial<{ readonly [key: string]: any }>;
12211-
};
12212-
})
12213-
| null;
12202+
readonly "application/json": {
12203+
/** Description of the gist */
12204+
readonly description?: string;
12205+
/** Names of files to be updated */
12206+
readonly files?: { readonly [key: string]: Partial<{ readonly [key: string]: any }> };
12207+
} | null;
1221412208
};
1221512209
};
1221612210
};
@@ -25452,8 +25446,7 @@ export interface operations {
2545225446
};
2545325447
readonly requestBody: {
2545425448
readonly content: {
25455-
readonly "application/json": (Partial<{ readonly [key: string]: any }> &
25456-
Partial<{ readonly [key: string]: any }>) & {
25449+
readonly "application/json": {
2545725450
/** An array of user `login`s that will be requested. */
2545825451
readonly reviewers?: readonly string[];
2545925452
/** An array of team `slug`s that will be requested. */

tests/v3/expected/github.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12195,18 +12195,12 @@ export interface operations {
1219512195
};
1219612196
requestBody: {
1219712197
content: {
12198-
"application/json":
12199-
| ((Partial<{ [key: string]: any }> & Partial<{ [key: string]: any }>) & {
12200-
/** Description of the gist */
12201-
description?: string;
12202-
/** Names of files to be updated */
12203-
files?: {
12204-
[key: string]: Partial<{ [key: string]: any }> &
12205-
Partial<{ [key: string]: any }> &
12206-
Partial<{ [key: string]: any }>;
12207-
};
12208-
})
12209-
| null;
12198+
"application/json": {
12199+
/** Description of the gist */
12200+
description?: string;
12201+
/** Names of files to be updated */
12202+
files?: { [key: string]: Partial<{ [key: string]: any }> };
12203+
} | null;
1221012204
};
1221112205
};
1221212206
};
@@ -25448,7 +25442,7 @@ export interface operations {
2544825442
};
2544925443
requestBody: {
2545025444
content: {
25451-
"application/json": (Partial<{ [key: string]: any }> & Partial<{ [key: string]: any }>) & {
25445+
"application/json": {
2545225446
/** An array of user `login`s that will be requested. */
2545325447
reviewers?: string[];
2545425448
/** An array of team `slug`s that will be requested. */

0 commit comments

Comments
 (0)