microsoft / microsoft/kiota

Enums in error responses lead to invalid code

Open
#2,435 2 comments 0 reactions 0 assignees View on GitHub
enhancement generator
Dominant language
C#
Stars
3.8k
Forks
333
Avg merge
16h 29m
Merged PRs (30d)
116

Description

When returning enums from an error like this

```json
{
"openapi": "3.0.1",
"info": {
"title": "N/A",
"version": "0.0.0"
},
"paths": {
"/health/live": {
"get": {
"tags": [
"Health"
],
"operationId": "GetLiveHealthStatus",
"responses": {
"200": {
"description": "Success",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/HealthStatus"
}
}
}
},
"503": {
"description": "Server Error",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/HealthStatus"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"HealthStatus": {
"enum": [
"Unhealthy",
"Degraded",
"Healthy"
],
"type": "string"
}
}
}
}
```

it generates invalid C# (at least, haven't tried with the other languages)

```csharp
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
public async Task GetAsync(Action? requestConfiguration = default, CancellationToken cancellationToken = default) {
#nullable restore
#else
public async Task GetAsync(Action requestConfiguration = default, CancellationToken cancellationToken = default) {
#endif
var requestInfo = ToGetRequestInformation(requestConfiguration);
var errorMapping = new Dictionary> {
{"503", HealthStatus?.CreateFromDiscriminatorValue},
};
return await RequestAdapter.SendPrimitiveAsync(requestInfo, errorMapping, cancellationToken);
}
```

the error given is `'HealthStatus' is a type, which is not valid in the given context`. There is no CreateFromDiscriminatorValue to call.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the supplied OpenAPI document and inspect the generator path that builds error mappings for enum responses in the C# output. Done means the generated client compiles and correctly represents the 503 enum error response without referencing a nonexistent CreateFromDiscriminatorValue member.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.