Allow sorting operations programmatically #2802
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds ability to order operations programmatically.
The problem
Currently, Stoplight Elements renders operations in order they are defined in the OpenAPI document.
However, you may want to order endpoints in the way, the OpenAPI document structure won't allow it. Imagine you want to order operations like so:
The OpenAPI document cannot represent operations in the way you want them to get ordered simply due to the structure of OpenAPI document itself (operations are grouped within paths). So here is the specification you have for such operations:
This results in operations rendered in the following order:
The solution
I propose to add the
operationsSorter
option to theAPI
component which will allow to pass the sort functionOperationSorter
:Here is how it can be used – in this case I've added
x-weight
extension to an operation and sort operations by this extension's value.This is the code of the story I've created:
Implementation considerations
I considered 2 possible points where the sorting can happen.
The first option is to sort it early, right after
ServiceNode
is created, but before operations are grouped.The second option is to sort operations after they are grouped within each group and right before rendering the operations in layout components.
I tried both approaches and decided to go with the first approach – as a developer, I'd expect
ServiceNode
object to have operations sorted according to the provided operations sorter (in case a sorter has been provided via the options).Thoughts?
Let me know what you think and if this is a good addition!
Elements Default PR Template
CONTRIBUTING.md
Other Available PR Templates:
CONTRIBUTING.md