-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Manual order of paths is broken for no reason #1151
Comments
Whether we do it or not, you need to keep in mind there is just no ordering supported anyways. Everything is represented as a JSON object and not as a JSON array, and by definition, there's no order of properties within a JSON object and they can be parsed differently by different parsers. If you want to control the order in the UI, you'd need to write your own sorter for it. That said, it doesn't mean we won't change the sorting behavior in swagger-core, I'd just recommend not relying on it as the behavior you expect is simply not guaranteed in any way. |
Thank you for your quick reply! I entirely understand that parsing logic could be tricky and does not have to be sequential, which would then break the originally defined order. I also understand that Swagger Spec which defines map to keep paths by definition does not guarantees the order in general. This makes the sorting logic from the getter even more confusing. However the proposed simplification from my message above has solved the issue for swagger-codegen project and out-coming JSON had the same order of paths as original YAML. The swagger-ui project that I'm using tends also to respect the order of paths in JSON file. But I don't like to have a separate branch / fork for this small but important change. It would be great if the getter could be simplified in your original project, unless it breaks something that I'm not aware of. Unfortunately I could not track down the reason for paths sorting in the commit history (neither in comments), and I might be missing some important reasons to sort the paths. |
I believe the sorting was added because, given it is a map, the order would change just fetching the items twice in a row. Since the maps are unordered, I think it's best to leave this as such. I do suggest using tags if you want to organize your resources better. Please reopen if you strongly disagree and we can try to find some sort of solution. |
@fehguy thank you for your reply! I see your point. But if you look at the implementation of This could have worked with YAML parser => JSON generator (I tested it). However the On the other hand I do use tags in my descriptions of API, but cannot figure out how they could help to preserve an order within the tag group. Actually it is a missing feature in the Path description to have some kind of Thanks a lot for the Swagger, you are doing a great job! |
@darklynx - the path sort option was explicitly removed from the spec. If you'd like to see it re-added in the future, you'd better open a feature request in the swagger-spec project, but explain your reasoning well (because we had reasons to exclude it ;)). We can continue the discussion there and assess whether it should change back in a future version of the spec. |
The discussion was in the 2.0 workgroup. If you email me, I'll try to find it and forward it to you. It may take a couple of days. |
I would like to suggest add new method
This is essential to preserve paths order defined in json/yaml file. |
The fact is that browsers (the most commonly used parsers of swagger JSON, no?) do preserve property order of objects, and this feature is part of the ES6 specification. Here’s an informative discussion of the reasons. I found this remark from Brendon Eich specifically noteworthy:
What do you mean that “the order would change just fetching the items twice in a row”? I don't know of any widely used Also, your claim that “maps are unordered” is an over-generalization. From the Map Javadoc:
If callers choose to use an ordered If fixing this issue is really as simple as removing a sort operation that has no reason to exist in the first place, I second @darklynx’s request for you to please consider it. Thanks. |
Just to check, there is no way to customly order my paths to show them in a logical order in swagger ui ? |
funny, it got fixed with no complains in this PR #1951 I do not get why did we have to run such a long discussion here and a year later people just approve the suggested changes w/o arguing 🤷♂️ |
io.swagger.models.Swagger
class supports preserved order for paths by usingLinkedHashMap
however the paths getter method has following magic inside:This completely breaks the original order of paths. I encountered this issue while generating swagger JSON from YAML description using swagger-codegen.
Are there any particular reason to sort the paths in the getter?
The quick replace of the getter with:
solved my problem, but maybe such fix could break some other projects/logic that I'm not aware of.
It would be great to have a possibility to preserve the original order of paths, because they are usually grouped and displayed in Swagger UI in the logical order, not in alphabetical.
The text was updated successfully, but these errors were encountered: