|
| 1 | +import { describe, it, expect } from 'bun:test' |
| 2 | +import { Kind } from '@sinclair/typebox' |
| 3 | + |
| 4 | +import { enumToOpenApi } from '../../src/openapi' |
| 5 | + |
| 6 | +describe('OpenAPI > enumToOpenAPI', () => { |
| 7 | + it('should convert enum schema to OpenAPI enum format', () => { |
| 8 | + const expectedSchema = { |
| 9 | + [Kind]: 'Union', |
| 10 | + anyOf: [{ const: 'male' }, { const: 'female' }] |
| 11 | + } |
| 12 | + |
| 13 | + const result = enumToOpenApi(expectedSchema as any) |
| 14 | + |
| 15 | + expect(result).toEqual({ |
| 16 | + type: 'string', |
| 17 | + enum: ['male', 'female'] |
| 18 | + }) |
| 19 | + }) |
| 20 | + |
| 21 | + it('should convert nested enums in object properties', () => { |
| 22 | + const expectedSchema = { |
| 23 | + type: 'object', |
| 24 | + properties: { |
| 25 | + name: { type: 'string' }, |
| 26 | + gender: { |
| 27 | + [Kind]: 'Union', |
| 28 | + anyOf: [{ const: 'male' }, { const: 'female' }] |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const result = enumToOpenApi(expectedSchema as any) |
| 34 | + |
| 35 | + expect(result).toEqual({ |
| 36 | + type: 'object', |
| 37 | + properties: { |
| 38 | + name: { type: 'string' }, |
| 39 | + gender: { |
| 40 | + type: 'string', |
| 41 | + enum: ['male', 'female'] |
| 42 | + } |
| 43 | + } |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should return original schema if not enum', () => { |
| 48 | + const expectedSchema = { |
| 49 | + type: 'string', |
| 50 | + description: 'Regular string field' |
| 51 | + } |
| 52 | + |
| 53 | + const result = enumToOpenApi(expectedSchema as any) |
| 54 | + |
| 55 | + expect(result).toEqual(expectedSchema) |
| 56 | + }) |
| 57 | +}) |
0 commit comments