itmannz
itmannz

Reputation: 599

swagger not showing HttpGet query parameters

I am working on asp.net core webapi(6.0) with swagger. When I run the application HttpGet method Parameters summary not visible in swagger. such as Id, StoreId, DateFrom, DateTo summary not display in swagger. Please advice

 /// <summary>
    /// Get product details.
    /// </summary>
    /// <remarks>Fetches product.</remarks>
    /// <param name="parameters"></param>
    /// <returns></returns>
    /// <response code="200">Successful Response</response>
    /// <response code="400">Bad Request</response>
    /// <response code="401">Unauthorized</response>
    /// <response code="424">Failed Dependency</response>
    /// <response code="500">Internal Server Error</response>
    public async Task<IActionResult> GetProduct([FromQuery] Parameter parameters)
        {
            var repo = await Repository.GetAllAsync(parameters);
            return Ok()
        }



public class Parameter
{
    /// <summary>
    /// Product Id
    /// </summary>
    public string? Id { get; set; }
    /// <summary>
    /// Optional Parameter. If supplied, result set will be restricted to the products from the supplied store alone
    /// </summary>
    public int? StoreId { get; set; }
    /// <summary>
    /// Optional Parameter.
    /// </summary>
    public DateTime? DateFrom { get; set; }
    /// <summary>
    /// Optional Parameter.
    /// </summary>
public DateTime? DateTo { get; set; }
}

Upvotes: 6

Views: 5079

Answers (3)

mitesh keswani
mitesh keswani

Reputation: 173

For me all 3 QueryParam were not showing up. so, I realised, I duplicated the name(s). On fixing that all QueryParam(s) were being shown on swagger. For instance::

@NotNull @QueryParam("rId") Long rId, @NotNull @QueryParam("pId") Long pId, @NotNull @QueryParam("pId") Integer cpId //repeater here, fixing this fixed the issue for me.

Additionally, the annotation were of jersey, you could check on same lines.

Upvotes: 0

Dev_New
Dev_New

Reputation: 1

This worked for me, -Generate xml doc file other project true as well where you have xml documentation for parameters.

  • you can add as many xml documentation files in StartUp.cs using .IncludeXmlComments (if 2, add two .IncludeXmlComments)
  • Swagger will combine all files and show the result.

Upvotes: 0

Jason Pan
Jason Pan

Reputation: 22029

XML comments can be used to add parameters to the swagger UI.

First enable GenerateDocumentationFile in your .csproj file to generate xml documentation file, which will be used in swagger json generation.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);1591</NoWarn>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

</Project>

Then config the swagger service to IncludeXmlComments from the genreated xml docs file:

//builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(options =>
{
    // using System.Reflection;
    var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});

the result: enter image description here

Upvotes: 3

Related Questions