Swagger json在.net core web api中显示了不需要的模式,如默认对象元数据。

问题描述 投票:0回答:1

我在asp.net Core web API项目中使用swagger(Swashbuckle.AspNetCore 5.4.1),它生成的JSON中包含了很多不需要的细节,如下图所示,但我希望它只显示有用的元数据,比如我的API特有的枚举细节。如果我在设置swagger时做错了什么,请告诉我。

enter image description here

以下是启动的代码

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                app.UseStaticFiles();

                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }

                // Enable middleware to serve generated Swagger as a JSON endpoint.
                app.UseSwagger();

                // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
                // specifying the Swagger JSON endpoint.
                app.UseSwaggerUI(su =>
                {
                    su.SwaggerEndpoint("/swagger/general/swagger.json", "Common API");
                    su.RoutePrefix = string.Empty;
                });


                app.UseHttpsRedirection();

                app.UseRouting();

                app.UseAuthorization();

                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }

  public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers()
                 .AddJsonOptions(options =>
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));



            services.AddSwaggerGen(c
               =>
            {
                c.SwaggerDoc("general", new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Title = "Common API",
                    Version = "General Purpose",
                    Description = "TEST API",
                    Contact = new Microsoft.OpenApi.Models.OpenApiContact
                    {
                        Name = "VJ-TESTER",
                        Email = "[email protected]",
                    },
                });

                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

        }
swagger asp.net-core-webapi swagger-ui swashbuckle swashbuckle.aspnetcore
1个回答
0
投票

它可能来自于控制器端点inout参数。

© www.soinside.com 2019 - 2024. All rights reserved.