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

Kiota does not support array of errors #3827

Open
0xced opened this issue Nov 28, 2023 · 2 comments
Open

Kiota does not support array of errors #3827

0xced opened this issue Nov 28, 2023 · 2 comments
Labels
enhancement New feature or request generator Issues or improvements relater to generation capabilities. help wanted Issue caused by core project dependency modules or library
Milestone

Comments

@0xced
Copy link
Contributor

0xced commented Nov 28, 2023

Let's say your OpenAPI operation error looks like that:

"404" : {
  "description" : "Status Code 404",
  "content" : {
    "application/json" : {
      "schema" : {
        "type" : "array",
        "items" : {
          "$ref" : "#/components/schemas/RestApiError"
        }
      }
    }
  }
}

And the RestApiError schema looks like that:

"RestApiError" : {
  "type" : "object",
  "properties" : {
    "errorCode" : {
      "type" : "string"
    },
    "message" : {
      "type" : "string"
    }
  }
}

The generated RestApiError class (which inherits from ApiException) can't be deserialized properly.

Here's a sample (actual) 404 error response:

[{"errorCode":"NOT_FOUND","message":"Some error message"}]

When thrown, the ErrorCode is null and the Message is Exception of type 'MySdk.Models.RestApiError' was thrown.

I'm not sure how Kiota should handle this situation where the error response is an array instead of an object and how the error class should be generated but currently the useful information (errorCode + message) is lost.

I was able to see the problem in the AssignFieldValues method of Microsoft.Kiota.Serialization.Json.JsonParseNode (which seems to be source generated)

private void AssignFieldValues<T>(T item) where T : IParsable
{
    if(_jsonNode.ValueKind != JsonValueKind.Object) return; // 👈 _jsonNode.ValueKind == JsonValueKind.Array ⇒ no deserialization occurs at all
    IDictionary<string, object>? itemAdditionalData = null;
    if(item is IAdditionalDataHolder holder)
@github-project-automation github-project-automation bot moved this to Todo in Kiota Nov 28, 2023
@baywet baywet added enhancement New feature or request help wanted Issue caused by core project dependency modules or library generator Issues or improvements relater to generation capabilities. labels Nov 28, 2023
@baywet baywet added this to the Backlog milestone Nov 28, 2023
@baywet
Copy link
Member

baywet commented Nov 28, 2023

Hi @0xced,
Thanks for using kiota and for reaching out.
This is a limitation of the current design.
See #2435 for additional details (although that issue is about enums, the limitations are the same)

@0xced
Copy link
Contributor Author

0xced commented Dec 21, 2023

It turns out that even though there could be several errors, the API only ever returns a single error in the array. So I was able to workaround this limitation by tweaking the CreateFromDiscriminatorValue method after the code is generated by kiota.

Generated code:

public static RestApiError CreateFromDiscriminatorValue(IParseNode parseNode) {
    _ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
    return new RestApiError();
}

Tweaked code:

public static RestApiError CreateFromDiscriminatorValue(IParseNode parseNode) {
    _ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
    var errors = parseNode.GetCollectionOfObjectValues<RestApiError>(_ => new RestApiError());
    return errors.FirstOrDefault() ?? new RestApiError();
}

I also added an override to the Message property in order to have a meaningful exception.

public override string Message => MessageEscaped ?? base.Message;

If the API ever returns multiple errors then at least I'll get the first error, which is already better than no error at all.

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. help wanted Issue caused by core project dependency modules or library
Projects
Status: New📃
Development

No branches or pull requests

2 participants