Skip to content

fix: put requests with application/x-www-form-urlencoded is incorrect… #1341

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions .changeset/fix-form-urlencoded-put.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"swagger-typescript-api": patch
---

Fix: PUT requests with application/x-www-form-urlencoded content type

This fixes an issue where PUT requests with `application/x-www-form-urlencoded` content type were incorrectly sent as `multipart/form-data`.
A new `createUrlEncoded` method has been added to the `HttpClient` to handle this content type correctly.
41 changes: 37 additions & 4 deletions src/schema-routes/schema-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,21 +561,48 @@ export class SchemaRoutes {
});
}

if (routeParams.formData.length) {
contentKind = CONTENT_KIND.FORM_DATA;
if (
contentKind === CONTENT_KIND.URL_ENCODED &&
routeParams.formData.length
) {
schema = this.convertRouteParamsIntoObject(routeParams.formData);
content = this.schemaParserFabric.getInlineParseContent(
schema,
typeName,
[operationId],
);
} else if (contentKind === CONTENT_KIND.FORM_DATA) {
schema = this.getSchemaFromRequestType(requestBody);
} else if (routeParams.formData.length) {
contentKind = CONTENT_KIND.FORM_DATA;
schema = this.convertRouteParamsIntoObject(routeParams.formData);
content = this.schemaParserFabric.getInlineParseContent(
schema,
typeName,
[operationId],
);
} else if (contentKind === CONTENT_KIND.URL_ENCODED) {
schema = this.getSchemaFromRequestType(requestBody);
content = this.schemaParserFabric.schemaUtils.safeAddNullToType(
requestBody,
this.getTypeFromRequestInfo({
requestInfo: requestBody,
parsedSchemas,
operationId,
defaultType: "any",
typeName,
}),
);
} else if (contentKind === CONTENT_KIND.FORM_DATA) {
schema = this.getSchemaFromRequestType(requestBody);
content = this.schemaParserFabric.schemaUtils.safeAddNullToType(
requestBody,
this.getTypeFromRequestInfo({
requestInfo: requestBody,
parsedSchemas,
operationId,
defaultType: "any",
typeName,
}),
);
} else if (requestBody) {
schema = this.getSchemaFromRequestType(requestBody);
content = this.schemaParserFabric.schemaUtils.safeAddNullToType(
Expand Down Expand Up @@ -1074,6 +1101,12 @@ export class SchemaRoutes {
security: hasSecurity,
method: method,
requestParams: requestParamsSchema,
type:
requestBodyInfo.contentKind === CONTENT_KIND.FORM_DATA
? "multipart/form-data"
: requestBodyInfo.contentKind === CONTENT_KIND.URL_ENCODED
? "application/x-www-form-urlencoded"
: undefined,

payload: specificArgs.body,
query: specificArgs.query,
Expand Down
20 changes: 20 additions & 0 deletions templates/base/http-clients/axios-http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ export class HttpClient<SecurityDataType = unknown> {
}, new FormData());
}

protected createUrlEncoded(input: Record<string, unknown>): URLSearchParams {
if (input instanceof URLSearchParams) {
return input;
}
return Object.keys(input || {}).reduce((searchParams, key) => {
const property = input[key];
const propertyContent: any[] = (property instanceof Array) ? property : [property]

for (const formItem of propertyContent) {
searchParams.append(key, this.stringifyFormItem(formItem));
}

return searchParams;
}, new URLSearchParams());
}

public request = async <T = any, _E = any>({
secure,
path,
Expand All @@ -120,6 +136,10 @@ export class HttpClient<SecurityDataType = unknown> {
body = this.createFormData(body as Record<string, unknown>);
}

if (type === ContentType.UrlEncoded && body && body !== null && typeof body === "object") {
body = this.createUrlEncoded(body as Record<string, unknown>);
}

if (type === ContentType.Text && body && body !== null && typeof body !== "string") {
body = JSON.stringify(body);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/extended.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6376,7 +6376,7 @@ export class Api<
method: "POST",
body: data,
secure: true,
type: ContentType.FormData,
type: ContentType.UrlEncoded,
...params,
}),

Expand Down Expand Up @@ -69452,7 +69452,7 @@ export class Api<
method: "POST",
body: data,
secure: true,
type: ContentType.FormData,
type: ContentType.UrlEncoded,
...params,
}),

Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/simple.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3968,7 +3968,7 @@ export class Api<
method: "POST",
body: data,
secure: true,
type: ContentType.FormData,
type: ContentType.UrlEncoded,
...params,
}),

Expand Down Expand Up @@ -43623,7 +43623,7 @@ export class Api<
method: "POST",
body: data,
secure: true,
type: ContentType.FormData,
type: ContentType.UrlEncoded,
...params,
}),

Expand Down
26 changes: 26 additions & 0 deletions tests/spec/axios/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,23 @@ export class HttpClient<SecurityDataType = unknown> {
}, new FormData());
}

protected createUrlEncoded(input: Record<string, unknown>): URLSearchParams {
if (input instanceof URLSearchParams) {
return input;
}
return Object.keys(input || {}).reduce((searchParams, key) => {
const property = input[key];
const propertyContent: any[] =
property instanceof Array ? property : [property];

for (const formItem of propertyContent) {
searchParams.append(key, this.stringifyFormItem(formItem));
}

return searchParams;
}, new URLSearchParams());
}

public request = async <T = any, _E = any>({
secure,
path,
Expand All @@ -2063,6 +2080,15 @@ export class HttpClient<SecurityDataType = unknown> {
body = this.createFormData(body as Record<string, unknown>);
}

if (
type === ContentType.UrlEncoded &&
body &&
body !== null &&
typeof body === "object"
) {
body = this.createUrlEncoded(body as Record<string, unknown>);
}

if (
type === ContentType.Text &&
body &&
Expand Down
26 changes: 26 additions & 0 deletions tests/spec/axiosSingleHttpClient/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,23 @@ export class HttpClient<SecurityDataType = unknown> {
}, new FormData());
}

protected createUrlEncoded(input: Record<string, unknown>): URLSearchParams {
if (input instanceof URLSearchParams) {
return input;
}
return Object.keys(input || {}).reduce((searchParams, key) => {
const property = input[key];
const propertyContent: any[] =
property instanceof Array ? property : [property];

for (const formItem of propertyContent) {
searchParams.append(key, this.stringifyFormItem(formItem));
}

return searchParams;
}, new URLSearchParams());
}

public request = async <T = any, _E = any>({
secure,
path,
Expand All @@ -2063,6 +2080,15 @@ export class HttpClient<SecurityDataType = unknown> {
body = this.createFormData(body as Record<string, unknown>);
}

if (
type === ContentType.UrlEncoded &&
body &&
body !== null &&
typeof body === "object"
) {
body = this.createUrlEncoded(body as Record<string, unknown>);
}

if (
type === ContentType.Text &&
body &&
Expand Down
Loading