Skip to content

Commit f9d1f6c

Browse files
committed
✨ feat: add tests
1 parent d4b5a0d commit f9d1f6c

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/openapi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export const unwrapSchema = (
9797
return schema.toJSONSchema?.() ?? schema?.toJsonSchema?.()
9898
}
9999

100-
const convertEnumToOpenApi = (schema: any): any => {
100+
export const convertEnumToOpenApi = (schema: any): any => {
101101
if (!schema || typeof schema !== 'object') return schema
102102

103103
if (

test/openapi.test.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect } from 'bun:test'
2-
import { getPossiblePath } from '../src/openapi'
2+
import { Kind } from '@sinclair/typebox'
3+
import { convertEnumToOpenApi, getPossiblePath } from '../src/openapi'
34

45
describe('OpenAPI utilities', () => {
56
it('getPossiblePath', () => {
@@ -12,3 +13,62 @@ describe('OpenAPI utilities', () => {
1213
])
1314
})
1415
})
16+
17+
describe('convertEnumToOpenApi', () => {
18+
it('should convert enum schema to OpenAPI enum format', () => {
19+
const expectedSchema = {
20+
[Kind]: 'Union',
21+
anyOf: [
22+
{ const: 'male' },
23+
{ const: 'female' }
24+
]
25+
}
26+
27+
const result = convertEnumToOpenApi(expectedSchema)
28+
29+
expect(result).toEqual({
30+
type: 'string',
31+
enum: ['male', 'female']
32+
})
33+
})
34+
35+
it('should convert nested enums in object properties', () => {
36+
const expectedSchema = {
37+
type: 'object',
38+
properties: {
39+
name: { type: 'string' },
40+
gender: {
41+
[Kind]: 'Union',
42+
anyOf: [
43+
{ const: 'male' },
44+
{ const: 'female' }
45+
]
46+
}
47+
}
48+
}
49+
50+
const result = convertEnumToOpenApi(expectedSchema)
51+
52+
expect(result).toEqual({
53+
type: 'object',
54+
properties: {
55+
name: { type: 'string' },
56+
gender: {
57+
type: 'string',
58+
enum: ['male', 'female']
59+
}
60+
}
61+
})
62+
})
63+
64+
it('should return original schema if not enum', () => {
65+
const expectedSchema = {
66+
type: 'string',
67+
description: 'Regular string field'
68+
}
69+
70+
const result = convertEnumToOpenApi(expectedSchema)
71+
72+
expect(result).toEqual(expectedSchema)
73+
})
74+
})

0 commit comments

Comments
 (0)