|
1 | 1 | import { type ParsedSchema, type ResolvedSchema } from '@/utils/schema'; |
| 2 | +import type { ProcessedDocument } from '@/utils/process-document'; |
2 | 3 |
|
3 | | -export function schemaToString(schema: ResolvedSchema, isRoot = true): string { |
4 | | - if (schema === true) return 'any'; |
5 | | - else if (schema === false) return 'never'; |
| 4 | +export function schemaToString( |
| 5 | + value: ResolvedSchema, |
| 6 | + ctx?: ProcessedDocument, |
| 7 | +): string { |
| 8 | + function run(schema: ResolvedSchema, isRoot: boolean): string { |
| 9 | + if (schema === true) return 'any'; |
| 10 | + else if (schema === false) return 'never'; |
6 | 11 |
|
7 | | - if (isNullable(schema) && isRoot) { |
8 | | - const type = schemaToString(schema, false); |
| 12 | + if (isNullable(schema) && isRoot) { |
| 13 | + const type = run(schema, false); |
9 | 14 |
|
10 | | - // null if schema only contains `nullable` |
11 | | - return type === 'unknown' ? 'null' : `${type} | null`; |
12 | | - } |
| 15 | + // null if schema only contains `nullable` |
| 16 | + return type === 'unknown' ? 'null' : `${type} | null`; |
| 17 | + } |
13 | 18 |
|
14 | | - if (schema.title) return schema.title; |
15 | | - |
16 | | - if (Array.isArray(schema.type)) { |
17 | | - return schema.type |
18 | | - .map((type) => |
19 | | - schemaToString( |
20 | | - { |
21 | | - ...schema, |
22 | | - type, |
23 | | - }, |
24 | | - false, |
25 | | - ), |
26 | | - ) |
27 | | - .filter((v) => v !== 'unknown' && v !== 'null') |
28 | | - .join(' | '); |
29 | | - } |
| 19 | + if (schema.title) return schema.title; |
| 20 | + const referenceName = ctx?.dereferenceMap.get(schema); |
| 21 | + if (referenceName) return referenceName.split('/').at(-1)!; |
30 | 22 |
|
31 | | - if (schema.type === 'array') |
32 | | - return `array<${schema.items ? schemaToString(schema.items) : 'unknown'}>`; |
| 23 | + if (Array.isArray(schema.type)) { |
| 24 | + return schema.type |
| 25 | + .map((type, _, originalType) => { |
| 26 | + schema.type = type; |
| 27 | + const str = run(schema, false); |
| 28 | + schema.type = originalType; |
33 | 29 |
|
34 | | - if (schema.oneOf) { |
35 | | - return schema.oneOf |
36 | | - .map((one) => schemaToString(one, false)) |
37 | | - .filter((v) => v !== 'unknown' && v !== 'null') |
38 | | - .join(' | '); |
39 | | - } |
| 30 | + return str; |
| 31 | + }) |
| 32 | + .filter((v) => v !== 'unknown' && v !== 'null') |
| 33 | + .join(' | '); |
| 34 | + } |
40 | 35 |
|
41 | | - const combinedOf = schema.anyOf ?? schema.allOf; |
42 | | - if (combinedOf) { |
43 | | - return combinedOf |
44 | | - .map((one) => schemaToString(one, false)) |
45 | | - .filter((v) => v !== 'unknown' && v !== 'null') |
46 | | - .join(' & '); |
47 | | - } |
| 36 | + if (schema.type === 'array') |
| 37 | + return `array<${schema.items ? run(schema.items, true) : 'unknown'}>`; |
48 | 38 |
|
49 | | - if (schema.not) return `not ${schemaToString(schema.not, false)}`; |
50 | | - if (schema.type === 'string' && schema.format === 'binary') return 'file'; |
| 39 | + if (schema.oneOf) { |
| 40 | + return schema.oneOf |
| 41 | + .map((one) => run(one, false)) |
| 42 | + .filter((v) => v !== 'unknown' && v !== 'null') |
| 43 | + .join(' | '); |
| 44 | + } |
51 | 45 |
|
52 | | - if (schema.type && Array.isArray(schema.type)) { |
53 | | - return schema.type.filter((v) => v !== 'null').join(' | '); |
54 | | - } |
| 46 | + const combinedOf = schema.anyOf ?? schema.allOf; |
| 47 | + if (combinedOf) { |
| 48 | + return combinedOf |
| 49 | + .map((one) => run(one, false)) |
| 50 | + .filter((v) => v !== 'unknown' && v !== 'null') |
| 51 | + .join(' & '); |
| 52 | + } |
| 53 | + |
| 54 | + if (schema.not) return `not ${run(schema.not, false)}`; |
| 55 | + if (schema.type === 'string' && schema.format === 'binary') return 'file'; |
| 56 | + |
| 57 | + if (schema.type && Array.isArray(schema.type)) { |
| 58 | + return schema.type.filter((v) => v !== 'null').join(' | '); |
| 59 | + } |
| 60 | + |
| 61 | + if (schema.type) { |
| 62 | + return schema.type as string; |
| 63 | + } |
55 | 64 |
|
56 | | - if (schema.type) { |
57 | | - return schema.type as string; |
| 65 | + return 'unknown'; |
58 | 66 | } |
59 | 67 |
|
60 | | - return 'unknown'; |
| 68 | + return run(value, true); |
61 | 69 | } |
62 | 70 |
|
63 | 71 | function isNullable(schema: ParsedSchema): boolean { |
|
0 commit comments