From 813ecec7c4d2ac5d56e059dc52f0ab6344cbf50e Mon Sep 17 00:00:00 2001 From: Patrick McElhaney Date: Sat, 18 Jan 2025 12:28:14 -0500 Subject: [PATCH] update ParametersTypeCoder to follow references (#1105) --- .changeset/cool-emus-repeat.md | 5 +++++ .../parameters-type-coder.js | 18 ++++++++++-------- .../__snapshots__/generate.test.ts.snap | 6 +++--- .../parameters-type-coder.test.js.snap | 4 ++-- 4 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 .changeset/cool-emus-repeat.md diff --git a/.changeset/cool-emus-repeat.md b/.changeset/cool-emus-repeat.md new file mode 100644 index 00000000..de457668 --- /dev/null +++ b/.changeset/cool-emus-repeat.md @@ -0,0 +1,5 @@ +--- +"counterfact": patch +--- + +follow $refs in parameters (fixes #1105) diff --git a/src/typescript-generator/parameters-type-coder.js b/src/typescript-generator/parameters-type-coder.js index 7eabacce..bb135cc2 100644 --- a/src/typescript-generator/parameters-type-coder.js +++ b/src/typescript-generator/parameters-type-coder.js @@ -16,17 +16,19 @@ export class ParametersTypeCoder extends TypeCoder { writeCode(script) { const typeDefinitions = (this.requirement?.data ?? []) - .filter((parameter) => parameter.in === this.placement) - .map((parameter, index) => { - const requirement = this.requirement.get(String(index)); - - const { name, required } = parameter; + .map((_, index) => { + return this.requirement.get(index); + }) + .filter((parameter) => parameter.get("in").data === this.placement) + .map((parameter) => { + const name = parameter.get("name")?.data; + const required = parameter.get("required")?.data; const optionalFlag = required ? "" : "?"; - const schema = requirement.has("schema") - ? requirement.get("schema") - : requirement; + const schema = parameter.has("schema") + ? parameter.get("schema") + : parameter; return `"${name}"${optionalFlag}: ${new SchemaTypeCoder(schema).write( script, diff --git a/test/typescript-generator/__snapshots__/generate.test.ts.snap b/test/typescript-generator/__snapshots__/generate.test.ts.snap index 9c531892..a0b1f4e2 100644 --- a/test/typescript-generator/__snapshots__/generate.test.ts.snap +++ b/test/typescript-generator/__snapshots__/generate.test.ts.snap @@ -371,7 +371,7 @@ export type HTTP_GET = ( export type HTTP_POST = ( $: OmitValueWhenNever<{ - query: { name?: number; status?: string }; + query: { name?: string; status?: string }; path: { petId: number }; headers: never; body: never; @@ -398,7 +398,7 @@ export type HTTP_POST = ( export type HTTP_DELETE = ( $: OmitValueWhenNever<{ query: never; - path: { petId: string }; + path: { petId: number }; headers: { api_key?: string }; body: never; context: Context; @@ -446,7 +446,7 @@ import type { ApiResponse } from "../../../components/schemas/ApiResponse.js"; export type HTTP_POST = ( $: OmitValueWhenNever<{ - query: { additionalMetadata?: number }; + query: { additionalMetadata?: string }; path: { petId: number }; headers: never; body: never; diff --git a/test/typescript-generator/__snapshots__/parameters-type-coder.test.js.snap b/test/typescript-generator/__snapshots__/parameters-type-coder.test.js.snap index 44fbf98c..efc2bf89 100644 --- a/test/typescript-generator/__snapshots__/parameters-type-coder.test.js.snap +++ b/test/typescript-generator/__snapshots__/parameters-type-coder.test.js.snap @@ -16,7 +16,7 @@ exports[`a ParametersTypeCoder generates types for parameters (OAS2): path 1`] = `; exports[`a ParametersTypeCoder generates types for parameters (OAS2): query 1`] = ` -"type TestType = { name?: string; age?: string }; +"type TestType = { name?: string; age?: number }; " `; @@ -31,6 +31,6 @@ exports[`a ParametersTypeCoder generates types for parameters: path 1`] = ` `; exports[`a ParametersTypeCoder generates types for parameters: query 1`] = ` -"type TestType = { name?: string; age?: string }; +"type TestType = { name?: string; age?: number }; " `;