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

Controllers not sorted alphabetically when using includeControllerXmlComments #2162

Closed
lonix1 opened this issue Jul 11, 2021 · 9 comments
Closed

Comments

@lonix1
Copy link

lonix1 commented Jul 11, 2021

Environment

aspnet: 5.0.301
Swashbuckle.AspNetCore: 6.1.4

Background

I created a minimal swashbuckle implementation. All I have is:

o.IncludeXmlComments(path, includeControllerXmlComments: true)
o.CustomSchemaIds(x => x.FullName);

And I added <summary> xml comments to my controllers.

Problem

If I use includeControllerXmlComments:false then the controllers are sorted alphabetically.

But if I use includeControllerXmlComments:true then the controllers are not sorted alphabetically.

@lscorcia
Copy link

+1 for this.
As a workaround, you can use the following document filter:

/// <summary>
/// Workaround for https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2162
/// When adding XML controller descriptions to the swagger description document,
/// controllers are listed out of alphabetical order.
///
/// This filter explicitly reorders them.
/// </summary>
public class TagReorderDocumentFilter : IDocumentFilter
{
  /// <summary>
  /// Allows customization of the swagger description document
  /// </summary>
  /// <param name="swaggerDoc">The generated swagger description document</param>
  /// <param name="context">Context information</param>
  public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
  {
    swaggerDoc.Tags = swaggerDoc.Tags
      .OrderBy(tag => tag.Name)
      .ToList();
  }
}

To apply the filter add it during Swagger initialization:

builder.Services.AddSwaggerGen(config => {
  ...

  // Workaround: After adding XML controller descriptions, they are listed out of alphabetical order
  config.DocumentFilter<TagReorderDocumentFilter>();
});

@lonix1
Copy link
Author

lonix1 commented Mar 2, 2022

In newer versions, this seems to work without the need for any customisation.

ASP.NET Core 5.0.11
Swashbuckle.AspNetCore 6.2.3

@lonix1 lonix1 closed this as completed Mar 2, 2022
@lscorcia
Copy link

lscorcia commented Mar 8, 2022

Unfortunately the issue is still present. ASP.NET Core 6.0 + Swashbuckle.AspNetCore 6.3.0. I will open a new issue.

@lonix1
Copy link
Author

lonix1 commented Mar 8, 2022

Hey @lscorcia I've reopened for you.

@lonix1 lonix1 reopened this Mar 8, 2022
@lscorcia
Copy link

lscorcia commented Mar 8, 2022

Thanks @lonix1 ! I prepared a small repro project at https://github.com/lscorcia/UnsortedControllersRepro . Please let me know if you need any additional information.

@lonix1
Copy link
Author

lonix1 commented Mar 8, 2022

@lscorcia Sorry to disappoint but I'm not a repo member... I was the one who reported the original problem. 😃 Hope you get your problem fixed.

@yingxinj
Copy link

@lscorcia I think this is a side-effect of the fact that if you add includeControllerXmlComments:true only controllers with XmlComments are added to swaggerDoc.Tags object in the document filter example above, so that the controllers with XmlComments and those without will be sorted separately. My open PR linked above resolves this issue, please let me know if it is helpful.

@martincostello
Copy link
Collaborator

#2565 should fix this.

@martincostello
Copy link
Collaborator

Duplicates #1757

@martincostello martincostello closed this as not planned Won't fix, can't repro, duplicate, stale May 6, 2024
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

No branches or pull requests

4 participants