|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { summarizeSchema } from '../lib/summarize-schema' |
| 3 | + |
| 4 | +describe('summarizeSchema — OAS 3.1 nullable handling', () => { |
| 5 | + // ── Bug #1 ────────────────────────────────────────────────────────────────── |
| 6 | + // renderProperties: type: ["array", "null"] on a property |
| 7 | + |
| 8 | + it('renders nullable array property (primitive items) with "or null"', () => { |
| 9 | + const schema = { |
| 10 | + type: 'object' as const, |
| 11 | + properties: { |
| 12 | + tags: { |
| 13 | + type: ['array', 'null'], |
| 14 | + items: { type: 'string' }, |
| 15 | + }, |
| 16 | + }, |
| 17 | + } |
| 18 | + const result = summarizeSchema(schema) |
| 19 | + expect(result).toContain('or null') |
| 20 | + expect(result).toContain('array of') |
| 21 | + }) |
| 22 | + |
| 23 | + it('renders nullable array property (object items with title) with "or null"', () => { |
| 24 | + const schema = { |
| 25 | + type: 'object' as const, |
| 26 | + properties: { |
| 27 | + items: { |
| 28 | + type: ['array', 'null'], |
| 29 | + items: { |
| 30 | + type: 'object', |
| 31 | + title: 'Widget', |
| 32 | + properties: { id: { type: 'string' } }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + const result = summarizeSchema(schema) |
| 38 | + expect(result).toContain('array of `Widget` or null') |
| 39 | + }) |
| 40 | + |
| 41 | + it('renders nullable array property (object items without title) with "or null"', () => { |
| 42 | + const schema = { |
| 43 | + type: 'object' as const, |
| 44 | + properties: { |
| 45 | + entries: { |
| 46 | + type: ['array', 'null'], |
| 47 | + items: { |
| 48 | + type: 'object', |
| 49 | + properties: { name: { type: 'string' } }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + const result = summarizeSchema(schema) |
| 55 | + expect(result).toContain('array of objects or null') |
| 56 | + }) |
| 57 | + |
| 58 | + it('renders non-nullable array property without "or null"', () => { |
| 59 | + const schema = { |
| 60 | + type: 'object' as const, |
| 61 | + properties: { |
| 62 | + tags: { |
| 63 | + type: 'array', |
| 64 | + items: { type: 'string' }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | + const result = summarizeSchema(schema) |
| 69 | + expect(result).not.toContain('or null') |
| 70 | + expect(result).toContain('array of') |
| 71 | + }) |
| 72 | + |
| 73 | + it('renders scalar type: ["string", "null"] property as "string or null"', () => { |
| 74 | + const schema = { |
| 75 | + type: 'object' as const, |
| 76 | + properties: { |
| 77 | + description: { |
| 78 | + type: ['string', 'null'], |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + const result = summarizeSchema(schema) |
| 83 | + expect(result).toContain('string or null') |
| 84 | + }) |
| 85 | + |
| 86 | + // ── Bug #2 ────────────────────────────────────────────────────────────────── |
| 87 | + // summarizeSchema: type: ["array", "null"] at the top level |
| 88 | + |
| 89 | + it('renders top-level type: ["array", "null"] with primitive items as "or null"', () => { |
| 90 | + const schema = { |
| 91 | + type: ['array', 'null'], |
| 92 | + items: { type: 'string' }, |
| 93 | + } |
| 94 | + const result = summarizeSchema(schema) |
| 95 | + expect(result).toContain('or null') |
| 96 | + expect(result).toContain('Array') |
| 97 | + }) |
| 98 | + |
| 99 | + it('renders top-level type: ["array", "null"] with object items as "or null"', () => { |
| 100 | + const schema = { |
| 101 | + type: ['array', 'null'], |
| 102 | + items: { |
| 103 | + type: 'object', |
| 104 | + title: 'Repo', |
| 105 | + properties: { id: { type: 'integer' } }, |
| 106 | + }, |
| 107 | + } |
| 108 | + const result = summarizeSchema(schema) |
| 109 | + expect(result).toContain('or null') |
| 110 | + expect(result).toContain('`Repo`') |
| 111 | + }) |
| 112 | + |
| 113 | + it('renders top-level type: ["array", "null"] with composition items as "or null"', () => { |
| 114 | + const schema = { |
| 115 | + type: ['array', 'null'], |
| 116 | + items: { |
| 117 | + oneOf: [ |
| 118 | + { title: 'TypeA', type: 'object' }, |
| 119 | + { title: 'TypeB', type: 'object' }, |
| 120 | + ], |
| 121 | + }, |
| 122 | + } |
| 123 | + const result = summarizeSchema(schema) |
| 124 | + expect(result).toContain('or null') |
| 125 | + }) |
| 126 | + |
| 127 | + it('renders top-level plain array (not nullable) without "or null"', () => { |
| 128 | + const schema = { |
| 129 | + type: 'array', |
| 130 | + items: { type: 'string' }, |
| 131 | + } |
| 132 | + const result = summarizeSchema(schema) |
| 133 | + expect(result).not.toContain('or null') |
| 134 | + }) |
| 135 | + |
| 136 | + // ── Existing branches still work ───────────────────────────────────────── |
| 137 | + it('handles top-level anyOf', () => { |
| 138 | + const schema = { |
| 139 | + anyOf: [ |
| 140 | + { |
| 141 | + type: 'object', |
| 142 | + title: 'Option A', |
| 143 | + properties: { x: { type: 'string' } } as Record<string, object>, |
| 144 | + }, |
| 145 | + { |
| 146 | + type: 'object', |
| 147 | + title: 'Option B', |
| 148 | + properties: { y: { type: 'number' } } as Record<string, object>, |
| 149 | + }, |
| 150 | + ], |
| 151 | + } |
| 152 | + const result = summarizeSchema(schema) |
| 153 | + expect(result).toContain('any of') |
| 154 | + expect(result).toContain('Option A') |
| 155 | + expect(result).toContain('Option B') |
| 156 | + }) |
| 157 | + |
| 158 | + it('handles top-level oneOf', () => { |
| 159 | + const schema = { |
| 160 | + oneOf: [ |
| 161 | + { title: 'Foo', type: 'object' }, |
| 162 | + { title: 'Bar', type: 'object' }, |
| 163 | + ], |
| 164 | + } |
| 165 | + const result = summarizeSchema(schema) |
| 166 | + expect(result).toContain('one of') |
| 167 | + }) |
| 168 | + |
| 169 | + it('handles top-level allOf', () => { |
| 170 | + const schema = { |
| 171 | + allOf: [ |
| 172 | + { title: 'Base', type: 'object' }, |
| 173 | + { title: 'Extension', type: 'object' }, |
| 174 | + ], |
| 175 | + } |
| 176 | + const result = summarizeSchema(schema) |
| 177 | + expect(result).toContain('all of') |
| 178 | + }) |
| 179 | + |
| 180 | + it('handles property-level anyOf', () => { |
| 181 | + const schema = { |
| 182 | + type: 'object' as const, |
| 183 | + properties: { |
| 184 | + value: { |
| 185 | + anyOf: [{ type: 'string' }, { type: 'number' }], |
| 186 | + }, |
| 187 | + }, |
| 188 | + } |
| 189 | + const result = summarizeSchema(schema) |
| 190 | + expect(result).toContain('any of') |
| 191 | + }) |
| 192 | + |
| 193 | + it('returns empty string for null/non-object input', () => { |
| 194 | + // @ts-expect-error testing runtime behavior |
| 195 | + expect(summarizeSchema(null)).toBe('') |
| 196 | + // @ts-expect-error testing runtime behavior |
| 197 | + expect(summarizeSchema('not an object')).toBe('') |
| 198 | + }) |
| 199 | +}) |
0 commit comments