microsoft/kiota

Kiota does not support array of errors

開放

#3,827 建立於 2023年11月28日

 (2 則留言) (0 個反應) (0 位負責人)C# (326 個分叉)auto 404
enhancementgeneratorhelp wanted

倉庫指標

星標
 (3,783 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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)

貢獻者指南