Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ParametersTypeCoder to follow references (#1105) #1165

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-emus-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

follow $refs in parameters (fixes #1105)
18 changes: 10 additions & 8 deletions src/typescript-generator/parameters-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions test/typescript-generator/__snapshots__/generate.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
"
`;

Expand All @@ -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 };
"
`;
Loading