fix(openapi-typescript): redundant nested unions from type arrays with anyOf/allOf by sovrin-tio · Pull Request #2855 · openapi-ts/openapi-typescript · GitHub
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
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
Loading