|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import type { |
| 4 | + ContextSpec, |
| 5 | + OpenApiDocument, |
| 6 | + OpenApiReferenceObject, |
| 7 | +} from '../types'; |
| 8 | +import { resolveValue } from './value'; |
| 9 | + |
| 10 | +function createContext(spec: OpenApiDocument): ContextSpec { |
| 11 | + return { |
| 12 | + target: 'core-test', |
| 13 | + workspace: '/tmp', |
| 14 | + spec, |
| 15 | + output: { |
| 16 | + override: { |
| 17 | + components: { |
| 18 | + schemas: { suffix: '' }, |
| 19 | + }, |
| 20 | + }, |
| 21 | + }, |
| 22 | + } as ContextSpec; |
| 23 | +} |
| 24 | + |
| 25 | +describe('resolveValue', () => { |
| 26 | + it('emits a named import for a component schema ref', () => { |
| 27 | + const context = createContext({ |
| 28 | + openapi: '3.1.0', |
| 29 | + components: { |
| 30 | + schemas: { |
| 31 | + Pet: { |
| 32 | + type: 'object', |
| 33 | + properties: { id: { type: 'string' } }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + const result = resolveValue({ |
| 40 | + schema: { $ref: '#/components/schemas/Pet' } as OpenApiReferenceObject, |
| 41 | + context, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(result.value).toBe('Pet'); |
| 45 | + expect(result.imports[0]).toMatchObject({ |
| 46 | + name: 'Pet', |
| 47 | + schemaName: 'Pet', |
| 48 | + }); |
| 49 | + expect(result.isRef).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + // Regression for issue #398: a $ref like `#/paths/.../schema` (emitted by |
| 53 | + // JSON-Schema-Ref-Parser bundle()) resolves to an inline schema with no |
| 54 | + // corresponding `export type`. orval previously generated a broken |
| 55 | + // `import { Schema } from './model'` referencing an undeclared type. |
| 56 | + it('inlines a path-based ref instead of emitting a broken import', () => { |
| 57 | + const context = createContext({ |
| 58 | + openapi: '3.0.3', |
| 59 | + paths: { |
| 60 | + '/{id}': { |
| 61 | + get: { |
| 62 | + parameters: [ |
| 63 | + { |
| 64 | + in: 'path', |
| 65 | + name: 'id', |
| 66 | + required: true, |
| 67 | + schema: { type: 'string' }, |
| 68 | + }, |
| 69 | + ], |
| 70 | + responses: { |
| 71 | + '200': { |
| 72 | + description: 'OK', |
| 73 | + content: { |
| 74 | + 'application/json': { |
| 75 | + schema: { |
| 76 | + $ref: '#/paths/~1%7Bid%7D/get/parameters/0/schema', |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } as unknown as OpenApiDocument); |
| 86 | + |
| 87 | + const result = resolveValue({ |
| 88 | + schema: { |
| 89 | + $ref: '#/paths/~1%7Bid%7D/get/parameters/0/schema', |
| 90 | + } as OpenApiReferenceObject, |
| 91 | + context, |
| 92 | + }); |
| 93 | + |
| 94 | + expect(result.value).toBe('string'); |
| 95 | + expect(result.imports).toHaveLength(0); |
| 96 | + expect(result.isRef).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + // Defensive guard: a self-referential path-ref would otherwise recurse via |
| 100 | + // getScalar -> resolveValue forever, since the named-ref cycle tracker keys |
| 101 | + // off `resolvedImport.name` and not the ref string. |
| 102 | + it('breaks cycles on self-referential path-based refs', () => { |
| 103 | + const selfRef = |
| 104 | + '#/paths/~1self/get/responses/200/content/application~1json/schema'; |
| 105 | + const context = createContext({ |
| 106 | + openapi: '3.0.3', |
| 107 | + paths: { |
| 108 | + '/self': { |
| 109 | + get: { |
| 110 | + responses: { |
| 111 | + '200': { |
| 112 | + description: 'OK', |
| 113 | + content: { |
| 114 | + 'application/json': { |
| 115 | + schema: { |
| 116 | + type: 'object', |
| 117 | + properties: { |
| 118 | + child: { $ref: selfRef }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + } as unknown as OpenApiDocument); |
| 129 | + |
| 130 | + expect(() => |
| 131 | + resolveValue({ |
| 132 | + schema: { $ref: selfRef } as OpenApiReferenceObject, |
| 133 | + context, |
| 134 | + }), |
| 135 | + ).not.toThrow(); |
| 136 | + }); |
| 137 | +}); |
0 commit comments