Skip to content

Commit 713ecab

Browse files
committed
fix: fix the problem with empty objects in intersections
Ticket: DX-2181
1 parent 6dfc168 commit 713ecab

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

packages/openapi-generator/src/optimize.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { combineComments } from './comments';
2-
import { isPrimitive, type Primitive, type Schema } from './ir';
2+
import { isPrimitive, type CombinedType, type Primitive, type Schema } from './ir';
33

44
export type OptimizeFn = (schema: Schema) => Schema;
55

@@ -29,6 +29,28 @@ export function foldIntersection(schema: Schema, optimize: OptimizeFn): Schema {
2929
}
3030
});
3131

32+
// If the combined object is empty (no properties) and result is an intersection,
33+
// we can simplify by removing the empty object
34+
const hasProperties = Object.keys(combinedObject.properties).length > 0;
35+
36+
if (!hasProperties && result !== combinedObject) {
37+
// At this point, result must be an intersection since it was reassigned
38+
const intersectionResult = result as unknown as CombinedType;
39+
40+
// Remove the empty object from the intersection
41+
const nonEmptySchemas = intersectionResult.schemas.filter(
42+
(s: Schema) => !(s.type === 'object' && Object.keys(s.properties).length === 0),
43+
);
44+
45+
if (nonEmptySchemas.length === 0) {
46+
return combinedObject;
47+
} else if (nonEmptySchemas.length === 1) {
48+
return nonEmptySchemas[0]!;
49+
} else {
50+
return { type: 'intersection', schemas: nonEmptySchemas };
51+
}
52+
}
53+
3254
return result;
3355
}
3456

0 commit comments

Comments
 (0)