kiota icon indicating copy to clipboard operation
kiota copied to clipboard

Enums in error responses lead to invalid code

Open nascosto opened this issue 1 year ago • 1 comments

When returning enums from an error like this

{
  "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)

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
        public async Task<HealthStatus?> GetAsync(Action<LiveRequestBuilderGetRequestConfiguration>? requestConfiguration = default, CancellationToken cancellationToken = default) {
#nullable restore
#else
        public async Task<HealthStatus?> GetAsync(Action<LiveRequestBuilderGetRequestConfiguration> requestConfiguration = default, CancellationToken cancellationToken = default) {
#endif
            var requestInfo = ToGetRequestInformation(requestConfiguration);
            var errorMapping = new Dictionary<string, ParsableFactory<IParsable>> {
                {"503", HealthStatus?.CreateFromDiscriminatorValue},
            };
            return await RequestAdapter.SendPrimitiveAsync<HealthStatus?>(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.

nascosto avatar Mar 20 '23 17:03 nascosto