microsoft/kiota

Kiota does not support array of errors

Aperta

#3827 aperta il 28 nov 2023

 (2 commenti) (0 reazioni) (0 assegnatari)C# (326 fork)auto 404
enhancementgeneratorhelp wanted

Metriche repository

Star
 (3783 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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)

Guida contributor