Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/eager-moles-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Fix redundant nested unions from a type array with a sibling `anyOf`/`allOf`, including when a `oneOf` sits alongside them.
2 changes: 1 addition & 1 deletion packages/openapi-typescript/examples/github-api-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35615,7 +35615,7 @@ export interface components {
* @description User-defined metadata to store domain-specific information limited to 8 keys with scalar values.
*/
metadata: {
[key: string]: (null | (string | number | boolean) | (number | string | boolean) | (boolean | string | number)) | string | number | boolean;
[key: string]: (null | string | number | boolean) | string | number | boolean;
};
dependency: {
/**
Expand Down
31 changes: 18 additions & 13 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,22 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
if (Array.isArray(schemaObject.type) && !Array.isArray(schemaObject)) {
// skip any primitive types that appear in oneOf as well
const uniqueTypes: ts.TypeNode[] = [];
// The caller transforms the sibling composition keywords itself, so drop them here
// or each type member re-applies them. 'type' alone fully describes a primitive
// member; object/array members keep anyOf/allOf, because there the composition is
// what carries the shape and stripping it leaves Record<string, never> / unknown[].
const transformTypeMember = (t: string) => {
const stackable = t === "object" || t === "array";
return transformSchemaObject(
{
...schemaObject,
type: t,
oneOf: undefined,
...(stackable ? {} : { anyOf: undefined, allOf: undefined }),
} as SchemaObject,
options,
);
};
if (Array.isArray(schemaObject.oneOf)) {
for (const t of schemaObject.type) {
if (
Expand All @@ -475,22 +491,11 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
) {
continue;
}
uniqueTypes.push(
t === "null" || t === null
? NULL
: transformSchemaObject(
{ ...schemaObject, type: t, oneOf: undefined } as SchemaObject, // don’t stack oneOf transforms
options,
),
);
uniqueTypes.push(t === "null" || t === null ? NULL : transformTypeMember(t));
}
} else {
for (const t of schemaObject.type) {
if (t === "null" || t === null) {
uniqueTypes.push(NULL);
} else {
uniqueTypes.push(transformSchemaObject({ ...schemaObject, type: t } as SchemaObject, options));
}
uniqueTypes.push(t === "null" || t === null ? NULL : transformTypeMember(t));
}
}
return tsUnion(uniqueTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,84 @@ describe("composition", () => {
// options: DEFAULT_OPTIONS,
},
],
[
"polymorphic > anyOf + nullable",
{
given: {
type: ["string", "null"],
anyOf: [{ type: "string", format: "ipv4" }, { type: "string", format: "ipv6" }, { type: "null" }],
},
want: "(string | null) | string | null",
// options: DEFAULT_OPTIONS,
},
],
[
"polymorphic > allOf + nullable",
{
given: {
type: ["string", "null"],
allOf: [{ type: "string" }],
},
want: "(string | null) & string",
// options: DEFAULT_OPTIONS,
},
],
[
"polymorphic > allOf + nullable, multiple members",
{
given: {
type: ["string", "null"],
allOf: [{ type: "string" }, { type: "string", format: "uuid" }],
},
want: "(string | null) & (string)",
// options: DEFAULT_OPTIONS,
},
],
[
"polymorphic > anyOf + nullable array",
{
given: {
type: ["array", "null"],
anyOf: [{ type: "array", items: { type: "string" } }],
},
want: "((unknown[] | string[]) | null) | string[]",
// options: DEFAULT_OPTIONS,
},
],
[
"polymorphic > oneOf + allOf + nullable",
{
given: {
type: ["string", "null"],
oneOf: [{ type: "number" }],
allOf: [{ type: "string" }],
},
want: "((string | null) & string) | number",
// options: DEFAULT_OPTIONS,
},
],
[
"polymorphic > anyOf + nullable object",
{
given: {
type: ["null", "object"],
anyOf: [
{ type: "object", properties: { a: { type: "string" } } },
{ type: "object", properties: { b: { type: "string" } } },
],
},
want: `(null | ({
a?: string;
} | {
b?: string;
})) | {
a?: string;
} | {
b?: string;
}`,
// options: DEFAULT_OPTIONS,
},
],
[
"oneOf > primitives",
{
Expand Down
Loading