Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums in error responses lead to invalid code #2435

Open
nascosto opened this issue Mar 20, 2023 · 1 comment
Open

Enums in error responses lead to invalid code #2435

nascosto opened this issue Mar 20, 2023 · 1 comment
Labels
enhancement New feature or request generator Issues or improvements relater to generation capabilities.
Milestone

Comments

@nascosto
Copy link

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.

@baywet baywet self-assigned this Mar 20, 2023
@baywet baywet added enhancement New feature or request generator Issues or improvements relater to generation capabilities. and removed Needs: Triage 🔍 labels Mar 20, 2023
@baywet baywet added this to Kiota Mar 20, 2023
@github-project-automation github-project-automation bot moved this to Todo in Kiota Mar 20, 2023
@baywet baywet added this to the Kiota v2 milestone Mar 20, 2023
@baywet
Copy link
Member

baywet commented Mar 20, 2023

Hi @nascosto
Thanks for your interest in Kiota and for reporting this.
This is currently a limitation. The error mapping was built with object types as error responses. It won't handle enum or scalar values since the error type needs to derive from exception/error so it can be thrown.
What could be done would be to project an additional type in those cases, with a "value" property which would map to the enum/scalar type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request generator Issues or improvements relater to generation capabilities.
Projects
Status: New📃
Development

No branches or pull requests

2 participants