|
| 1 | +import { describe, it, expect } from 'bun:test' |
| 2 | +import { Elysia, t } from 'elysia' |
| 3 | + |
| 4 | +import { toOpenAPISchema } from '../../src/openapi' |
| 5 | + |
| 6 | +const serializable = ( |
| 7 | + a: Record<string, unknown> | undefined |
| 8 | +): Record<string, unknown> | undefined => JSON.parse(JSON.stringify(a)) |
| 9 | + |
| 10 | +describe('OpenAPI > references', () => { |
| 11 | + it('use references when schema is not available', () => { |
| 12 | + const app = new Elysia().get('/', () => {}) |
| 13 | + |
| 14 | + const schema = toOpenAPISchema(app, undefined, { |
| 15 | + '/': { |
| 16 | + get: { |
| 17 | + params: {} as any, |
| 18 | + query: {} as any, |
| 19 | + headers: {} as any, |
| 20 | + body: {} as any, |
| 21 | + response: { |
| 22 | + 200: t.Object({ |
| 23 | + name: t.Literal('lilith') |
| 24 | + }) |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + }) |
| 29 | + |
| 30 | + expect(serializable(schema)).toEqual({ |
| 31 | + components: { |
| 32 | + schemas: {} |
| 33 | + }, |
| 34 | + paths: { |
| 35 | + '/': { |
| 36 | + get: { |
| 37 | + operationId: 'getIndex', |
| 38 | + responses: { |
| 39 | + '200': { |
| 40 | + content: { |
| 41 | + 'application/json': { |
| 42 | + schema: { |
| 43 | + properties: { |
| 44 | + name: { |
| 45 | + const: 'lilith', |
| 46 | + type: 'string' |
| 47 | + } |
| 48 | + }, |
| 49 | + required: ['name'], |
| 50 | + type: 'object' |
| 51 | + } |
| 52 | + } |
| 53 | + }, |
| 54 | + description: 'Response for status 200' |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('prefers schema over definition', () => { |
| 64 | + const app = new Elysia().get('/', () => ({ name: 'fouco' }) as const, { |
| 65 | + query: t.Object({ |
| 66 | + id: t.Number() |
| 67 | + }), |
| 68 | + response: t.Object({ |
| 69 | + name: t.Literal('fouco') |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + const schema = toOpenAPISchema(app, undefined, { |
| 74 | + '/': { |
| 75 | + get: { |
| 76 | + params: {} as any, |
| 77 | + query: {} as any, |
| 78 | + headers: {} as any, |
| 79 | + body: {} as any, |
| 80 | + response: { |
| 81 | + 200: t.Object({ |
| 82 | + name: t.Literal('lilith') |
| 83 | + }) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + expect(serializable(schema)).toEqual({ |
| 90 | + components: { |
| 91 | + schemas: {} |
| 92 | + }, |
| 93 | + paths: { |
| 94 | + '/': { |
| 95 | + get: { |
| 96 | + operationId: 'getIndex', |
| 97 | + parameters: [ |
| 98 | + { |
| 99 | + in: 'query', |
| 100 | + name: 'id', |
| 101 | + required: true, |
| 102 | + schema: { |
| 103 | + type: 'number' |
| 104 | + } |
| 105 | + } |
| 106 | + ], |
| 107 | + responses: { |
| 108 | + '200': { |
| 109 | + content: { |
| 110 | + 'application/json': { |
| 111 | + schema: { |
| 112 | + properties: { |
| 113 | + name: { |
| 114 | + const: 'fouco', |
| 115 | + type: 'string' |
| 116 | + } |
| 117 | + }, |
| 118 | + required: ['name'], |
| 119 | + type: 'object' |
| 120 | + } |
| 121 | + } |
| 122 | + }, |
| 123 | + description: 'Response for status 200' |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + it('use multiple references', () => { |
| 133 | + const app = new Elysia().get('/', () => {}) |
| 134 | + |
| 135 | + const schema = toOpenAPISchema(app, undefined, [ |
| 136 | + { |
| 137 | + '/': { |
| 138 | + get: { |
| 139 | + params: {} as any, |
| 140 | + query: {} as any, |
| 141 | + headers: {} as any, |
| 142 | + body: {} as any, |
| 143 | + response: { |
| 144 | + 200: t.Object({ |
| 145 | + name: t.Literal('lilith') |
| 146 | + }) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + }, |
| 151 | + { |
| 152 | + '/': { |
| 153 | + get: { |
| 154 | + params: {} as any, |
| 155 | + query: t.Object({ |
| 156 | + id: t.Number() |
| 157 | + }), |
| 158 | + headers: {} as any, |
| 159 | + body: {} as any, |
| 160 | + response: {} |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + ]) |
| 165 | + |
| 166 | + expect(serializable(schema)).toEqual({ |
| 167 | + components: { |
| 168 | + schemas: {} |
| 169 | + }, |
| 170 | + paths: { |
| 171 | + '/': { |
| 172 | + get: { |
| 173 | + operationId: 'getIndex', |
| 174 | + parameters: [ |
| 175 | + { |
| 176 | + in: 'query', |
| 177 | + name: 'id', |
| 178 | + required: true, |
| 179 | + schema: { |
| 180 | + type: 'number' |
| 181 | + } |
| 182 | + } |
| 183 | + ], |
| 184 | + responses: { |
| 185 | + '200': { |
| 186 | + content: { |
| 187 | + 'application/json': { |
| 188 | + schema: { |
| 189 | + properties: { |
| 190 | + name: { |
| 191 | + const: 'lilith', |
| 192 | + type: 'string' |
| 193 | + } |
| 194 | + }, |
| 195 | + required: ['name'], |
| 196 | + type: 'object' |
| 197 | + } |
| 198 | + } |
| 199 | + }, |
| 200 | + description: 'Response for status 200' |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + }) |
| 207 | + }) |
| 208 | +}) |
0 commit comments