Skip to content

Fix CancellationToken body parameter bug for POST/PUT/PATCH methods#97

Merged
twogood merged 5 commits into
mainfrom
copilot/fix-96
Aug 8, 2025
Merged

Fix CancellationToken body parameter bug for POST/PUT/PATCH methods#97
twogood merged 5 commits into
mainfrom
copilot/fix-96

Conversation

Copilot AI commented Aug 8, 2025

Copy link
Copy Markdown
Contributor

This PR fixes a bug where CancellationToken parameters were incorrectly being used as the request body for POST, PUT, and PATCH HTTP methods.

Problem

When a REST client method had a CancellationToken as the last parameter, the RequestHandler would attempt to serialize it as the request body instead of using the actual body parameter. This caused serialization errors like:

System.NotSupportedException: Serialization and deserialization of 'System.IntPtr' instances is not supported. Path: $.WaitHandle.Handle.

Root Cause

The RequestHandler constructor always set _bodyArgumentIndex to _parameters.Length - 1 (the last parameter), without considering that the last parameter might be a CancellationToken that should be handled separately.

Solution

Added logic in the RequestHandler constructor to detect when:

  1. The last parameter is a CancellationToken
  2. The HTTP method supports request bodies (POST, PUT, PATCH)

In these cases, the body argument index is adjusted to use the second-to-last parameter (_parameters.Length - 2) instead.

Example

Before this fix, the following would fail:

public interface IApiClient
{
    [Post("/api/data")]
    Task CreateData(MyRequest data, CancellationToken cancellationToken);
}

Now it correctly serializes data as the request body and uses cancellationToken for cancellation.

Testing

Added comprehensive tests covering:

  • POST, PUT, and PATCH methods with CancellationToken as last parameter
  • Verification that the correct request body is sent (not the CancellationToken)
  • Regression test ensuring methods without CancellationToken still work

All existing tests continue to pass, ensuring no breaking changes.

Fixes #96.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: twogood <189982+twogood@users.noreply.github.com>
Copilot AI changed the title [WIP] Bug: for POST/PUT etc that take a body, if a CancellationToken is the last parameter it will be sent instead of the body Fix CancellationToken body parameter bug for POST/PUT/PATCH methods Aug 8, 2025
Copilot AI requested a review from twogood August 8, 2025 05:53
@twogood

twogood commented Aug 8, 2025

Copy link
Copy Markdown
Owner

@copilot do not add dependency on any json package. Use a simple plain text body instead. Even if that is not the most common use case it is good enough for unit tests.

…ext bodies

Co-authored-by: twogood <189982+twogood@users.noreply.github.com>

Copilot AI commented Aug 8, 2025

Copy link
Copy Markdown
Contributor Author

@copilot do not add dependency on any json package. Use a simple plain text body instead. Even if that is not the most common use case it is good enough for unit tests.

Done! Removed the JSON package dependency and updated the tests to use plain text bodies instead. The tests are now much simpler and still effectively verify that CancellationToken parameters are not being used as request bodies. All tests continue to pass. (87f48ba)

@twogood twogood marked this pull request as ready for review August 8, 2025 08:17
@twogood twogood merged commit ae5d34e into main Aug 8, 2025
8 checks passed
@twogood twogood deleted the copilot/fix-96 branch August 8, 2025 08:32
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.

Bug: for POST/PUT etc that take a body, if a CancellationToken is the last parameter it will be sent instead of the body

2 participants