Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | import { readFileSync } from 'node:fs'; |
| 3 | import { join } from 'node:path'; |
| 4 | import { hideDeprecatedFields } from '../../lib/openapi-deprecated'; |
| 5 | import { openapi, openapiV3 } from '../../lib/openapi'; |
| 6 | |
| 7 | describe('playground OpenAPI deprecation filter', () => { |
| 8 | test('removes fields and their values while preserving the source and legacy operations', () => { |
| 9 | const schema = { |
| 10 | type: 'object', |
| 11 | properties: { |
| 12 | current: { type: 'string' }, |
| 13 | old: { deprecated: true }, |
| 14 | nested: { type: 'array', items: { properties: { old: { deprecated: true }, keep: {} } } }, |
| 15 | metadata: { type: 'object', additionalProperties: true }, |
| 16 | }, |
| 17 | required: ['current', 'old'], |
| 18 | default: { |
| 19 | current: 'yes', |
| 20 | old: 'no', |
| 21 | nested: [{ old: 1, keep: 2 }], |
| 22 | metadata: { deprecated: true }, |
| 23 | }, |
| 24 | }; |
| 25 | const source = { |
| 26 | paths: { |
| 27 | '/test': { |
| 28 | post: { |
| 29 | deprecated: true, |
| 30 | requestBody: { |
| 31 | content: { |
| 32 | 'application/json': { |
| 33 | schema, |
| 34 | example: schema.default, |
| 35 | examples: { named: { value: schema.default } }, |
| 36 | }, |
| 37 | }, |
| 38 | }, |
| 39 | }, |
| 40 | }, |
| 41 | }, |
| 42 | }; |
| 43 | const original = structuredClone(source); |
| 44 | const result = hideDeprecatedFields(source); |
| 45 | const media = result.paths['/test'].post.requestBody.content['application/json']; |
| 46 | const expected = { current: 'yes', nested: [{ keep: 2 }], metadata: { deprecated: true } }; |
| 47 | expect(media.schema.properties).not.toHaveProperty('old'); |
| 48 | expect(media.schema.required).toEqual(['current']); |
| 49 | expect(media.schema.default).toEqual(expected); |
| 50 | expect(media.example).toEqual(expected); |
| 51 | expect(media.examples.named.value).toEqual(expected); |
| 52 | expect(result.paths['/test'].post.deprecated).toBe(true); |
| 53 | expect(source).toEqual(original); |
| 54 | expect(hideDeprecatedFields(result)).toEqual(result); |
| 55 | }); |
| 56 | |
| 57 | test('handles references, aliases, compositions, recursion and boolean schemas', () => { |
| 58 | const source = { |
| 59 | components: { |
| 60 | schemas: { |
| 61 | 'Old/Field': { deprecated: true }, |
| 62 | Alias: { $ref: '#/components/schemas/Old~1Field' }, |
| 63 | Recursive: { |
| 64 | properties: { |
| 65 | old: { deprecated: true }, |
| 66 | next: { $ref: '#/components/schemas/Recursive' }, |
| 67 | }, |
| 68 | }, |
| 69 | Composed: { |
| 70 | allOf: [{ properties: { old: { deprecated: true }, keep: true }, required: ['old'] }], |
| 71 | }, |
| 72 | Root: { |
| 73 | properties: { |
| 74 | alias: { $ref: '#/components/schemas/Alias' }, |
| 75 | retained: { $ref: '#/components/schemas/Alias', deprecated: false }, |
| 76 | recursive: { $ref: '#/components/schemas/Recursive' }, |
| 77 | external: { $ref: 'https://example.com/schema.json' }, |
| 78 | boolean: true, |
| 79 | }, |
| 80 | default: { alias: 1, retained: 2, recursive: { old: 1, next: { old: 2 } } }, |
| 81 | }, |
| 82 | }, |
| 83 | }, |
| 84 | }; |
| 85 | const result = hideDeprecatedFields(source).components.schemas; |
| 86 | expect(result.Root.properties).not.toHaveProperty('alias'); |
| 87 | expect(result.Root.properties.retained).toEqual( |
| 88 | source.components.schemas.Root.properties.retained |
| 89 | ); |
| 90 | expect(result.Root.properties.external).toEqual( |
| 91 | source.components.schemas.Root.properties.external |
| 92 | ); |
| 93 | expect(result.Root.properties.boolean).toBe(true); |
| 94 | expect(result.Root.default).toEqual({ retained: 2, recursive: { next: {} } }); |
| 95 | expect(result.Recursive.properties).not.toHaveProperty('old'); |
| 96 | expect(result.Composed.allOf[0].properties).toEqual({ keep: true }); |
| 97 | expect(result.Composed.allOf[0].required).toEqual([]); |
| 98 | }); |
| 99 | |
| 100 | test('cleans composed requirements and referenced examples without deleting current union fields', () => { |
| 101 | const source = { |
| 102 | components: { examples: { shared: { value: { old: 1, keep: 2 } } } }, |
| 103 | paths: { |
| 104 | '/test': { |
| 105 | post: { |
| 106 | requestBody: { |
| 107 | content: { |
| 108 | 'application/json': { |
| 109 | schema: { |
| 110 | allOf: [{ properties: { old: { deprecated: true }, keep: {} } }], |
| 111 | required: ['old', 'keep'], |
| 112 | }, |
| 113 | examples: { sample: { $ref: '#/components/examples/shared' } }, |
| 114 | }, |
| 115 | }, |
| 116 | }, |
| 117 | }, |
| 118 | }, |
| 119 | }, |
| 120 | }; |
| 121 | const result = hideDeprecatedFields(source); |
| 122 | const media = result.paths['/test'].post.requestBody.content['application/json']; |
| 123 | expect(media.schema.required).toEqual(['keep']); |
| 124 | expect(media.examples.sample).toEqual({ value: { keep: 2 } }); |
| 125 | expect(result.components.examples.shared.value).toEqual({ old: 1, keep: 2 }); |
| 126 | const union = { |
| 127 | components: { |
| 128 | schemas: { |
| 129 | Union: { |
| 130 | oneOf: [{ properties: { field: { deprecated: true } } }, { properties: { field: {} } }], |
| 131 | default: { field: 'still supported' }, |
| 132 | }, |
| 133 | }, |
| 134 | }, |
| 135 | }; |
| 136 | expect(hideDeprecatedFields(union).components.schemas.Union.default).toEqual({ |
| 137 | field: 'still supported', |
| 138 | }); |
| 139 | }); |
| 140 | |
| 141 | test('does not treat examples or extension values as schema definitions', () => { |
| 142 | const payload = { schema: { properties: { old: { deprecated: true } } } }; |
| 143 | const source = { example: payload, 'x-custom': payload }; |
| 144 | expect(hideDeprecatedFields(source)).toEqual(source); |
| 145 | }); |
| 146 | |
| 147 | test('keeps deprecated reference fields and filters only the playground copy', async () => { |
| 148 | for (const server of [openapi, openapiV3]) { |
| 149 | const schemas = await server.getSchemas(); |
| 150 | for (const [path, { bundled }] of Object.entries(schemas)) { |
| 151 | if (path.endsWith('openapi-webhooks.json')) continue; |
| 152 | const original = structuredClone(bundled); |
| 153 | const reference = bundled.components?.schemas?.ComposioManagedAuthConfigCreate; |
| 154 | expect(reference).toHaveProperty('properties.tool_access_config'); |
| 155 | expect(reference).toHaveProperty('properties.restrict_to_following_tools'); |
| 156 | const playground = hideDeprecatedFields(bundled); |
| 157 | const managed = playground.components?.schemas?.ComposioManagedAuthConfigCreate; |
| 158 | expect(managed).toHaveProperty('properties.credentials'); |
| 159 | expect(managed).not.toHaveProperty('properties.tool_access_config'); |
| 160 | expect(managed).not.toHaveProperty('properties.restrict_to_following_tools'); |
| 161 | expect(playground.components?.schemas?.CustomAuthConfigCreate).not.toHaveProperty( |
| 162 | 'properties.tool_access_config' |
| 163 | ); |
| 164 | const operation = Object.values(playground.paths ?? {}) |
| 165 | .flatMap(item => item?.post ?? []) |
| 166 | .find( |
| 167 | item => |
| 168 | item.operationId === 'postAuthConfigs' || item.operationId === 'postApiV3AuthConfigs' |
| 169 | ); |
| 170 | expect(operation).toBeDefined(); |
| 171 | expect(JSON.stringify(operation?.requestBody)).not.toContain('restrict_to_following_tools'); |
| 172 | expect(bundled).toEqual(original); |
| 173 | } |
| 174 | } |
| 175 | // The downloadable contract remains complete on disk. |
| 176 | expect(readFileSync(join(process.cwd(), 'public/openapi.json'), 'utf8')).toContain( |
| 177 | '"restrict_to_following_tools"' |
| 178 | ); |
| 179 | }); |
| 180 | }); |