Skip to content

Sort GroupedOpenApi by name #398

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

Closed
stefan-huettemann opened this issue Feb 2, 2020 · 3 comments
Closed

Sort GroupedOpenApi by name #398

stefan-huettemann opened this issue Feb 2, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@stefan-huettemann
Copy link

Hi,

Problem: unsorted groups

when defining multiple groups as:

 @Bean
    public GroupedOpenApi openApiServices() {
        return GroupedOpenApi.builder()
                             .setGroup("Group Name A")
                             .pathsToMatch("/some/path/pattern/**")
                             .build();
    }

it seems to result in an arbitrary ordered list of groups in swagger config, as in example:

"urls": [
    {
      "url": "/sasmd/v3/api-docs/9-Actuator",
      "name": "9-Actuator"
    },
    {
      "url": "/sasmd/v3/api-docs/2-Security",
      "name": "2-Security"
    },
    {
      "url": "/sasmd/v3/api-docs/1-Other",
      "name": "1-Other"
    }
  ]

I would like as the default behavior to sort the groups by name.

Solution

A possible solution would be to change org/springdoc/core/SwaggerUiConfigProperties.java:376 (see: 26d58f2):

swaggerUrls = swaggerUrls.stream().filter(elt -> StringUtils.isNotEmpty(elt.getUrl())).sorted().sorted(Comparator.comparing(SwaggerUrl::getName)).collect(Collectors.toList());

Alternatives

maybe also a "order" attribute on the GroupedOpenApi.builder() would be nice to have.

Best,
-Stefan

P.S.: if you like the idea and would prefer a pull request, let me know. Thanks.

@bnasslahsen
Copy link
Collaborator

Hi @stefan-huettemann,

The groups will be sorted by name (natural order) from the next release.
By default, the sort order will be ascending.

If you need to change the order, the following property is introduced:

springdoc.swagger-ui.groups-order=DESC

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
@stefannawrath
Copy link

I figured out, the sort is based on the name, not on Display-Name. For me, it makes more sense to sort the displayed group name.

@johncurrier
Copy link

For some reason AbstractSwaggerUiConfigProperties.Direction doesn't have public access, so this technique can't be used programmatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants