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

Tags are sorted in 'random' order in OpenAPI. #882

Closed
bmeesters opened this issue Aug 11, 2021 · 9 comments · Fixed by #883
Closed

Tags are sorted in 'random' order in OpenAPI. #882

bmeesters opened this issue Aug 11, 2021 · 9 comments · Fixed by #883

Comments

@bmeesters
Copy link
Contributor

bmeesters commented Aug 11, 2021

Currently the endpoints documentation contains a map of tag name to PathItem:

final class OpenApi private (
    val info: Info,
    val paths: Map[String, PathItem],    // <----
    val components: Components
)

Since this uses a map underneath the tags appear random in the resulting openAPI (see for example https://otm5.opentripmodel.org/, which is btw a nice example of what endpoints4s can achieve!). This order also changes if a new tag is added since Maps are unordered in their keys.

I think it would be quite nice if we can sort the keys, for example alphabetically, so it looks a little more tight. I would also be willing to implement this feature myself.

My suggestion would be to add an optional 'sorting' to the OpenApi class that is used when present.

@julienrf
Copy link
Member

I wonder if this is more the responsibility of the OpenAPI UI generator instead? I think the current implementation keeps the paths in the order they are passed to the call to openApi(...)(endpoint1, endpoint2, ..., endpointN) (but we have no test for that, so I’m not sure).

@bmeesters
Copy link
Contributor Author

As far as I can see the openApi(...)(endpoint1, endpoint2, ..., endpointN) will group the endpoints by tag using a simply groupBy. The result is then later processed sequentially by simply calling .iterator on the scala.Map and processed in that order. In any case the UI we use simply uses the order it is present in the JSON.

Would you be open for a PR? I can make something locally and confirm I can ensure an user defined order (defaulting to just a String Ord).

@bmeesters
Copy link
Contributor Author

This is the piece of code (in endpoints4s.openapi.EndpointsWithCustomErrors#openApi) where the ordering changes:

endpoints
        .groupBy(_.path)
        .map { case (k, es) =>
        ...
        }

@julienrf
Copy link
Member

Right, the groupBy breaks the ordering!

Do you think it would be enough to just keep the order in which the endpoints are passed to the openApi call? It would be simpler to implement it with scala/scala-library-next#51, but we can do it manually in the meantime.

@bmeesters
Copy link
Contributor Author

The original order would be good enough for me (we just pass it alphabetically ourselves).

So what is your proposal? Use LinkedHashMap to keep the original insertion order or sort in another way?

@julienrf
Copy link
Member

Yes, sorry if that was not clear, but if keeping the original insertion order works for you that would be a simple solution (we could make it more sophisticated in the future if needed). So yes, you will have to use a LinkedHashMap and to implement the groupBy operation by yourself (so that it returns a LinkedHashMap instead of a default Map).

@bmeesters
Copy link
Contributor Author

bmeesters commented Aug 12, 2021

NP :) I still think we might misunderstand each other. To be clear, we need a PR in endpoints4s to make this possible. The Map implementation in the openApi method has to change to use LinkedHashMap instead. I think everything else will work from there on out. Since the JSON creation (also in endpoints4s) will automatically use the original order since that map is used under the hood. We, as the users, never get to work with the map itself.

Shall I make a PR? I think it is just a one-liner

@julienrf
Copy link
Member

I thought we could change the implementation of openApi without changing the signatures of other methods, but I might be wrong. In any case yes, a PR is welcome :)

@bmeesters
Copy link
Contributor Author

Yes, I think so too, I will try 😄

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

Successfully merging a pull request may close this issue.

2 participants