From ce1d05deeef7c4221b33476310ee7d32bbc8990c Mon Sep 17 00:00:00 2001 From: Drew Powers Date: Wed, 16 Nov 2022 09:36:42 -0700 Subject: [PATCH] Fix param --- src/transform/operation-object.ts | 5 ++++- src/types.ts | 2 +- test/path-item-object.test.ts | 7 +++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/transform/operation-object.ts b/src/transform/operation-object.ts index 1ac71c813..8a0190a23 100644 --- a/src/transform/operation-object.ts +++ b/src/transform/operation-object.ts @@ -71,7 +71,10 @@ export default function transformOperationObject( else if (p.$ref) { const parts = parseTSIndex(p.$ref); const paramI = parts.indexOf("parameters"); - if (paramI === -1 || parts[paramI + 1] !== paramIn || !parts[paramI + 2]) continue; + // note: parameters can share names in different parts of the `in` key. `partsParamIn` makes sure we’re not + // referencing the wrong parameter with the same name but a different `in` + const partsParamIn = parts.find((p) => p === "query" || p === "header" || p === "path" || p === "cookie"); + if (paramI === -1 || (partsParamIn && partsParamIn !== paramIn)) continue; const key = parts.pop() as string; const index = makeTSIndex(parts); if (!refs[index]) refs[index] = [key]; diff --git a/src/types.ts b/src/types.ts index e0a85557d..dc8944641 100644 --- a/src/types.ts +++ b/src/types.ts @@ -238,7 +238,7 @@ export interface ParameterObject extends Extensable { /** Determines whether the parameter value SHOULD allow reserved characters, as defined by [RFC3986] `:/?#[]@!$&'()*+,;=` to be included without percent-encoding. This property only applies to parameters with an `in` value of `query`. The default value is `false`. */ allowReserved?: boolean; /** The schema defining the type used for the parameter. */ - schema?: SchemaObject; + schema?: SchemaObject | ReferenceObject; /** Example of the parameter’s potential value. */ example?: any; /** Examples of the parameter’s potential value. */ diff --git a/test/path-item-object.test.ts b/test/path-item-object.test.ts index 2a2705189..ccec9f393 100644 --- a/test/path-item-object.test.ts +++ b/test/path-item-object.test.ts @@ -52,6 +52,7 @@ describe("Path Item Object", () => { 404: { $ref: 'components["responses"]["NotFound"]' }, }, }, + parameters: [{ $ref: 'components["parameters"]["PerPage"]' }, { $ref: 'components["parameters"]["PageNumber"]' }], }; const generated = transformPathItemObject(schema, options); expect(generated).toBe(`{ @@ -79,6 +80,12 @@ describe("Path Item Object", () => { 404: components["responses"]["NotFound"]; }; }; + parameters?: { + query?: Pick, "PerPage" | "PageNumber">; + header?: Pick, "PerPage" | "PageNumber">; + path?: Pick; + cookie?: Pick, "PerPage" | "PageNumber">; + }; }`); });