|
| 1 | +import { GlobalParameterConsolidator } from '../src/GlobalParamWrapper'; |
| 2 | +import { OpenAPIV3 } from 'openapi-types'; |
| 3 | + |
| 4 | +describe('GlobalParameterConsolidator', () => { |
| 5 | + describe('consolidate', () => { |
| 6 | + it('should consolidate global parameters and replace in operations', () => { |
| 7 | + const spec: OpenAPIV3.Document = { |
| 8 | + openapi: '3.1.0', |
| 9 | + info: { title: 'Test API', version: '1.0.0' }, |
| 10 | + paths: { |
| 11 | + '/test': { |
| 12 | + get: { |
| 13 | + responses: {}, |
| 14 | + parameters: [ |
| 15 | + { $ref: '#/components/parameters/_global___query::pretty' }, |
| 16 | + { name: 'id', in: 'path', schema: { type: 'string' } } |
| 17 | + ] |
| 18 | + } |
| 19 | + }, |
| 20 | + '/search': { |
| 21 | + post: { |
| 22 | + responses: {}, |
| 23 | + parameters: [ |
| 24 | + { $ref: '#/components/parameters/_global___query::pretty' }, |
| 25 | + { $ref: '#/components/parameters/_global___query::format' }, |
| 26 | + { name: 'query', in: 'body', schema: { type: 'string' } } |
| 27 | + ] |
| 28 | + } |
| 29 | + } |
| 30 | + }, |
| 31 | + components: { |
| 32 | + parameters: { |
| 33 | + '_global___query::pretty': { |
| 34 | + name: 'pretty', |
| 35 | + in: 'query', |
| 36 | + description: 'Pretty print', |
| 37 | + schema: { type: 'boolean' } |
| 38 | + }, |
| 39 | + '_global___query::format': { |
| 40 | + name: 'format', |
| 41 | + in: 'query', |
| 42 | + description: 'Response format', |
| 43 | + schema: { type: 'string', enum: ['json', 'yaml'] } |
| 44 | + }, |
| 45 | + 'regular_param': { |
| 46 | + name: 'id', |
| 47 | + in: 'path', |
| 48 | + schema: { type: 'string' } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + const consolidator = new GlobalParameterConsolidator(spec); |
| 55 | + const result = consolidator.consolidate(); |
| 56 | + |
| 57 | + // Should create GlobalParams schema with only global parameters |
| 58 | + expect(result.components?.schemas?.GlobalParams).toBeDefined(); |
| 59 | + const globalParams = result.components!.schemas!.GlobalParams as OpenAPIV3.SchemaObject; |
| 60 | + expect(globalParams.type).toBe('object'); |
| 61 | + expect(globalParams.properties).toHaveProperty('pretty'); |
| 62 | + expect(globalParams.properties).toHaveProperty('format'); |
| 63 | + expect(globalParams.properties).not.toHaveProperty('id'); // regular param excluded |
| 64 | + |
| 65 | + // Should create globalParams parameter reference |
| 66 | + expect(result.components?.parameters?.globalParams).toBeDefined(); |
| 67 | + const globalParam = result.components!.parameters!.globalParams as any; |
| 68 | + expect(globalParam.name).toBe('globalParams'); |
| 69 | + expect(globalParam.in).toBe('query'); |
| 70 | + expect(globalParam.schema.$ref).toBe('#/components/schemas/GlobalParams'); |
| 71 | + |
| 72 | + // Should replace single global param in GET operation |
| 73 | + const getOp = result.paths!['/test']!.get!; |
| 74 | + expect(getOp.parameters).toHaveLength(2); |
| 75 | + expect(getOp.parameters).toContainEqual({ $ref: '#/components/parameters/globalParams' }); |
| 76 | + |
| 77 | + // Should consolidate multiple global params into one in POST operation |
| 78 | + const postOp = result.paths!['/search']!.post!; |
| 79 | + expect(postOp.parameters).toHaveLength(2); |
| 80 | + const globalCount = postOp.parameters!.filter(p => |
| 81 | + (p as any).$ref === '#/components/parameters/globalParams' |
| 82 | + ).length; |
| 83 | + expect(globalCount).toBe(1); // Only one globalParams reference |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle edge cases', () => { |
| 87 | + // Test with no components/parameters |
| 88 | + const emptySpec: OpenAPIV3.Document = { |
| 89 | + openapi: '3.1.0', |
| 90 | + info: { title: 'Test API', version: '1.0.0' }, |
| 91 | + paths: {}, |
| 92 | + components: { parameters: {} } |
| 93 | + }; |
| 94 | + |
| 95 | + const consolidator1 = new GlobalParameterConsolidator(emptySpec); |
| 96 | + const result1 = consolidator1.consolidate(); |
| 97 | + expect(result1.components?.schemas?.GlobalParams).toBeDefined(); |
| 98 | + expect(result1.components?.parameters?.globalParams).toBeDefined(); |
| 99 | + |
| 100 | + // Test preserves non-global parameters |
| 101 | + const mixedSpec: OpenAPIV3.Document = { |
| 102 | + openapi: '3.1.0', |
| 103 | + info: { title: 'Test API', version: '1.0.0' }, |
| 104 | + paths: { |
| 105 | + '/users/{id}': { |
| 106 | + get: { |
| 107 | + responses: {}, |
| 108 | + parameters: [ |
| 109 | + { name: 'id', in: 'path', schema: { type: 'string' } }, |
| 110 | + { $ref: '#/components/parameters/_global___query::pretty' }, |
| 111 | + { name: 'include', in: 'query', schema: { type: 'string' } } |
| 112 | + ] |
| 113 | + } |
| 114 | + } |
| 115 | + }, |
| 116 | + components: { |
| 117 | + parameters: { |
| 118 | + '_global___query::pretty': { |
| 119 | + name: 'pretty', |
| 120 | + in: 'query', |
| 121 | + schema: { type: 'boolean' } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + const consolidator2 = new GlobalParameterConsolidator(mixedSpec); |
| 128 | + const result2 = consolidator2.consolidate(); |
| 129 | + const getOp = result2.paths!['/users/{id}']!.get!; |
| 130 | + expect(getOp.parameters).toHaveLength(3); |
| 131 | + expect(getOp.parameters!.find(p => (p as OpenAPIV3.ParameterObject).name === 'id')).toBeDefined(); |
| 132 | + expect(getOp.parameters!.find(p => (p as OpenAPIV3.ParameterObject).name === 'include')).toBeDefined(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should handle all HTTP methods', () => { |
| 136 | + const spec: OpenAPIV3.Document = { |
| 137 | + openapi: '3.1.0', |
| 138 | + info: { title: 'Test API', version: '1.0.0' }, |
| 139 | + paths: { |
| 140 | + '/resource': { |
| 141 | + get: { |
| 142 | + responses: {}, |
| 143 | + parameters: [{ $ref: '#/components/parameters/_global___query::pretty' }] |
| 144 | + }, |
| 145 | + post: { |
| 146 | + responses: {}, |
| 147 | + parameters: [{ $ref: '#/components/parameters/_global___query::pretty' }] |
| 148 | + }, |
| 149 | + put: { |
| 150 | + responses: {}, |
| 151 | + parameters: [{ $ref: '#/components/parameters/_global___query::pretty' }] |
| 152 | + }, |
| 153 | + delete: { |
| 154 | + responses: {}, |
| 155 | + parameters: [{ $ref: '#/components/parameters/_global___query::pretty' }] |
| 156 | + } |
| 157 | + } |
| 158 | + }, |
| 159 | + components: { |
| 160 | + parameters: { |
| 161 | + '_global___query::pretty': { |
| 162 | + name: 'pretty', |
| 163 | + in: 'query', |
| 164 | + schema: { type: 'boolean' } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + }; |
| 169 | + |
| 170 | + const consolidator = new GlobalParameterConsolidator(spec); |
| 171 | + const result = consolidator.consolidate(); |
| 172 | + |
| 173 | + ['get', 'post', 'put', 'delete'].forEach(method => { |
| 174 | + const operation = result.paths!['/resource']![method as keyof OpenAPIV3.PathItemObject]; |
| 175 | + expect((operation as OpenAPIV3.OperationObject).parameters).toContainEqual( |
| 176 | + { $ref: '#/components/parameters/globalParams' } |
| 177 | + ); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should not duplicate parameters with same name', () => { |
| 182 | + const spec: OpenAPIV3.Document = { |
| 183 | + openapi: '3.1.0', |
| 184 | + info: { title: 'Test API', version: '1.0.0' }, |
| 185 | + paths: {}, |
| 186 | + components: { |
| 187 | + parameters: { |
| 188 | + '_global___query::pretty': { |
| 189 | + name: 'pretty', |
| 190 | + in: 'query', |
| 191 | + schema: { type: 'boolean' } |
| 192 | + }, |
| 193 | + '_global___query::pretty_duplicate': { |
| 194 | + name: 'pretty', // Same name |
| 195 | + in: 'query', |
| 196 | + schema: { type: 'boolean' } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + }; |
| 201 | + |
| 202 | + const consolidator = new GlobalParameterConsolidator(spec); |
| 203 | + const result = consolidator.consolidate(); |
| 204 | + |
| 205 | + const globalParams = result.components!.schemas!.GlobalParams as OpenAPIV3.SchemaObject; |
| 206 | + const prettyKeys = Object.keys(globalParams.properties || {}).filter(k => k === 'pretty'); |
| 207 | + expect(prettyKeys).toHaveLength(1); |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments