Kiota does not support array of errors
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 333
- Avg merge
- 16h 29m
- Merged PRs (30d)
- 116
Description
Let's say your OpenAPI operation error looks like that:
```json
"404" : {
"description" : "Status Code 404",
"content" : {
"application/json" : {
"schema" : {
"type" : "array",
"items" : {
"$ref" : "#/components/schemas/RestApiError"
}
}
}
}
}
```
And the `RestApiError` schema looks like that:
```json
"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:
```json
[{"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)
```csharp
private void AssignFieldValues(T item) where T : IParsable
{
if(_jsonNode.ValueKind != JsonValueKind.Object) return; // 👈 _jsonNode.ValueKind == JsonValueKind.Array ⇒ no deserialization occurs at all
IDictionary? itemAdditionalData = null;
if(item is IAdditionalDataHolder holder)
```
Contributor guide
Research direction
Start in Microsoft.Kiota.Serialization.Json.JsonParseNode, especially the source-generated AssignFieldValues method, and reproduce the issue with the array-shaped 404 response shown here. Trace how generated RestApiError inherits from ApiException and determine the expected handling for array error payloads. Done means the sample response preserves errorCode and message when the exception is thrown.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, json
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100