Skip to content

Commit

Permalink
Fixed parameter.body on post endpoints - #8.
Browse files Browse the repository at this point in the history
  • Loading branch information
brentrager authored and astahmer committed Oct 31, 2023
1 parent fd3b80b commit 00eb659
Show file tree
Hide file tree
Showing 18 changed files with 1,013 additions and 94 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-glasses-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"typed-openapi": minor
---

Fixed parameter.body on post endpoints - #8.
1 change: 1 addition & 0 deletions packages/typed-openapi/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ const generateEndpointSchemaList = (ctx: GeneratorContext) => {
${parameters.query ? `query: ${parameterObjectToString(parameters.query)},` : ""}
${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
${parameters.header ? `header: ${parameterObjectToString(parameters.header)},` : ""}
${parameters.body ? `body: ${parameterObjectToString(parameters.body)},` : ""}
}`
: "parameters: never,"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/typed-openapi/src/map-openapi-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {

// Body
if (operation.requestBody) {
endpoint.meta.hasParameters = true;
const requestBody = refs.unwrap(operation.requestBody ?? {});
const content = requestBody.content;
const matchingMediaType = Object.keys(content).find(isAllowedParamMediaTypes);
Expand Down Expand Up @@ -154,7 +155,7 @@ type MutationMethod = "post" | "put" | "patch" | "delete";
type Method = "get" | "head" | "options" | MutationMethod;

export type EndpointParameters = {
body?: Box;
body?: Box<BoxRef>;
query?: Box<BoxRef> | Record<string, AnyBox>;
header?: Box<BoxRef> | Record<string, AnyBox>;
path?: Box<BoxRef> | Record<string, AnyBox>;
Expand Down
24 changes: 19 additions & 5 deletions packages/typed-openapi/tests/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ describe("generator", () => {
export type put_UpdatePet = {
method: "PUT";
path: "/pet";
parameters: never;
parameters: {
body: Pet;
};
response: Schemas.Pet;
};
export type post_AddPet = {
method: "POST";
path: "/pet";
parameters: never;
parameters: {
body: Pet;
};
response: Schemas.Pet;
};
export type get_FindPetsByStatus = {
Expand Down Expand Up @@ -108,6 +112,8 @@ describe("generator", () => {
parameters: {
query: Partial<{ additionalMetadata: string }>;
path: { petId: number };
body: string;
};
response: Schemas.ApiResponse;
};
Expand All @@ -120,7 +126,9 @@ describe("generator", () => {
export type post_PlaceOrder = {
method: "POST";
path: "/store/order";
parameters: never;
parameters: {
body: Order;
};
response: Schemas.Order;
};
export type get_GetOrderById = {
Expand All @@ -142,13 +150,17 @@ describe("generator", () => {
export type post_CreateUser = {
method: "POST";
path: "/user";
parameters: never;
parameters: {
body: User;
};
response: Schemas.User;
};
export type post_CreateUsersWithListInput = {
method: "POST";
path: "/user/createWithList";
parameters: never;
parameters: {
body: Array<User>;
};
response: unknown;
};
export type get_LoginUser = {
Expand Down Expand Up @@ -178,6 +190,8 @@ describe("generator", () => {
path: "/user/{username}";
parameters: {
path: { username: string };
body: User;
};
response: unknown;
};
Expand Down
10 changes: 5 additions & 5 deletions packages/typed-openapi/tests/map-openapi-endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ describe("map-openapi-endpoints", () => {
"meta": {
"alias": "put_UpdatePet",
"areParametersRequired": false,
"hasParameters": false,
"hasParameters": true,
},
"method": "put",
"operation": {
Expand Down Expand Up @@ -1329,7 +1329,7 @@ describe("map-openapi-endpoints", () => {
"meta": {
"alias": "post_AddPet",
"areParametersRequired": false,
"hasParameters": false,
"hasParameters": true,
},
"method": "post",
"operation": {
Expand Down Expand Up @@ -1907,7 +1907,7 @@ describe("map-openapi-endpoints", () => {
"meta": {
"alias": "post_PlaceOrder",
"areParametersRequired": false,
"hasParameters": false,
"hasParameters": true,
},
"method": "post",
"operation": {
Expand Down Expand Up @@ -2081,7 +2081,7 @@ describe("map-openapi-endpoints", () => {
"meta": {
"alias": "post_CreateUser",
"areParametersRequired": false,
"hasParameters": false,
"hasParameters": true,
},
"method": "post",
"operation": {
Expand Down Expand Up @@ -2145,7 +2145,7 @@ describe("map-openapi-endpoints", () => {
"meta": {
"alias": "post_CreateUsersWithListInput",
"areParametersRequired": false,
"hasParameters": false,
"hasParameters": true,
},
"method": "post",
"operation": {
Expand Down
Loading

0 comments on commit 00eb659

Please sign in to comment.