Skip to content

Commit c948b40

Browse files
committed
Merge pull request #496 from chatch/master
sort routes alphabetically (254)
2 parents dfd5e93 + 5d2bed7 commit c948b40

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ To use swagger-ui you should take a look at the [source of swagger-ui html page]
5959
* *dom_id parameter* is the the id of a dom element inside which SwaggerUi will put the user interface for swagger
6060
* *booleanValues* SwaggerUI renders boolean data types as a dropdown. By default it provides a 'true' and 'false' string as the possible choices. You can use this parameter to change the values in dropdown to be something else, for example 0 and 1 by setting booleanValues to new Array(0, 1)
6161
* *docExpansion* controls how the API listing is displayed. It can be set to 'none' (default), 'list' (shows operations for each resource), or 'full' (fully expanded: shows operations and their details)
62+
* *sorter* apply a sort to the API list. It can be 'alpha' (sort paths alphanumerically) or 'method' (sort operations by HTTP method). Default is the order returned by the server unchanged.
6263
* *onComplete* is a callback function parameter which can be passed to be notified of when SwaggerUI has completed rendering successfully.
6364
* *onFailure* is a callback function parameter which can be passed to be notified of when SwaggerUI encountered a failure was unable to render.
6465
* All other parameters are explained in greater detail below

src/main/coffeescript/SwaggerUi.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class SwaggerUi extends Backbone.Router
5555
# so it gets called when SwaggerApi completes loading
5656
render:() ->
5757
@showMessage('Finished Loading Resource Information. Rendering Swagger UI...')
58-
@mainView = new MainView({model: @api, el: $('#' + @dom_id)}).render()
58+
@mainView = new MainView({model: @api, el: $('#' + @dom_id), swaggerOptions: @options}).render()
5959
@showMessage()
6060
switch @options.docExpansion
6161
when "full" then Docs.expandOperationsForResource('')

src/main/coffeescript/view/MainView.coffee

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
class MainView extends Backbone.View
2-
initialize: ->
2+
sorters = {
3+
'alpha' : (a,b) -> return a.path.localeCompare(b.path),
4+
'method' : (a,b) -> return a.method.localeCompare(b.method),
5+
}
36

7+
initialize: (opts={}) ->
8+
if opts.swaggerOptions.sorter
9+
sorterName = opts.swaggerOptions.sorter
10+
sorter = sorters[sorterName]
11+
for route in @model.apisArray
12+
route.operationsArray.sort sorter
13+
if (sorterName == "alpha") # sort top level paths if alpha
14+
@model.apisArray.sort sorter
15+
416
render: ->
517
# Render the outer container for resources
618
$(@el).html(Handlebars.templates.main(@model))
@@ -25,4 +37,4 @@ class MainView extends Backbone.View
2537
$('#resources').append resourceView.render().el
2638

2739
clear: ->
28-
$(@el).html ''
40+
$(@el).html ''

src/main/html/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
onFailure: function(data) {
4848
log("Unable to Load SwaggerUI");
4949
},
50-
docExpansion: "none"
50+
docExpansion: "none",
51+
sorter : "alpha"
5152
});
5253

5354
$('#input_apiKey').change(function() {

0 commit comments

Comments
 (0)