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

Parameter Array - InvalidCastException #3354

Closed
szv opened this issue Sep 24, 2023 · 4 comments · Fixed by #3477
Closed

Parameter Array - InvalidCastException #3354

szv opened this issue Sep 24, 2023 · 4 comments · Fixed by #3477
Assignees
Labels
enhancement New feature or request generator Issues or improvements relater to generation capabilities. WIP
Milestone

Comments

@szv
Copy link

szv commented Sep 24, 2023

Hello,

I am facing the following problem.

Kiota v1.6.1

Description

Backend

I have a backend using AspNetCore's MinimalAPI as shown below:

app.MapGet("/api/ratings", ([FromQuery] long[] bookIds, [FromServices] ReviewRepository repo) =>
{
    return repo.GetRatings(bookIds);
})
.WithName("GetRatings")
.WithOpenApi();
OpenAPI document
{
  "openapi": "3.0.1",
  "info": {
    "title": "BookSample.ReviewAPI",
    "version": "1.0"
  },
  "servers": [
    {
      "url": "https://localhost:7078"
    }
  ],
  "paths": {
    "/api/ratings": {
      "get": {
        "tags": [
          "Ratings"
        ],
        "operationId": "GetRatings",
        "parameters": [
          {
            "name": "bookIds",
            "in": "query",
            "required": true,
            "style": "form",
            "schema": {
              "type": "array",
              "items": {
                "type": "integer",
                "format": "int64"
              }
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/api/reviews": {
      "get": {
        "tags": [
          "Reviews"
        ],
        "operationId": "GetReviews",
        "parameters": [
          {
            "name": "bookIds",
            "in": "query",
            "required": true,
            "style": "form",
            "schema": {
              "type": "array",
              "items": {
                "type": "integer",
                "format": "int64"
              }
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Review"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Review": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int64"
          },
          "bookId": {
            "type": "integer",
            "format": "int64"
          },
          "username": {
            "type": "string",
            "nullable": true
          },
          "text": {
            "type": "string",
            "nullable": true
          },
          "stars": {
            "type": "integer",
            "format": "int32"
          }
        },
        "additionalProperties": false
      }
    }
  }
}

Client

On the client the C# API-client generated by Kiota throws an InvalidCastException, when calling the endpoint described above.

await _reviewsClient.Api
  .Ratings
  .GetAsync(x => x.QueryParameters.BookIds = new long?[] { 1, 2, 3, 4 }, cancellationToken);

The exception message says: "Unable to cast object of type 'System.Int64' to type 'System.String'."

Workaround

When replacing the long[] with a string[] on the backend and client, it works.

As a workaround I convert the long[] to a string[] manually.
On the backend I parse the strings.

@github-project-automation github-project-automation bot moved this to Todo in Kiota Sep 24, 2023
@baywet baywet self-assigned this Sep 25, 2023
@baywet baywet added enhancement New feature or request generator Issues or improvements relater to generation capabilities. labels Sep 25, 2023
@baywet baywet added this to the Kiota v2.0 milestone Sep 25, 2023
@baywet
Copy link
Member

baywet commented Sep 25, 2023

Hi @szv
Thanks for your interest in kiota and for reaching out.
This issue has the same origin as #2490 and would represent a breaking change if we fixed it now.
Scheduling for v2 for the time being.
We could project both properties (string[], obsolete, and another one as long[]) as a workaround to make the change non breaking. But we'll know more about this approach once #2952 is implemented.

@darrelmiller
Copy link
Member

Thanks @baywet for bringing all these issue together. I knew we had discussed these before but couldn't find exactly the right conversations to link.

@baywet
Copy link
Member

baywet commented Oct 12, 2023

addressed by #3477

@Nex-Code
Copy link

@baywet I don't believe the changes you added for #3477 fixed this above issue.

I've made a PR here microsoft/kiota-dotnet#158

When an array is passed into the query parameters it needs to be expanded and the items dealt with as their own components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request generator Issues or improvements relater to generation capabilities. WIP
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants