Skip to content

Commit dbe4784

Browse files
authored
fix: [IOCOM-2842] Add support for body in patch call (#333)
* Add support for body in patch API * Copilot suggestion
1 parent 692ed2b commit dbe4784

6 files changed

Lines changed: 151 additions & 3 deletions

File tree

__mocks__/api.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ paths:
177177
description: "Created"
178178
"500":
179179
description: "Fatal error"
180+
/patch-test-parameter-with-body-ref:
181+
patch:
182+
operationId: "patchTestParameterWithBodyReference"
183+
parameters:
184+
- name: body
185+
in: body
186+
schema:
187+
$ref: "#/definitions/NewModel"
188+
responses:
189+
"201":
190+
description: "Created"
191+
"500":
192+
description: "Fatal error"
180193

181194
/test-parameter-with-dash/{path-param}:
182195
get:

__mocks__/openapi_v3/api.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ paths:
198198
description: "Created"
199199
500:
200200
description: "Fatal error"
201+
/patch-test-parameter-with-body-ref:
202+
patch:
203+
operationId: "patchTestParameterWithBodyReference"
204+
requestBody:
205+
content:
206+
application/json:
207+
schema:
208+
$ref: "#/components/schemas/NewModel"
209+
responses:
210+
201:
211+
description: "Created"
212+
500:
213+
description: "Fatal error"
201214
/test-parameter-with-dash/{path-param}:
202215
get:
203216
operationId: "testParameterWithDash"

src/commands/gen-api-models/__tests__/__snapshots__/index.test.ts.snap

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,8 @@ import {
933933
testParameterWithBodyReferenceDefaultDecoder,
934934
PutTestParameterWithBodyReferenceT,
935935
putTestParameterWithBodyReferenceDefaultDecoder,
936+
PatchTestParameterWithBodyReferenceT,
937+
patchTestParameterWithBodyReferenceDefaultDecoder,
936938
TestParameterWithDashT,
937939
testParameterWithDashDefaultDecoder,
938940
TestParameterWithDashAnUnderscoreT,
@@ -975,6 +977,7 @@ export type ApiOperation = TypeofApiCall<TestAuthBearerT> &
975977
TypeofApiCall<TestParameterWithReferenceT> &
976978
TypeofApiCall<TestParameterWithBodyReferenceT> &
977979
TypeofApiCall<PutTestParameterWithBodyReferenceT> &
980+
TypeofApiCall<PatchTestParameterWithBodyReferenceT> &
978981
TypeofApiCall<TestParameterWithDashT> &
979982
TypeofApiCall<TestParameterWithDashAnUnderscoreT> &
980983
TypeofApiCall<TestWithTwoParamsT> &
@@ -998,6 +1001,7 @@ export type ParamKeys = keyof (TypeofApiParams<TestAuthBearerT> &
9981001
TypeofApiParams<TestParameterWithReferenceT> &
9991002
TypeofApiParams<TestParameterWithBodyReferenceT> &
10001003
TypeofApiParams<PutTestParameterWithBodyReferenceT> &
1004+
TypeofApiParams<PatchTestParameterWithBodyReferenceT> &
10011005
TypeofApiParams<TestParameterWithDashT> &
10021006
TypeofApiParams<TestParameterWithDashAnUnderscoreT> &
10031007
TypeofApiParams<TestWithTwoParamsT> &
@@ -1043,6 +1047,7 @@ export type WithDefaultsT<
10431047
| TestParameterWithReferenceT
10441048
| TestParameterWithBodyReferenceT
10451049
| PutTestParameterWithBodyReferenceT
1050+
| PatchTestParameterWithBodyReferenceT
10461051
| TestParameterWithDashT
10471052
| TestParameterWithDashAnUnderscoreT
10481053
| TestWithTwoParamsT
@@ -1092,6 +1097,10 @@ export type Client<
10921097
PutTestParameterWithBodyReferenceT
10931098
>;
10941099

1100+
readonly patchTestParameterWithBodyReference: TypeofApiCall<
1101+
PatchTestParameterWithBodyReferenceT
1102+
>;
1103+
10951104
readonly testParameterWithDash: TypeofApiCall<TestParameterWithDashT>;
10961105

10971106
readonly testParameterWithDashAnUnderscore: TypeofApiCall<
@@ -1193,6 +1202,13 @@ export type Client<
11931202
>
11941203
>;
11951204

1205+
readonly patchTestParameterWithBodyReference: TypeofApiCall<
1206+
ReplaceRequestParams<
1207+
PatchTestParameterWithBodyReferenceT,
1208+
Omit<RequestParams<PatchTestParameterWithBodyReferenceT>, K>
1209+
>
1210+
>;
1211+
11961212
readonly testParameterWithDash: TypeofApiCall<
11971213
ReplaceRequestParams<
11981214
TestParameterWithDashT,
@@ -1554,6 +1570,35 @@ export function createClient<K extends ParamKeys>({
15541570
options
15551571
);
15561572

1573+
const patchTestParameterWithBodyReferenceT: ReplaceRequestParams<
1574+
PatchTestParameterWithBodyReferenceT,
1575+
RequestParams<PatchTestParameterWithBodyReferenceT>
1576+
> = {
1577+
method: \\"patch\\",
1578+
1579+
headers: ({ [\\"customToken\\"]: customToken }) => ({
1580+
\\"custom-token\\": customToken,
1581+
1582+
\\"Content-Type\\": \\"application/json\\"
1583+
}),
1584+
response_decoder: patchTestParameterWithBodyReferenceDefaultDecoder(),
1585+
url: ({}) => \`\${basePath}/patch-test-parameter-with-body-ref\`,
1586+
1587+
body: ({ [\\"body\\"]: body }) =>
1588+
body?.constructor?.name === \\"Readable\\" ||
1589+
body?.constructor?.name === \\"ReadableStream\\"
1590+
? (body as ReadableStream)
1591+
: body?.constructor?.name === \\"Buffer\\"
1592+
? (body as Buffer)
1593+
: JSON.stringify(body),
1594+
1595+
query: () => withoutUndefinedValues({})
1596+
};
1597+
const patchTestParameterWithBodyReference: TypeofApiCall<PatchTestParameterWithBodyReferenceT> = createFetchRequestForApi(
1598+
patchTestParameterWithBodyReferenceT,
1599+
options
1600+
);
1601+
15571602
const testParameterWithDashT: ReplaceRequestParams<
15581603
TestParameterWithDashT,
15591604
RequestParams<TestParameterWithDashT>
@@ -1829,6 +1874,9 @@ export function createClient<K extends ParamKeys>({
18291874
putTestParameterWithBodyReference: (withDefaults || identity)(
18301875
putTestParameterWithBodyReference
18311876
),
1877+
patchTestParameterWithBodyReference: (withDefaults || identity)(
1878+
patchTestParameterWithBodyReference
1879+
),
18321880
testParameterWithDash: (withDefaults || identity)(testParameterWithDash),
18331881
testParameterWithDashAnUnderscore: (withDefaults || identity)(
18341882
testParameterWithDashAnUnderscore
@@ -2921,6 +2969,8 @@ import {
29212969
testParameterWithBodyReferenceDefaultDecoder,
29222970
PutTestParameterWithBodyReferenceT,
29232971
putTestParameterWithBodyReferenceDefaultDecoder,
2972+
PatchTestParameterWithBodyReferenceT,
2973+
patchTestParameterWithBodyReferenceDefaultDecoder,
29242974
TestParameterWithDashT,
29252975
testParameterWithDashDefaultDecoder,
29262976
TestParameterWithDashAnUnderscoreT,
@@ -2964,6 +3014,7 @@ export type ApiOperation = TypeofApiCall<TestAuthBearerT> &
29643014
TypeofApiCall<TestParameterWithReferenceT> &
29653015
TypeofApiCall<TestParameterWithBodyReferenceT> &
29663016
TypeofApiCall<PutTestParameterWithBodyReferenceT> &
3017+
TypeofApiCall<PatchTestParameterWithBodyReferenceT> &
29673018
TypeofApiCall<TestParameterWithDashT> &
29683019
TypeofApiCall<TestParameterWithDashAnUnderscoreT> &
29693020
TypeofApiCall<TestWithTwoParamsT> &
@@ -2988,6 +3039,7 @@ export type ParamKeys = keyof (TypeofApiParams<TestAuthBearerT> &
29883039
TypeofApiParams<TestParameterWithReferenceT> &
29893040
TypeofApiParams<TestParameterWithBodyReferenceT> &
29903041
TypeofApiParams<PutTestParameterWithBodyReferenceT> &
3042+
TypeofApiParams<PatchTestParameterWithBodyReferenceT> &
29913043
TypeofApiParams<TestParameterWithDashT> &
29923044
TypeofApiParams<TestParameterWithDashAnUnderscoreT> &
29933045
TypeofApiParams<TestWithTwoParamsT> &
@@ -3034,6 +3086,7 @@ export type WithDefaultsT<
30343086
| TestParameterWithReferenceT
30353087
| TestParameterWithBodyReferenceT
30363088
| PutTestParameterWithBodyReferenceT
3089+
| PatchTestParameterWithBodyReferenceT
30373090
| TestParameterWithDashT
30383091
| TestParameterWithDashAnUnderscoreT
30393092
| TestWithTwoParamsT
@@ -3085,6 +3138,10 @@ export type Client<
30853138
PutTestParameterWithBodyReferenceT
30863139
>;
30873140

3141+
readonly patchTestParameterWithBodyReference: TypeofApiCall<
3142+
PatchTestParameterWithBodyReferenceT
3143+
>;
3144+
30883145
readonly testParameterWithDash: TypeofApiCall<TestParameterWithDashT>;
30893146

30903147
readonly testParameterWithDashAnUnderscore: TypeofApiCall<
@@ -3193,6 +3250,13 @@ export type Client<
31933250
>
31943251
>;
31953252

3253+
readonly patchTestParameterWithBodyReference: TypeofApiCall<
3254+
ReplaceRequestParams<
3255+
PatchTestParameterWithBodyReferenceT,
3256+
Omit<RequestParams<PatchTestParameterWithBodyReferenceT>, K>
3257+
>
3258+
>;
3259+
31963260
readonly testParameterWithDash: TypeofApiCall<
31973261
ReplaceRequestParams<
31983262
TestParameterWithDashT,
@@ -3584,6 +3648,35 @@ export function createClient<K extends ParamKeys>({
35843648
options
35853649
);
35863650

3651+
const patchTestParameterWithBodyReferenceT: ReplaceRequestParams<
3652+
PatchTestParameterWithBodyReferenceT,
3653+
RequestParams<PatchTestParameterWithBodyReferenceT>
3654+
> = {
3655+
method: \\"patch\\",
3656+
3657+
headers: ({ [\\"customToken\\"]: customToken }) => ({
3658+
\\"custom-token\\": customToken,
3659+
3660+
\\"Content-Type\\": \\"application/json\\"
3661+
}),
3662+
response_decoder: patchTestParameterWithBodyReferenceDefaultDecoder(),
3663+
url: ({}) => \`\${basePath}/patch-test-parameter-with-body-ref\`,
3664+
3665+
body: ({ [\\"body\\"]: body }) =>
3666+
body?.constructor?.name === \\"Readable\\" ||
3667+
body?.constructor?.name === \\"ReadableStream\\"
3668+
? (body as ReadableStream)
3669+
: body?.constructor?.name === \\"Buffer\\"
3670+
? (body as Buffer)
3671+
: JSON.stringify(body),
3672+
3673+
query: () => withoutUndefinedValues({})
3674+
};
3675+
const patchTestParameterWithBodyReference: TypeofApiCall<PatchTestParameterWithBodyReferenceT> = createFetchRequestForApi(
3676+
patchTestParameterWithBodyReferenceT,
3677+
options
3678+
);
3679+
35873680
const testParameterWithDashT: ReplaceRequestParams<
35883681
TestParameterWithDashT,
35893682
RequestParams<TestParameterWithDashT>
@@ -3860,6 +3953,9 @@ export function createClient<K extends ParamKeys>({
38603953
putTestParameterWithBodyReference: (withDefaults || identity)(
38613954
putTestParameterWithBodyReference
38623955
),
3956+
patchTestParameterWithBodyReference: (withDefaults || identity)(
3957+
patchTestParameterWithBodyReference
3958+
),
38633959
testParameterWithDash: (withDefaults || identity)(testParameterWithDash),
38643960
testParameterWithDashAnUnderscore: (withDefaults || identity)(
38653961
testParameterWithDashAnUnderscore

src/commands/gen-api-models/__tests__/parse.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,32 @@ describe.each`
240240
);
241241
});
242242

243+
it("should parse a patch operation with body as ref", () => {
244+
const parsed = getParser(spec).parseOperation(
245+
//@ts-ignore
246+
spec,
247+
"/patch-test-parameter-with-body-ref",
248+
[],
249+
"undefined",
250+
"undefined"
251+
)("patch");
252+
253+
expect(parsed).toEqual(
254+
expect.objectContaining({
255+
method: "patch",
256+
path: "/patch-test-parameter-with-body-ref",
257+
consumes: "application/json",
258+
parameters: expect.arrayContaining([
259+
{
260+
name: "body?",
261+
in: "body",
262+
type: "NewModel | ReadableStream<Uint8Array> | Buffer"
263+
}
264+
])
265+
})
266+
);
267+
});
268+
243269
it("should parse an operation with header parameters", () => {
244270
const parsed = getParser(spec).parseOperation(
245271
//@ts-ignore

src/commands/gen-api-models/parse.v3.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ export const parseOperation = (
349349
]
350350
: [undefined, false];
351351
const bodyParam: ReadonlyArray<IParameterInfo> =
352-
["post", "put"].includes(method) && bodySchema
352+
["patch", "post", "put"].includes(method) && bodySchema
353353
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
354354
// @ts-ignore
355355
"multipart/form-data" in operation?.requestBody?.content
@@ -398,7 +398,7 @@ export const parseOperation = (
398398
];
399399

400400
const contentTypeHeaders =
401-
(method === "post" || method === "put") &&
401+
(method === "patch" || method === "post" || method === "put") &&
402402
Object.keys([...operationParams, ...bodyParam]).length > 0
403403
? ["Content-Type"]
404404
: [];

src/commands/gen-api-models/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface IGenerateApiOptions {
2020
/**
2121
* Supported http methods
2222
*/
23-
export type SupportedMethod = "get" | "post" | "put" | "delete";
23+
export type SupportedMethod = "get" | "patch" | "post" | "put" | "delete";
2424

2525
export type SupportedAuthScheme = "bearer" | "digest" | "none";
2626

0 commit comments

Comments
 (0)