Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger json is showing unwanted schemas such as default objects metadata in .net core web api

I am using swagger(Swashbuckle.AspNetCore 5.4.1) in asp.net Core web API project and its generating JSON with a lot of unwanted details in components as below however I was expecting it should only show useful metadata like enum details specific to my API . Please tell me if I am doing anything wrong here while setting up the swagger

enter image description here

Below is the code for Startup

    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);
            });

        }
like image 975
V J Avatar asked Sep 07 '25 09:09

V J


1 Answers

It may come from controllers endpoints in/out parameters.

like image 124
Tomasz Anuszkiewicz Avatar answered Sep 10 '25 07:09

Tomasz Anuszkiewicz