Skip to content

IResponseHandler set through RequestInformation.SetResponseHandler is ignored #7017

@bstordrup

Description

@bstordrup

What are you generating using Kiota, clients or plugins?

API Client/SDK

In what context or format are you using Kiota?

Windows executable

Client library/SDK language

Csharp

Describe the bug

The Microsoft.Kiota.Abstractions.RequestInformation class has a SetResponseHandler method that sets an IResponseHandler instance and sets the ResponseHandlerOption to the request options.

As far as I understand, you should use code like this to use it:

    PackagesRequestBuilder packagesRequestBuilder = vbusOrg.Packages;
    RequestInformation packagesRequestInfo = packagesRequestBuilder.ToGetRequestInformation();
    packagesRequestInfo.SetResponseHandler(adapter); // adapter implements IResponseHandler

    var packageListResponse = await packagesRequestBuilder.GetAsync(requestConfiguration =>
    {
        requestConfiguration.QueryParameters.PackageType = GetPackage_typeQueryParameterType.Nuget;
        requestConfiguration.QueryParameters.PerPage = 5;
    });

But the problem is that the GetAsync method is generated with the following content:

        public async Task<List<global::GitHub.Models.Package>?> GetAsync(Action<RequestConfiguration<global::GitHub.Orgs.Item.Packages.PackagesRequestBuilder.PackagesRequestBuilderGetQueryParameters>>? requestConfiguration = default, CancellationToken cancellationToken = default)
        {
            var requestInfo = ToGetRequestInformation(requestConfiguration);
            var errorMapping = new Dictionary<string, ParsableFactory<IParsable>>
            {
                { "401", global::GitHub.Models.BasicError.CreateFromDiscriminatorValue },
                { "403", global::GitHub.Models.BasicError.CreateFromDiscriminatorValue },
            };
            var collectionResult = await RequestAdapter.SendCollectionAsync<global::GitHub.Models.Package>(requestInfo, global::GitHub.Models.Package.CreateFromDiscriminatorValue, errorMapping, cancellationToken).ConfigureAwait(false);
            return collectionResult?.AsList();
        }

As seen, the method generates a new RequestInformation, and I cannot see any methods that actually allow the one generated before the call to GetAsync to be passed into the method.

This means that it (as far as I can see) in practice is impossible to use your own IResponseHandler implementation.

Expected behavior

When setting a custom IResponseHandler instance to the RequestInformation instance, I would expect it to be utilized.

How to reproduce

  1. Generate an API client of any kind (I'm using github api).
  2. Create a class implementing IResponseHandler.
  3. Set a breakpoint in the HandleResponseAsync method.
  4. Execute the GetAsync code as shown in bug description.
  5. Notice that tghe breakpoint from (3) is not hit

Open API description file

No response

Kiota Version

1.28.0+57130b1b1db3bc5c060498682f41e20c8ae089f2

Latest Kiota version known to work for scenario above?(Not required)

No response

Known Workarounds

No response

Configuration

  • OS: Windows 11 and Linux Mint 22.2 (based on Ubuntu 24.04)
  • Architecture x64 (both)

Debug output

Click to expand log

Other information

As far as I can see, the issue is that the RequestInformation instance generated before the call to GetAsync is simply not passed into the GetAsync method or the RequestBuilder instance in any way.

I see some ways of resolving this:

  1. Create an overload of all the Send* methods that pass a RequestInformation instanse to be used instead of creating a new one.
  2. Add an optional RequestInformation parameter to all the Send* methods and use that instead of creating a new one.
  3. Add a RequestInformation field to the RequestBuilder class and use that if set instead of creating a new one.
  4. Create an event handler on RequestBuilder class that can be subscribed to in order to provide the RequestInformation.

Note that these would need to be done on the IRequestAdapter interface also as the RequestBuilder calls further to the RequestAdapter member.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CsharpPull requests that update .net codetype:bugA broken experience

    Type

    No type

    Projects

    Status

    Proposed 💡

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions