|
| 1 | +import { EntityConverter } from '../../generators/EntityConverter'; |
| 2 | +import { TypeParser } from '../../generators/TypeParser'; |
| 3 | +import { UtilityHelpers } from '../../generators/UtilityHelpers'; |
| 4 | +import { EntityClass } from '../../interfaces/EntityClass'; |
| 5 | +import { OpenAPISpec } from '../../interfaces/OpenAPISchema'; |
| 6 | + |
| 7 | +describe('EntityConverter - Nullable Date Format Handling', () => { |
| 8 | + let entityConverter: EntityConverter; |
| 9 | + let typeParser: TypeParser; |
| 10 | + let utilityHelpers: UtilityHelpers; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + utilityHelpers = new UtilityHelpers(); |
| 14 | + typeParser = new TypeParser(utilityHelpers); |
| 15 | + entityConverter = new EntityConverter(typeParser, utilityHelpers); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should preserve date format for nullable date fields', () => { |
| 19 | + const entities: EntityClass[] = [ |
| 20 | + { |
| 21 | + name: 'TestEntity', |
| 22 | + description: 'Test entity with nullable date fields', |
| 23 | + attributes: [ |
| 24 | + { |
| 25 | + name: 'last_status_at', |
| 26 | + type: 'String ([Date](/api/datetime-format#date))', |
| 27 | + description: 'When the most recent status was posted.', |
| 28 | + optional: false, |
| 29 | + nullable: true, |
| 30 | + deprecated: false, |
| 31 | + enumValues: [], |
| 32 | + versions: ['1.0.0'], |
| 33 | + }, |
| 34 | + { |
| 35 | + name: 'regular_nullable_string', |
| 36 | + type: 'String', |
| 37 | + description: 'A regular nullable string', |
| 38 | + optional: false, |
| 39 | + nullable: true, |
| 40 | + deprecated: false, |
| 41 | + enumValues: [], |
| 42 | + versions: ['1.0.0'], |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + ]; |
| 47 | + |
| 48 | + const spec: OpenAPISpec = { |
| 49 | + openapi: '3.0.0', |
| 50 | + info: { title: 'Test', version: '1.0.0' }, |
| 51 | + paths: {}, |
| 52 | + }; |
| 53 | + |
| 54 | + entityConverter.convertEntities(entities, spec); |
| 55 | + |
| 56 | + const schema = spec.components?.schemas?.['TestEntity']; |
| 57 | + expect(schema).toBeDefined(); |
| 58 | + expect(schema?.properties).toBeDefined(); |
| 59 | + |
| 60 | + // Nullable date field should have date format preserved |
| 61 | + const lastStatusAtProperty = schema?.properties?.['last_status_at']; |
| 62 | + expect(lastStatusAtProperty?.type).toEqual(['string', 'null']); |
| 63 | + expect(lastStatusAtProperty?.format).toBe('date'); |
| 64 | + |
| 65 | + // Regular nullable string should NOT have format |
| 66 | + const regularNullableProperty = |
| 67 | + schema?.properties?.['regular_nullable_string']; |
| 68 | + expect(regularNullableProperty?.type).toEqual(['string', 'null']); |
| 69 | + expect(regularNullableProperty?.format).toBeUndefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should preserve datetime format for nullable datetime fields', () => { |
| 73 | + const entities: EntityClass[] = [ |
| 74 | + { |
| 75 | + name: 'TestEntity', |
| 76 | + description: 'Test entity with nullable datetime fields', |
| 77 | + attributes: [ |
| 78 | + { |
| 79 | + name: 'created_at', |
| 80 | + type: 'String ([Datetime](/api/datetime-format#datetime))', |
| 81 | + description: 'When the entity was created.', |
| 82 | + optional: false, |
| 83 | + nullable: true, |
| 84 | + deprecated: false, |
| 85 | + enumValues: [], |
| 86 | + versions: ['1.0.0'], |
| 87 | + }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + ]; |
| 91 | + |
| 92 | + const spec: OpenAPISpec = { |
| 93 | + openapi: '3.0.0', |
| 94 | + info: { title: 'Test', version: '1.0.0' }, |
| 95 | + paths: {}, |
| 96 | + }; |
| 97 | + |
| 98 | + entityConverter.convertEntities(entities, spec); |
| 99 | + |
| 100 | + const schema = spec.components?.schemas?.['TestEntity']; |
| 101 | + expect(schema).toBeDefined(); |
| 102 | + |
| 103 | + // Nullable datetime field should have date-time format preserved |
| 104 | + const createdAtProperty = schema?.properties?.['created_at']; |
| 105 | + expect(createdAtProperty?.type).toEqual(['string', 'null']); |
| 106 | + expect(createdAtProperty?.format).toBe('date-time'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should preserve email format for nullable email fields', () => { |
| 110 | + const entities: EntityClass[] = [ |
| 111 | + { |
| 112 | + name: 'TestEntity', |
| 113 | + description: 'Test entity with nullable email fields', |
| 114 | + attributes: [ |
| 115 | + { |
| 116 | + name: 'email', |
| 117 | + type: 'String', |
| 118 | + description: 'The email address of the user.', |
| 119 | + optional: false, |
| 120 | + nullable: true, |
| 121 | + deprecated: false, |
| 122 | + enumValues: [], |
| 123 | + versions: ['1.0.0'], |
| 124 | + }, |
| 125 | + ], |
| 126 | + }, |
| 127 | + ]; |
| 128 | + |
| 129 | + const spec: OpenAPISpec = { |
| 130 | + openapi: '3.0.0', |
| 131 | + info: { title: 'Test', version: '1.0.0' }, |
| 132 | + paths: {}, |
| 133 | + }; |
| 134 | + |
| 135 | + entityConverter.convertEntities(entities, spec); |
| 136 | + |
| 137 | + const schema = spec.components?.schemas?.['TestEntity']; |
| 138 | + expect(schema).toBeDefined(); |
| 139 | + |
| 140 | + // Nullable email field should have email format preserved |
| 141 | + const emailProperty = schema?.properties?.['email']; |
| 142 | + expect(emailProperty?.type).toEqual(['string', 'null']); |
| 143 | + expect(emailProperty?.format).toBe('email'); |
| 144 | + }); |
| 145 | +}); |
0 commit comments