|
| 1 | +import { TypeParser } from '../../generators/TypeParser'; |
| 2 | +import { UtilityHelpers } from '../../generators/UtilityHelpers'; |
| 3 | +import { OpenAPISpec } from '../../interfaces/OpenAPISchema'; |
| 4 | + |
| 5 | +describe('TypeParser - Hash wrapper pattern handling', () => { |
| 6 | + let typeParser: TypeParser; |
| 7 | + let spec: OpenAPISpec; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + const utilityHelpers = new UtilityHelpers(); |
| 11 | + typeParser = new TypeParser(utilityHelpers); |
| 12 | + |
| 13 | + // Create a minimal spec with some test entities |
| 14 | + spec = { |
| 15 | + openapi: '3.1.0', |
| 16 | + info: { title: 'Test', version: '1.0.0' }, |
| 17 | + paths: {}, |
| 18 | + components: { |
| 19 | + schemas: { |
| 20 | + AsyncRefresh: { |
| 21 | + type: 'object', |
| 22 | + properties: { |
| 23 | + id: { type: 'string' }, |
| 24 | + status: { type: 'string' }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }; |
| 30 | + }); |
| 31 | + |
| 32 | + describe('Hash with a single key of `key_name`', () => { |
| 33 | + it('should handle simple hash wrapper with count', () => { |
| 34 | + const result = typeParser.parseResponseSchema( |
| 35 | + 'Hash with a single key of `count`', |
| 36 | + spec |
| 37 | + ); |
| 38 | + |
| 39 | + expect(result).toEqual({ |
| 40 | + type: 'object', |
| 41 | + properties: { |
| 42 | + count: { |
| 43 | + type: 'integer', |
| 44 | + }, |
| 45 | + }, |
| 46 | + required: ['count'], |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should handle simple hash wrapper with different key name', () => { |
| 51 | + const result = typeParser.parseResponseSchema( |
| 52 | + 'Hash with a single key of `merged`', |
| 53 | + spec |
| 54 | + ); |
| 55 | + |
| 56 | + expect(result).toEqual({ |
| 57 | + type: 'object', |
| 58 | + properties: { |
| 59 | + merged: { |
| 60 | + type: 'integer', |
| 61 | + }, |
| 62 | + }, |
| 63 | + required: ['merged'], |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('Hash with a single key of `key_name` with value of [Entity]', () => { |
| 69 | + it('should handle hash wrapper with entity reference', () => { |
| 70 | + const result = typeParser.parseResponseSchema( |
| 71 | + 'Hash with a single key of `async_refresh` with value of [AsyncRefresh]', |
| 72 | + spec |
| 73 | + ); |
| 74 | + |
| 75 | + expect(result).toEqual({ |
| 76 | + type: 'object', |
| 77 | + properties: { |
| 78 | + async_refresh: { |
| 79 | + $ref: '#/components/schemas/AsyncRefresh', |
| 80 | + }, |
| 81 | + }, |
| 82 | + required: ['async_refresh'], |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle hash wrapper with different key and entity', () => { |
| 87 | + // Add another entity to the spec |
| 88 | + spec.components!.schemas!['Status'] = { |
| 89 | + type: 'object', |
| 90 | + properties: { |
| 91 | + id: { type: 'string' }, |
| 92 | + content: { type: 'string' }, |
| 93 | + }, |
| 94 | + }; |
| 95 | + |
| 96 | + const result = typeParser.parseResponseSchema( |
| 97 | + 'Hash with a single key of `status` with value of [Status]', |
| 98 | + spec |
| 99 | + ); |
| 100 | + |
| 101 | + expect(result).toEqual({ |
| 102 | + type: 'object', |
| 103 | + properties: { |
| 104 | + status: { |
| 105 | + $ref: '#/components/schemas/Status', |
| 106 | + }, |
| 107 | + }, |
| 108 | + required: ['status'], |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should return null if entity does not exist in schema', () => { |
| 113 | + const result = typeParser.parseResponseSchema( |
| 114 | + 'Hash with a single key of `nonexistent` with value of [NonexistentEntity]', |
| 115 | + spec |
| 116 | + ); |
| 117 | + |
| 118 | + // Should fall through to later patterns or return null |
| 119 | + expect(result).toBeNull(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('Hash with a single [type] attribute `key_name`', () => { |
| 124 | + it('should handle boolean attribute', () => { |
| 125 | + const result = typeParser.parseResponseSchema( |
| 126 | + 'Hash with a single boolean attribute `merged`', |
| 127 | + spec |
| 128 | + ); |
| 129 | + |
| 130 | + expect(result).toEqual({ |
| 131 | + type: 'object', |
| 132 | + properties: { |
| 133 | + merged: { |
| 134 | + type: 'boolean', |
| 135 | + }, |
| 136 | + }, |
| 137 | + required: ['merged'], |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should handle integer attribute', () => { |
| 142 | + const result = typeParser.parseResponseSchema( |
| 143 | + 'Hash with a single integer attribute `total`', |
| 144 | + spec |
| 145 | + ); |
| 146 | + |
| 147 | + expect(result).toEqual({ |
| 148 | + type: 'object', |
| 149 | + properties: { |
| 150 | + total: { |
| 151 | + type: 'integer', |
| 152 | + }, |
| 153 | + }, |
| 154 | + required: ['total'], |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should handle string attribute', () => { |
| 159 | + const result = typeParser.parseResponseSchema( |
| 160 | + 'Hash with a single string attribute `name`', |
| 161 | + spec |
| 162 | + ); |
| 163 | + |
| 164 | + expect(result).toEqual({ |
| 165 | + type: 'object', |
| 166 | + properties: { |
| 167 | + name: { |
| 168 | + type: 'string', |
| 169 | + }, |
| 170 | + }, |
| 171 | + required: ['name'], |
| 172 | + }); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('Case insensitivity', () => { |
| 177 | + it('should handle case variations in hash pattern', () => { |
| 178 | + const result = typeParser.parseResponseSchema( |
| 179 | + 'hash with a single key of `count`', |
| 180 | + spec |
| 181 | + ); |
| 182 | + |
| 183 | + expect(result).toEqual({ |
| 184 | + type: 'object', |
| 185 | + properties: { |
| 186 | + count: { |
| 187 | + type: 'integer', |
| 188 | + }, |
| 189 | + }, |
| 190 | + required: ['count'], |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should handle case variations in entity pattern', () => { |
| 195 | + const result = typeParser.parseResponseSchema( |
| 196 | + 'HASH WITH A SINGLE KEY OF `async_refresh` WITH VALUE OF [AsyncRefresh]', |
| 197 | + spec |
| 198 | + ); |
| 199 | + |
| 200 | + expect(result).toEqual({ |
| 201 | + type: 'object', |
| 202 | + properties: { |
| 203 | + async_refresh: { |
| 204 | + $ref: '#/components/schemas/AsyncRefresh', |
| 205 | + }, |
| 206 | + }, |
| 207 | + required: ['async_refresh'], |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should handle case variations in attribute pattern', () => { |
| 212 | + const result = typeParser.parseResponseSchema( |
| 213 | + 'HASH WITH A SINGLE BOOLEAN ATTRIBUTE `enabled`', |
| 214 | + spec |
| 215 | + ); |
| 216 | + |
| 217 | + expect(result).toEqual({ |
| 218 | + type: 'object', |
| 219 | + properties: { |
| 220 | + enabled: { |
| 221 | + type: 'boolean', |
| 222 | + }, |
| 223 | + }, |
| 224 | + required: ['enabled'], |
| 225 | + }); |
| 226 | + }); |
| 227 | + }); |
| 228 | +}); |
0 commit comments