Burgyn / MMLib.SwaggerForOcelot

This repo contains swagger extension for ocelot.
MIT License
352 stars 94 forks source link

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

Open q00Dree opened 1 year ago

q00Dree commented 1 year ago

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 commented 2 months ago

Hello there,

Problem with Ocelot core library, workaround here https://github.com/ThreeMammals/Ocelot/issues/1510