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

[BUG] HeaderCollection attribute doesn't seem to work with POSTs #1102

Open
uhfath opened this issue Mar 3, 2021 · 4 comments
Open

[BUG] HeaderCollection attribute doesn't seem to work with POSTs #1102

uhfath opened this issue Mar 3, 2021 · 4 comments
Labels

Comments

@uhfath
Copy link

uhfath commented Mar 3, 2021

As described in docs I use [HeaderCollection] attribute to supply some dynamic headers to my requests:

[Get("/{bucket}")]
Task<ListBucketResult> ListObjectsAsync(string bucket, ListObjectsRequest request, [HeaderCollection]IDictionary<string, string> headers, CancellationToken cancellationToken = default);

[Post("/{bucket}/{key}?uploads")]
Task<InitiateMultipartUploadResult> CreateMultipartUploadAsync(string bucket, string key, [HeaderCollection]IDictionary<string, string> headers, CancellationToken cancellationToken = default);

While it works perfectly with GETs it just doesn't work the same way with POSTs:

One or more errors occurred. (The type System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] is not supported because it implements IDictionary.)
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at TestMinioClient.Program.Main(String[] args) in D:\Projects\TestMinioClient\Program.cs:line 237

Also with GETs everything is working when supplied header collection is null. But an exception (value is null) is thrown when using the same with POSTs.

Tested using 6.0.24

@uhfath uhfath added the bug label Mar 3, 2021
@james-s-tayler
Copy link
Contributor

james-s-tayler commented Mar 19, 2021

Interesting. We've got a test where [HeaderCollection] is used in conjunction with POST and that works fine, though the difference I can see here is that test includes a [Body] in the method signature. Looking at the code sample and the stacktrace my initial suspicions are that 1) it's trying to serialize the [HeaderCollection] dictionary for some reason, and 2) your method signature is causing it to do so for some reason. Gut feel is a bug triggered by not having a [Body]. Will investigate.

Edit: Suspicions confirmed. I have managed to create a failing test for this when [HeaderCollection] is present on a POST request with no [Body] attribute.

Looks like the logic in RestMethodInfo.cs attempts to find what it considers to be the Body and if [Body] is present it will use that, else it has some logic which makes certain assumptions. In this case it's as I suspected - it's guessing that the HeaderCollection is the Body of the request, hence this behavior.

Looks like I missed a spot there. Will update that logic to ignore parameters with attribute [HeaderCollection]

@james-s-tayler
Copy link
Contributor

@uhfath I have opened a PR to fix this.

@uhfath
Copy link
Author

uhfath commented Mar 24, 2021

@james-s-tayler thanks for taking a look into this and fixing the issue. Will test as soon as I get the chance.

@james-s-tayler
Copy link
Contributor

james-s-tayler commented Mar 24, 2021

@uhfath no worries. Looks like the fix will be released in 6.0.42 so you'll have to wait until that's available in NuGet https://www.nuget.org/packages/Refit/

The automatic type-safe REST library for Xamarin and .NET

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

No branches or pull requests

2 participants