Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 180 lines 5.91 KB
Raw
1 import { describe, it, expect } from 'vitest';
2 import { relaxOutputSchema } from '../src/relax-output-schema';
3
4 describe('relaxOutputSchema (issue #3047)', () => {
5 it('adds "null" to a primitive type so API nulls validate', () => {
6 expect(relaxOutputSchema({ type: 'string' })).toEqual({ type: ['string', 'null'] });
7 });
8
9 it('appends "null" to an existing type array without duplicating it', () => {
10 expect(relaxOutputSchema({ type: ['string', 'number'] })).toEqual({
11 type: ['string', 'number', 'null'],
12 });
13 expect(relaxOutputSchema({ type: ['string', 'null'] })).toEqual({
14 type: ['string', 'null'],
15 });
16 });
17
18 it('relaxes nested object properties, flips additionalProperties false → true, and drops required', () => {
19 const input = {
20 type: 'object',
21 additionalProperties: false,
22 properties: {
23 avatarUrl: { type: 'string' },
24 assignee: {
25 type: 'object',
26 additionalProperties: false,
27 properties: { name: { type: 'string' } },
28 },
29 },
30 required: ['avatarUrl'],
31 };
32
33 // `required` is dropped (API may omit `avatarUrl` entirely), nested objects
34 // are relaxed the same way, and nothing else is altered.
35 expect(relaxOutputSchema(input)).toEqual({
36 type: ['object', 'null'],
37 additionalProperties: true,
38 properties: {
39 avatarUrl: { type: ['string', 'null'] },
40 assignee: {
41 type: ['object', 'null'],
42 additionalProperties: true,
43 properties: { name: { type: ['string', 'null'] } },
44 },
45 },
46 });
47 });
48
49 it('drops required so an omitted field still validates (issue #3047 gap)', () => {
50 const input = {
51 type: 'object',
52 additionalProperties: false,
53 required: ['id', 'name'],
54 properties: { id: { type: 'string' }, name: { type: 'string' } },
55 };
56 expect(relaxOutputSchema(input)).toEqual({
57 type: ['object', 'null'],
58 additionalProperties: true,
59 properties: { id: { type: ['string', 'null'] }, name: { type: ['string', 'null'] } },
60 });
61 });
62
63 it('widens an enum to also admit null without dropping the other members', () => {
64 expect(relaxOutputSchema({ type: 'string', enum: ['open', 'closed'] })).toEqual({
65 type: ['string', 'null'],
66 enum: ['open', 'closed', null],
67 });
68 // idempotent: an enum that already allows null is left as-is
69 expect(relaxOutputSchema({ type: 'string', enum: ['open', null] })).toEqual({
70 type: ['string', 'null'],
71 enum: ['open', null],
72 });
73 });
74
75 it('converts const into a nullable two-member enum', () => {
76 expect(relaxOutputSchema({ const: 'fixed' })).toEqual({ enum: ['fixed', null] });
77 });
78
79 it('adds additionalProperties: true to an object that omits it', () => {
80 expect(relaxOutputSchema({ type: 'object', properties: {} })).toEqual({
81 type: ['object', 'null'],
82 additionalProperties: true,
83 properties: {},
84 });
85 });
86
87 it('does not touch additionalProperties on non-object (primitive) nodes', () => {
88 expect(relaxOutputSchema({ type: 'string' })).not.toHaveProperty('additionalProperties');
89 });
90
91 it('preserves and recurses into a schema-valued additionalProperties', () => {
92 const input = {
93 type: 'object',
94 additionalProperties: { type: 'string' },
95 };
96 expect(relaxOutputSchema(input)).toEqual({
97 type: ['object', 'null'],
98 additionalProperties: { type: ['string', 'null'] },
99 });
100 });
101
102 it('relaxes array item schemas', () => {
103 const input = {
104 type: 'array',
105 items: {
106 type: 'object',
107 additionalProperties: false,
108 properties: { id: { type: 'number' } },
109 },
110 };
111 expect(relaxOutputSchema(input)).toEqual({
112 type: ['array', 'null'],
113 items: {
114 type: ['object', 'null'],
115 additionalProperties: true,
116 properties: { id: { type: ['number', 'null'] } },
117 },
118 });
119 });
120
121 it('recurses into anyOf/oneOf/allOf branches', () => {
122 const input = {
123 anyOf: [{ type: 'string' }, { type: 'object', properties: { a: { type: 'boolean' } } }],
124 };
125 expect(relaxOutputSchema(input)).toEqual({
126 anyOf: [
127 { type: ['string', 'null'] },
128 {
129 type: ['object', 'null'],
130 additionalProperties: true,
131 properties: { a: { type: ['boolean', 'null'] } },
132 },
133 ],
134 });
135 });
136
137 it('leaves a `not` subschema untouched so relaxation never narrows validation', () => {
138 // `not` negates its subschema: relaxing the inner schema (making it
139 // nullable) would *reject* values that were valid before (e.g. `null` under
140 // `not: { type: 'string' }`), violating the only-widen invariant. The inner
141 // schema must therefore be preserved verbatim, even when nested in an object
142 // that is itself relaxed.
143 expect(relaxOutputSchema({ not: { type: 'string' } })).toEqual({ not: { type: 'string' } });
144 expect(
145 relaxOutputSchema({
146 type: 'object',
147 additionalProperties: false,
148 properties: { x: { not: { type: 'string' } } },
149 })
150 ).toEqual({
151 type: ['object', 'null'],
152 additionalProperties: true,
153 properties: { x: { not: { type: 'string' } } },
154 });
155 });
156
157 it('returns an empty schema unchanged (no type, no properties)', () => {
158 expect(relaxOutputSchema({})).toEqual({});
159 });
160
161 it('does not mutate the input schema (including required/enum/const paths)', () => {
162 const input = {
163 type: 'object',
164 additionalProperties: false,
165 required: ['a'],
166 properties: {
167 a: { type: 'string', enum: ['x', 'y'] },
168 b: { const: 'z' },
169 },
170 };
171 const snapshot = JSON.parse(JSON.stringify(input));
172 relaxOutputSchema(input);
173 expect(input).toEqual(snapshot);
174 });
175
176 it('passes through non-object inputs untouched', () => {
177 expect(relaxOutputSchema(null as unknown as object)).toBeNull();
178 expect(relaxOutputSchema(undefined as unknown as object)).toBeUndefined();
179 });
180 });