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

QueryParameters Enum in Uri - Exception Illegal class passed as substitution #3693

Closed
infokomes opened this issue Nov 10, 2023 · 10 comments
Closed
Assignees
Labels
Csharp Pull requests that update .net code Go Java type:bug A broken experience TypeScript Pull requests that update Javascript code
Milestone

Comments

@infokomes
Copy link

infokomes commented Nov 10, 2023

Info:
"kiotaVersion": "1.8.2",
"language": "CSharp",
"usesBackingStore": false,
"excludeBackwardCompatible": false,
"includeAdditionalData": false,

PackageReference:

<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.7.0" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.3.0" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.1.0" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.1.1" />
<PackageReference Include="Microsoft.Kiota.Authentication.Azure" Version="1.1.1" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.1.0" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.1.0" />

Method URI return Exception:
return new Uri(UriTemplate.Expand(UrlTemplate, dictionary));

System.ArgumentException
HResult=0x80070057
Message=Illegal class passed as substitution, found ...GetFormatQueryParameterType at col:62
Source=Std.UriTemplate
StackTrace:
at Std.UriTemplate.GetSubstitutionType(Object value, Int32 col)

class :

  public enum GetFormatQueryParameterType {
       [EnumMember(Value = "metadata")]
       Metadata,
       [EnumMember(Value = "minimal")]
       Minimal,
   }

schema openapi: 3.0.3:

  - name: format
    in: query
    schema:
      type: string
      enum: [metadata, minimal]
      default:
        minimal
@github-project-automation github-project-automation bot moved this to Todo in Kiota Nov 10, 2023
@baywet baywet self-assigned this Nov 10, 2023
@baywet baywet added type:bug A broken experience Csharp Pull requests that update .net code TypeScript Pull requests that update Javascript code Go Java labels Nov 10, 2023
@baywet baywet added this to the Kiota v1.9 milestone Nov 10, 2023
@baywet
Copy link
Member

baywet commented Nov 10, 2023

Hi @infokomes,
Thanks for reporting this.
I think this is a side effect of #2490 and associated PRs.

@andreaTP std uri template is unhappy because kiota abstractions is passing it enums values or arrays of enum values.
I believe the right solution would be for kiota abstractions to do the conversion to string or array of string before passing the context to std URI template.
That's because the code symbols (enum members) and query/path parameter value can be different, and the serialization information will be needed.
In some languages, like dotnet, Std Uri Template could have access to this, but in others it's going to be sitting in a method.
Thoughts?

@andreaTP
Copy link
Contributor

Now on the phone, but, in general I agree.
The information about the string substitution to be done (I.e. EnumMember) is not known to std-uritemplate or it will very much differ across languages.
I think that doing the conversion in the kiota abstractions is the right fix here.

Just to mention, in general I would not be against enum specific handling in std-uritemplate (e.g. like we do for date formats) but, in this case, I think that the most maintainable solution is in abstractions.

@andreaTP
Copy link
Contributor

Opened std-uritemplate/std-uritemplate#91 to showcase how the enum support may look like in std-uritemplate (implemented it in C# and Java ATM).
But this is not going to fully resolve this issue as far as I understand it, and some work should be done in abstractions.

@baywet
Copy link
Member

baywet commented Nov 13, 2023

I was able to implement a fix for dotnet. I'll start looking at other languages on how it could be implemented as well.
microsoft/kiota-dotnet#148

@baywet
Copy link
Member

baywet commented Nov 13, 2023

And for Java microsoft/kiota-java#809

@baywet
Copy link
Member

baywet commented Nov 15, 2023

and the go version, gee the type system is limited microsoft/kiota-abstractions-go#120

@baywet
Copy link
Member

baywet commented Nov 22, 2023

took me a while to get to typescript because I was busy with other things but it's already supported, added a few unit tests to prevent regressions.
microsoft/kiota-typescript#967

@echolumaque
Copy link

echolumaque commented Nov 25, 2023

@baywet, hello I'm still experiencing this in .NET MAUI. I'm on .NET 8 when using enums for query parameters. How can I help to post the diagnostics?

image

Stacktrace:

image

I'm using Kiota 1.8.2:

image

I then tried this code:

public async Task GetSymptoms()
{
    var test = await _apiClient.Symptoms.GetAsync(client =>
    {
        client.QueryParameters = new SymptomsRequestBuilderGetQueryParameters
        {
            BirthYear = 2001,
            BodyPart = "head",
            Gender = "male"
        };
    });
}

It says that BodyPart and Gender is deprecated (they're both stings) and I should use enums for it. However when I run it, this exception now appears:

image

@andreaTP
Copy link
Contributor

@echolumaque can you check the version of the used libraries?

I do believe that updating to 1.7.2 Microsoft.Kiota.Abstractions should fix the issue.

@echolumaque
Copy link

@echolumaque can you check the version of the used libraries?

I do believe that updating to 1.7.2 Microsoft.Kiota.Abstractions should fix the issue.

Thanks, works like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Csharp Pull requests that update .net code Go Java type:bug A broken experience TypeScript Pull requests that update Javascript code
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants