microsoft/kiota

Kiota does not support array of errors

Open

#3,827 opened on Nov 28, 2023

 (2 comments) (0 reactions) (0 assignees)C# (326 forks)auto 404
enhancementgeneratorhelp wanted

Repository metrics

Stars
 (3,783 stars)
PR merge metrics
 (PR metrics pending)

Description

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)

Contributor guide