|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { createTestContextSpec } from '../test-utils/context'; |
| 4 | +import type { OpenApiParameterObject, OpenApiReferenceObject } from '../types'; |
| 5 | +import { getParameters } from './parameters'; |
| 6 | + |
| 7 | +describe('getParameters', () => { |
| 8 | + it('resolves a header parameter referenced via #/components/parameters/* and surfaces its named import', () => { |
| 9 | + // Refs that target a slot under `#/components/<NAMED_COMPONENT_SECTIONS>` |
| 10 | + // have a matching `export type` emitted by `generateParameterDefinition`, |
| 11 | + // so the import must flow through to downstream getters (e.g. |
| 12 | + // `getQueryParams` renders the header type as that named import). |
| 13 | + const sharedHeader: OpenApiParameterObject = { |
| 14 | + name: 'Content-Type', |
| 15 | + in: 'header', |
| 16 | + schema: { type: 'string' }, |
| 17 | + }; |
| 18 | + const context = createTestContextSpec({ |
| 19 | + spec: { |
| 20 | + components: { parameters: { ContentTypeHeader: sharedHeader } }, |
| 21 | + }, |
| 22 | + }); |
| 23 | + |
| 24 | + const ref: OpenApiReferenceObject = { |
| 25 | + $ref: '#/components/parameters/ContentTypeHeader', |
| 26 | + }; |
| 27 | + |
| 28 | + const result = getParameters({ parameters: [ref], context }); |
| 29 | + |
| 30 | + expect(result.header).toHaveLength(1); |
| 31 | + expect(result.header[0].parameter.name).toBe('Content-Type'); |
| 32 | + expect(result.header[0].imports).toEqual([ |
| 33 | + { name: 'ContentTypeHeader', schemaName: 'ContentTypeHeader' }, |
| 34 | + ]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('drops imports when a header parameter $ref targets a non-component slot (issue #1879)', () => { |
| 38 | + // Repro for #1879: JSON Pointer refs into another path's `parameters` array |
| 39 | + // (`#/paths/~1requestA/post/parameters/0`) resolve to a synthesized name |
| 40 | + // like `N0` that has no corresponding `export type`. Surfacing that import |
| 41 | + // would produce a dangling reference downstream — drop it so consumers |
| 42 | + // inline the resolved parameter's `schema` instead. Mirrors the #398 fix |
| 43 | + // applied to schema refs in `resolvers/value.ts`. |
| 44 | + const requestAHeader: OpenApiParameterObject = { |
| 45 | + name: 'Content-Type', |
| 46 | + in: 'header', |
| 47 | + schema: { type: 'string' }, |
| 48 | + }; |
| 49 | + const context = createTestContextSpec({ |
| 50 | + spec: { |
| 51 | + paths: { |
| 52 | + '/requestA': { |
| 53 | + post: { parameters: [requestAHeader], responses: {} }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + const ref: OpenApiReferenceObject = { |
| 60 | + $ref: '#/paths/~1requestA/post/parameters/0', |
| 61 | + }; |
| 62 | + |
| 63 | + const result = getParameters({ parameters: [ref], context }); |
| 64 | + |
| 65 | + expect(result.header).toHaveLength(1); |
| 66 | + expect(result.header[0].parameter.name).toBe('Content-Type'); |
| 67 | + // The resolver synthesizes `{ name: 'N0', schemaName: '0' }` from the |
| 68 | + // numeric path segment. Without the fix this leaks downstream and types |
| 69 | + // the header as `N0` while importing it from `./n0`. |
| 70 | + expect(result.header[0].imports).toEqual([]); |
| 71 | + }); |
| 72 | +}); |
0 commit comments