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

Swagger renders endpoint parameter in path with a wrong type #18479

Closed
1 task done
lommez opened this issue Dec 14, 2023 · 4 comments · Fixed by #18517
Closed
1 task done

Swagger renders endpoint parameter in path with a wrong type #18479

lommez opened this issue Dec 14, 2023 · 4 comments · Fixed by #18517

Comments

@lommez
Copy link

lommez commented Dec 14, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Description

In my application, it has an endpoint just like the code below:

    HttpGet("get-by-origin/{origin}")]
    public IActionResult GetByOrigin(Origin origin)
    {
        return Ok();
    }

Here is the enum, an enum converter and where it is add to the JsonOptions:

    public enum Origin
    {
        Inside = 1,
        Outside = 2
    }
    
    public class OriginConverter : JsonConverter<Origin>
    {
        public override Origin Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.String)
            {
                throw new JsonException();
            }

            if (Enum.TryParse(reader.GetString(), ignoreCase: true, out Origin enumValue))
            {
                return enumValue;
            }
            else
            {
                throw new JsonException();
            }
        }

        public override void Write(Utf8JsonWriter writer, Origin value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString());
        }
    }
    
    // That method is called when the services are configured 
    private void ConfigureJsonOptions(ServiceConfigurationContext context)
    {
        context
            .Services
            .AddOptions<JsonOptions>()
            .Configure<IServiceProvider>((options, serviceProvider) =>
            {
                options.JsonSerializerOptions.Converters.Add(new OriginConverter());
            });
    }

And in this image, is where the Swagger is configured:

image

But the endpoint parameter on the path is configured as int32 instead of string. And even the select is filled with a value, it is considered as an invalid form.

image

I´ve tried to do it in complete new application without using abp and works perfectly.
It looks like that converters added to the JSON options has no effect.
Is something that I´m missing or this is a bug?

Reproduction Steps

No response

Expected behavior

No response

Actual behavior

No response

Regression?

No response

Known Workarounds

No response

Version

7.4.4

User Interface

Angular

Database Provider

EF Core (Default)

Tiered or separate authentication server

Separate Auth Server

Operation System

Windows (Default)

Other information

No response

@lommez lommez added the bug label Dec 14, 2023
@maliming
Copy link
Member

hi

Please share a minimal project. Thanks.

@maliming maliming removed the bug label Dec 18, 2023
@lommez
Copy link
Author

lommez commented Dec 19, 2023

Hi, @maliming
i´ve provided a sample application to show that Swagger is not working as expected.
At least in this demo the type on the endpoint path is in correct type, as string.
But Swagger form didn´t recognize that field is filled.

image

Here is the url to my project

https://github.com/lommez/abp-swagger

@maliming
Copy link
Member

hi

context.Services.AddAbpSwaggerGenWithOAuth(
            configuration["AuthServer:Authority"]!,
            new Dictionary<string, string>
            {
                    {"AbpSwagger", "AbpSwagger API"}
            },
            options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "AbpSwagger API", Version = "v1" });
                options.DocInclusionPredicate((docName, description) => true);
                options.CustomSchemaIds(type => type.FullName);
                options.SchemaFilter<MyAbpSwashbuckleEnumSchemaFilter>();
                options.HideAbpEndpoints();
            });
public class MyAbpSwashbuckleEnumSchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        if (context.Type.IsEnum)
        {
            schema.Enum.Clear();
            schema.Type = "string";
            schema.Format = null;
            Enum.GetNames(context.Type)
                .ToList()
                .ForEach(name => schema.Enum.Add(new OpenApiString($"{name}")));
        }
    }
}

@lommez
Copy link
Author

lommez commented Dec 19, 2023

Thanks @maliming !
It works perfectly!

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

Successfully merging a pull request may close this issue.

2 participants