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

Is it possible to order(sort) request methods (POST,GET,PUT,DELETE) in Swagger-UI using swagger-jaxrs2 with Sring boot + Jersey controller API? #3695

Closed
tGuichin opened this issue Sep 9, 2020 · 13 comments

Comments

@tGuichin
Copy link

tGuichin commented Sep 9, 2020

Currently the swagger-jaxrs2 generated Swagger-UI has no order for each "GET", "PUT", "POST"... etc method. It randomly spits the endpoints out as it pleases. Is there a way to sort them? I tried using @JsonPropertyOrder({"POST","GET","PUT","DELETE"}) , which is the order I would like them in, but to no success. Also tried @JsonPropertyOrder(alphabetic = true) . This annotation was included in the Jersey Controller class.

I'm using:

<dependency>
	<groupId>io.swagger.core.v3</groupId>
	<artifactId>swagger-jaxrs2</artifactId>
	<version>2.1.4</version>
</dependency>
@tGuichin tGuichin changed the title Is it possible to order(sort) request methods? Is it possible to order(sort) request methods (POST,GET,PUT,DELETE) in Swagger-UI using swagger-jaxrs2 with Sring boot + Jersey controller API? Sep 9, 2020
@swarth100
Copy link

I think you need to configure swagger core's Json object mapper

Does this work:

Declare this anywhere

/* Mixin to ensure custom ordering of certain properties */
@JsonPropertyOrder(alphabetic = true)
public abstract static class AlphabeticallySorted {}

Configure the Json object mapper for API Operation class ordering

import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.Operation;

...

Json.mapper().addMixIn(Operation.class, AlphabeticallySorted.class);

This way you can configure the ordering of any part of the OAS Schema

Also if you wish for keys in unordered maps to be sorted:

Json.mapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);

@tGuichin
Copy link
Author

I'm not fully sure how to implement what you mentioned, but I will play around with it to see if I can figure it out when I get some time. Thank you, @swarth100 .

@tGuichin
Copy link
Author

tGuichin commented Sep 11, 2020

@swarth100 So your solution worked, somewhat. I only added Json.mapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); and it sorted the Swagger-UI endpoints by path (/user/name/...) instead of by request method(GET/PUT/POST/DELETE). The Json.mapper().addMixIn piece have no affect on the sorting. Any ideas on if it's possible to sort by method? I tried Json.mapper().addMixIn(HttpMethod.class, AlphabeticallySorted.class); with no luck. Thanks in advance.

@swarth100
Copy link

Does adding the AlphabeticallySorted.class MixIn for io.swagger.v3.oas.models.PathItem do the trick?

@tGuichin
Copy link
Author

Seems like a dud. Json.mapper().addMixIn seems to have no effect on anything.

@tGuichin
Copy link
Author

I think this is because the Request methods are javax components and not swagger oas components. I'm trying to target the wrong class.

@swarth100
Copy link

I am unable to replicate your issue.

I forked the latest dropwizard-example to test things out. The branch with the changes is check-jaxkson-mixin

You can see my changes here https://github.com/swarth100/dropwizard/compare/master...swarth100:check-jaxkson-mixin

If I set

Json.mapper().addMixIn(io.swagger.v3.oas.models.PathItem.class, AlphabeticallySorted.class);

The serialised spec is produced with the correct path item property ordering.

If I annotate AlphabeticallySorted with @JsonPropertyOrder(alphabetic = true) I obtain:

    "/people" : {
      "get" : {
        ...
      },
      "post" : {
        ...
      },
      "put" : {
        ...
      }
    },

If I annotate AlphabeticallySorted with @JsonPropertyOrder({"post","get","put","delete"}) I obtain:

    "/people" : {
      "post" : {
        ...
      },
      "get" : {
        ...
      },
      "put" : {
        ...
      }
    },

If I do not annotate AlphabeticallySorted I obtain:

    "/people" : {
      "get" : {
        ...
      },
      "put" : {
        ...
      },
      "post" : {
        ...
      }
    },

I hope this helps


I think this is because the Request methods are javax components and not swagger oas components. I'm trying to target the wrong class.

Unless I misunderstood something all JAX-RS annotations will be scanned by swagger-core and they will be converted into the OAS class structure. In particular HTTP methods will be converted into an instance of io.swagger.v3.oas.models.PathItem.class

@tGuichin
Copy link
Author

Do you see the same in Swagger UI? I will try again, but I did everything you did and it sorts by path instead of method.

@tGuichin
Copy link
Author

@swarth100 I'm pretty sure I'm doing the same thing you are, but I'm just not getting the results in the Swagger-UI. Everything is being sorted by path by Json.mapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); I tried following the dropwizard example, but that was no good for me either.

@swarth100
Copy link

swarth100 commented Sep 15, 2020

Hey @tGuichin,

As the issue only seems to occur when using Swagger UI, and as the spec's order is indeed configurable I am driven to think the problem might reside within some part of the spring/swagger UI DTOs. I think you might find the best guidance to your problems on their issue boards.

I'm sure you've come across this issue already, but I think it could be a start (even though it's JS): swagger-api/swagger-ui#4158 (comment)

I believe swagger-UI ordering and configuration must reside in the instantiation of the swagger ui bundle

EDIT: It seems like support for operationsSorter was added to spring in 2019: https://github.com/springdoc/springdoc-openapi/blob/cf8fec38c482c5bf154d76b28b4c4b6a3f058366/CHANGELOG.md#added-28

@tGuichin
Copy link
Author

springdoc-openapi is different from swagger-jaxrs2 as far as I know. I will do some more research and see what else I can find. Thank you.

@tGuichin
Copy link
Author

Now that I look at the UI closer, it looks like the endpoints are sorted first alphabetically by path, then by method GET, POST, PUT, DELETE in that order.

@frantuma
Copy link
Member

frantuma commented Dec 3, 2020

Along with the solutions mentioned in comments in this ticket, a solution has been implemented in #3740, see PR comment for details and usage scenarios.

Additionally the current issue discussed seems to be related to swagger UI and should be reported there.
Closing this ticket, any further issues in this area should be reported in a new or not closed ticket.

@frantuma frantuma closed this as completed Dec 3, 2020
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

3 participants