Skip to content

Commit 9d4138f

Browse files
committed
🧹 chore: refactor enumToOpenAPI
1 parent 303be10 commit 9d4138f

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, it, expect } from 'bun:test'
2+
3+
import { getPossiblePath } from '../../src/openapi'
4+
5+
describe('OpenAPI > getPossiblePath', () => {
6+
it('remain the same if no optional', () => {
7+
expect(getPossiblePath('/user/:user/name/:name')).toEqual([
8+
'/user/:user/name/:name'
9+
])
10+
})
11+
12+
it('list all possibility from optional', () => {
13+
expect(getPossiblePath('/user/:user?/name/:name?')).toEqual([
14+
'/user/:user/name/:name',
15+
'/user/name/:name',
16+
'/user/name',
17+
'/user/:user/name',
18+
'/user/name'
19+
])
20+
})
21+
})

0 commit comments

Comments
 (0)