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

Encode the request body if Content-Type header is x-www-form-urlencoded #2096

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

obulat
Copy link

@obulat obulat commented Jan 11, 2025

Changes

Fixes #2069
This PR updates the defaultBodySerializer to correctly serialize the body if Content-Type header is set to x-www-form-urlencoded. This is useful for OAuth2 token requests.

How to Review

I'm not sure about the headers: what type are they, and are they normalized?
I added handling for both a plain JS object (headers['Accept-Content']) and a Headers object (headers.get['Accept-Content']), as well as both capitalized and lower-case versions.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

@obulat obulat requested a review from a team as a code owner January 11, 2025 06:20
@obulat obulat requested a review from drwpow January 11, 2025 06:20
Copy link

netlify bot commented Jan 11, 2025

👷 Deploy request for openapi-ts pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 3fd7e57

Copy link

changeset-bot bot commented Jan 11, 2025

⚠️ No Changeset found

Latest commit: 3fd7e57

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@@ -72,7 +72,7 @@ export default function createClient(clientOptions) {
});
}

const serializedBody = body === undefined ? undefined : bodySerializer(body);
const serializedBody = body === undefined ? undefined : bodySerializer(body, headers);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So these are the headers unique to this request (probably could be named better). Below on line 92, we merge all the headers together. We should probably move that up, adn pass that in here to bodySerializer. That way it takes into account headers that users have set in their createClient() call, and it handles the normalization for you so it’s always a Header type (and you aren’t dealing with what the user passed in)

@drwpow
Copy link
Contributor

drwpow commented Jan 25, 2025

This approach looks good to me, and good test! But before we merge, we’ll want to:

  1. Use the merged/normalized headers like the comment mentioned
  2. Add a patch changeset (see comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically encode the body when "Accept": "application/x-www-form-encoded" header is passed
2 participants