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

Ocelot Gateway controllers return enum values as integer instead of string. #279

Open
q00Dree opened this issue Oct 24, 2023 · 1 comment

Comments

@q00Dree
Copy link

q00Dree commented Oct 24, 2023

Hello, I have my own controllers defined in Ocelot ApiGateway and faced problem described in issue title. I encounter this problem only when I use the controller methods that are defined in Ocelot APIGateway project. Models from proxied services return values for enum as strings. The enum values on the swagger specification page for the Ocelot Api Gateway also have string values.

Is there any way to correct this behavior?

Startup.cs

services.AddControllers()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
            });

services.AddOcelot(Configuration).AddPolly();
services.AddSwaggerForOcelot(Configuration);

services.AddSwaggerGen(c =>
{
    c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
    {
        Description = "JWT Authorization header using the Bearer scheme. Example: Bearer \"{token}\"",
        In = ParameterLocation.Header,
        Name = HeaderNames.Authorization,
        Type = SecuritySchemeType.Http,
        Scheme = JwtBearerDefaults.AuthenticationScheme
    });

    c.OperationFilter<SecurityRequirementsOperationFilter>();
    c.DescribeAllParametersInCamelCase();
    c.SupportNonNullableReferenceTypes();
    c.UseAllOfToExtendReferenceSchemas();
    c.EnableAnnotations();

    c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "ApiGateway.Server.xml"), true);
    c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "ApiGateway.DataTransfer.Http.xml"));
});

Routes/ocelot.SwaggerEndPoints.json

{
  "SwaggerEndPoints": [
    {
      "Key": "apigateway",
      "TransformByOcelotConfig": false,
      "Config": [
        {
          "Name": "ApiGateway.Server API",
          "Version": "1.0",
          "Url": "http://localhost/swagger/v1/swagger.json"
        }
      ]
    }
    // Configurations to microservices...
  ]
}

Values for enum in the swagger specification.
image

The result of executing the endpoint in Swagger
As you can see in Reponse Body enum value is integer, but in Example Value is string.
image

@EynsherKiel
Copy link

Hello there,

Problem with Ocelot core library, workaround here
ThreeMammals/Ocelot#1510

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants