swagger.json paths and definitions are empty. No operations defined in spec
Asked Answered
I

5

6

I am developing a .netcore web application. I am using of swagger and I've made all the necessary adjustments. Unfortunately it does not work and I just see No operations defined in spec! in the swagger output page.

The swagger file with /swagger/v1/swagger.json has the following content:

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "Something"
  },
  "paths": {},
  "definitions": {}
}

I want to see my controllers and their actions in swagger output page.

Isoelectronic answered 12/1, 2019 at 13:21 Comment(0)
I
4

after some research i was found that my problem was about using swagger along with OData in .NetCore2.1. i found a solution for this problem.

first i added two following Nuget packages:

Swashbuckle.AspNetCore
Swashbuckle.AspNetCore.Annotations

then, i added following codes in Startup.cs

services.AddMvc(options => {
                foreach (var outputFormatter in 
options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => 
_.SupportedMediaTypes.Count == 0))
                {
                    outputFormatter.SupportedMediaTypes.Add(new 
MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }
                foreach (var inputFormatter in 
options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => 
_.SupportedMediaTypes.Count == 0))
                {
                    inputFormatter.SupportedMediaTypes.Add(new 
MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

the, i added following code line in controllers:

[ApiExplorerSettings(IgnoreApi = false)] 

please note that it was worked for me but it may be need more research for eventually side effects

Isoelectronic answered 13/1, 2019 at 10:15 Comment(0)
A
0

Sounds like you need to add some routes in the controller. With rest services you will need to add them separate from what was built automatically by Visual Studio. for example // GET: Items [HttpGet] [Route("/items")] ...rest of function... this will give your swagger a reference to what it does when you click the button.

Adjunction answered 18/11, 2021 at 22:46 Comment(0)
S
0

I am on .Net 7 and Swagger 6.5. All I needed to do was add the following before the builder.Services.AddSwaggerGen();

builder.Services.AddControllersWithViews();
Samson answered 19/10, 2023 at 12:58 Comment(0)
D
-1

you need to enable XML Documentation file under project obtions => Build tab.

Then you need to read this file through swagger so that swagger can create documentation from it.

private static string[] XmlCommentsFilePath
{
    get
    {
        var basePath = PlatformServices.Default.Application.ApplicationBasePath;

        var apiDocFile = typeof(Startup).GetTypeInfo().Assembly.GetName().Name + ".xml";
        var apiPath = Path.Combine(basePath, apiDocFile);

        return new[] {apiPath};

    }
}

In ConfigureServices

services.AddSwaggerGen(options =>
{ 
    var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();

    // add a swagger document for each discovered API version
    provider.ApiVersionDescriptions.ForEach(x => options.SwaggerDoc(x.GroupName, CreateInfoForApiVersion(x)));

    ....
});
Debbi answered 12/1, 2019 at 13:55 Comment(1)
thank you for your response. i used it before in another project and swagger automatically read all of actions without any XML file. i tried XML file from project properties->Build but it did not workIsoelectronic
V
-1

Please add a in contoller any method like a then showing swagger methods

  [HttpGet]
    [EnableQuery]
    public IQueryable<int> Get()
    {
        return 1;
    }
Vorster answered 3/11, 2021 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.