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

C# - Wrong type for position parameter in RequestBuilder #3791

Closed
Spaeda opened this issue Nov 23, 2023 · 2 comments · Fixed by #3801
Closed

C# - Wrong type for position parameter in RequestBuilder #3791

Spaeda opened this issue Nov 23, 2023 · 2 comments · Fixed by #3801
Assignees
Labels
generator Issues or improvements relater to generation capabilities. type:bug A broken experience WIP
Milestone

Comments

@Spaeda
Copy link
Contributor

Spaeda commented Nov 23, 2023

Hello,

When I generate a C# client with Kiota CLI version 1.8.2, result code doesn't correctly detect parameter type in url.
Type is correctly detected only if parameter is last url part (ex. http://myurl/<id>).

My swagger definition:

{
  "openapi": "3.0.1",
  "info": {
    "title": "Api",
    "version": "1.0"
  },
  "paths": {
    "/groups/{groupId}/users": {
      "get": {
        "tags": [
          "PersonEndpoint"
        ],
        "parameters": [
          {
            "name": "groupId",
            "in": "path",
            "required": true,
            "style": "simple",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    },
    "/users/{userId}": {
      "get": {
        "tags": [
          "UserEndpoint"
        ],
        "parameters": [
          {
            "name": "userId",
            "in": "path",
            "required": true,
            "style": "simple",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }
  },
  "components": { }
}

Used command line: kiota generate -l CSharp -c MyClient -n MyNamespace -d swagger.json -o MyClient

Generated RequestBuilder for my endpoint with parameter as last url part:

// <auto-generated/>
using Microsoft.Kiota.Abstractions;
using MyNamespace.Users.Item;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System;
namespace MyNamespace.Users {
    /// <summary>
    /// Builds and executes requests for operations under \users
    /// </summary>
    public class UsersRequestBuilder : BaseRequestBuilder {
        /// <summary>Gets an item from the MyNamespace.users.item collection</summary>
        /// <param name="position">Unique identifier of the item</param>
        public WithUserItemRequestBuilder this[Guid position] { get {
            var urlTplParams = new Dictionary<string, object>(PathParameters);
            urlTplParams.Add("userId", position);
            return new WithUserItemRequestBuilder(urlTplParams, RequestAdapter);
        } }
        /// <summary>Gets an item from the MyNamespace.users.item collection</summary>
        /// <param name="position">Unique identifier of the item</param>
        [Obsolete("This indexer is deprecated and will be removed in the next major version. Use the one with the typed parameter instead.")]
        public WithUserItemRequestBuilder this[string position] { get {
            var urlTplParams = new Dictionary<string, object>(PathParameters);
            if (!string.IsNullOrWhiteSpace(position)) urlTplParams.Add("userId", position);
            return new WithUserItemRequestBuilder(urlTplParams, RequestAdapter);
        } }
        /// <summary>
        /// Instantiates a new UsersRequestBuilder and sets the default values.
        /// </summary>
        /// <param name="pathParameters">Path parameters for the request</param>
        /// <param name="requestAdapter">The request adapter to use to execute the requests.</param>
        public UsersRequestBuilder(Dictionary<string, object> pathParameters, IRequestAdapter requestAdapter) : base(requestAdapter, "{+baseurl}/users", pathParameters) {
        }
        /// <summary>
        /// Instantiates a new UsersRequestBuilder and sets the default values.
        /// </summary>
        /// <param name="rawUrl">The raw URL to use for the request builder.</param>
        /// <param name="requestAdapter">The request adapter to use to execute the requests.</param>
        public UsersRequestBuilder(string rawUrl, IRequestAdapter requestAdapter) : base(requestAdapter, "{+baseurl}/users", rawUrl) {
        }
    }
}

Generated RequestBuilder for my endpoint with the parameter between two url part:

// <auto-generated/>
using Microsoft.Kiota.Abstractions;
using MyNamespace.Groups.Item;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System;
namespace MyNamespace.Groups {
    /// <summary>
    /// Builds and executes requests for operations under \groups
    /// </summary>
    public class GroupsRequestBuilder : BaseRequestBuilder {
        /// <summary>Gets an item from the MyNamespace.groups.item collection</summary>
        /// <param name="position">Unique identifier of the item</param>
        public WithGroupItemRequestBuilder this[string position] { get {
            var urlTplParams = new Dictionary<string, object>(PathParameters);
            urlTplParams.Add("groupId", position);
            return new WithGroupItemRequestBuilder(urlTplParams, RequestAdapter);
        } }
        /// <summary>
        /// Instantiates a new GroupsRequestBuilder and sets the default values.
        /// </summary>
        /// <param name="pathParameters">Path parameters for the request</param>
        /// <param name="requestAdapter">The request adapter to use to execute the requests.</param>
        public GroupsRequestBuilder(Dictionary<string, object> pathParameters, IRequestAdapter requestAdapter) : base(requestAdapter, "{+baseurl}/groups", pathParameters) {
        }
        /// <summary>
        /// Instantiates a new GroupsRequestBuilder and sets the default values.
        /// </summary>
        /// <param name="rawUrl">The raw URL to use for the request builder.</param>
        /// <param name="requestAdapter">The request adapter to use to execute the requests.</param>
        public GroupsRequestBuilder(string rawUrl, IRequestAdapter requestAdapter) : base(requestAdapter, "{+baseurl}/groups", rawUrl) {
        }
    }
}

I search in issues but I didn't find any tickets for this bug.

@sebastienlevert
Copy link
Contributor

Just to confirm the bug you are seeing. You mean that the public WithGroupItemRequestBuilder this[string position] should have a corresponding public WithGroupItemRequestBuilder this[Guid position] , the same way the user has it?

@baywet
Copy link
Member

baywet commented Nov 23, 2023

Ah, un ancien SUPINFO!! :D
Hi @Spaeda,
Thanks for your interest in kiota and for reaching out.
This is most likely caused by the skip in this method.

.Skip(parentNode.Path.Count(static x => x == OpenAPIUrlTreeNodePathSeparator)))

The code should probably be updated not to skip anything instead, and use the last parameter in the path segments.

You can add a unit test after this one.

public async Task IndexerTypeIsAccurateAndBackwardCompatibleIndexersAreAdded()

With all that information in mind, would you be willing to submit a pull request?

@baywet baywet added type:bug A broken experience generator Issues or improvements relater to generation capabilities. labels Nov 23, 2023
@baywet baywet added this to the Kiota v1.10 milestone Nov 23, 2023
@baywet baywet modified the milestones: Kiota v1.10, Kiota v1.9 Dec 7, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in Kiota Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generator Issues or improvements relater to generation capabilities. type:bug A broken experience WIP
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants