Skip to content

Commit

Permalink
Merge pull request #1060 from pmcelhaney/consumes-in-root
Browse files Browse the repository at this point in the history
fix an issue where request body type is not found when consumes is in the root
  • Loading branch information
pmcelhaney authored Oct 2, 2024
2 parents fc2610f + 2748fa2 commit a1623d6
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-planets-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

fix: request body type is not found in OpenAPI 2 when consumes is in the root
18 changes: 11 additions & 7 deletions src/typescript-generator/operation-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ export class OperationTypeCoder extends TypeCoder {
script,
);

const bodyRequirement = this.requirement.get("consumes")
? parameters
.find((parameter) =>
["body", "formData"].includes(parameter.get("in").data),
)
.get("schema")
: this.requirement.select("requestBody/content/application~1json/schema");
const bodyRequirement =
this.requirement.get("consumes") ||
this.requirement.specification?.rootRequirement?.get("consumes")
? parameters
?.find((parameter) =>
["body", "formData"].includes(parameter.get("in").data),
)
?.get("schema")
: this.requirement.select(
"requestBody/content/application~1json/schema",
);

const bodyType =
bodyRequirement === undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,69 @@ exports[`an OperationTypeCoder generates a complex post operation (OpenAPI 2) 1`
"
`;

exports[`an OperationTypeCoder generates a complex post operation (OpenAPI 2, consumes in root) 1`] = `
"type TestType = (
$: OmitValueWhenNever<{
query: { name?: string };
path: { id?: string };
header: { name?: string };
body: Type;
context: ExternalType;
response: ResponseBuilderFactory<{
200: {
headers: never;
requiredHeaders: never;
content: {
"application/json": {
schema: Type;
};
};
};
400: {
headers: never;
requiredHeaders: never;
content: {
"application/json": {
schema: Type;
};
};
};
[statusCode in Exclude<HttpStatusCode, 200 | 400>]: {
headers: never;
requiredHeaders: never;
content: {
"application/json": {
schema: Type;
};
};
};
}>;
x: SharedType;
proxy: (url: string) => COUNTERFACT_RESPONSE;
user: never;
}>,
) =>
| {
status: 200;
contentType?: "application/json";
body?: Type;
}
| {
status: 400;
contentType?: "application/json";
body?: Type;
}
| {
status: number | undefined;
contentType?: "application/json";
body?: Type;
}
| { status: 415; contentType: "text/plain"; body: string }
| COUNTERFACT_RESPONSE
| { ALL_REMAINING_HEADERS_ARE_OPTIONAL: COUNTERFACT_RESPONSE };
"
`;

exports[`an OperationTypeCoder generates a complex post operation 1`] = `
"type TestType = (
$: OmitValueWhenNever<{
Expand Down
46 changes: 46 additions & 0 deletions test/typescript-generator/operation-type-coder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,52 @@ describe("an OperationTypeCoder", () => {
).resolves.toMatchSnapshot();
});

it("generates a complex post operation (OpenAPI 2, consumes in root)", async () => {
const requirement = new Requirement(
{
parameters: [
{ in: "path", name: "id", type: "string" },
{ in: "query", name: "name", type: "string" },
{ in: "header", name: "name", type: "string" },
{
in: "body",
name: "body",
schema: { $ref: "#/components/schemas/Example" },
},
],

produces: ["application/json"],

responses: {
200: {
schema: { $ref: "#/components/schemas/Example" },
},

400: {
schema: { $ref: "#/components/schemas/Error" },
},

default: {
schema: { $ref: "#/components/schemas/Error" },
},
},
},
"#/paths/hello/post",
);

requirement.specification = {
rootRequirement: new Requirement({
consumes: ["application/json"],
}),
};

const coder = new OperationTypeCoder(requirement, "get");

await expect(
format(`type TestType = ${coder.write(dummyScript)}`),
).resolves.toMatchSnapshot();
});

it("generates a complex post operation (OpenAPI 2 with produces at the root)", async () => {
const specification = new Specification();

Expand Down

0 comments on commit a1623d6

Please sign in to comment.