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

Allows to define the order of the operations on the summary pages and sidenav. #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"os"
"regexp"
"sort"
"strings"

"github.com/dapperdox/dapperdox/config"
Expand Down Expand Up @@ -110,6 +111,7 @@ type Method struct {
OperationName string
NavigationName string
Path string
SortOrder string
Consumes []string
Produces []string
PathParams []Parameter
Expand Down Expand Up @@ -181,6 +183,12 @@ type Header struct {
Enum []string
}

type BySortOrder []Method

func (a BySortOrder) Len() int { return len(a) }
func (a BySortOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a BySortOrder) Less(i, j int) bool { return a[i].SortOrder < a[j].SortOrder }

// -----------------------------------------------------------------------------

func LoadSpecifications(specHost string, collapse bool) error {
Expand Down Expand Up @@ -349,12 +357,14 @@ func (c *APISpecification) Load(specLocation string, specHost string) error {
// If API was populated (will not be if tags do not match), add to set
if !groupingByTag && len(api.Methods) > 0 {
logger.Tracef(nil, " + Adding %s\n", name)
sort.Sort(BySortOrder(api.Methods))
c.APIs = append(c.APIs, *api) // All APIs (versioned within)
}
}

if groupingByTag && len(api.Methods) > 0 {
logger.Tracef(nil, " + Adding %s\n", name)
sort.Sort(BySortOrder(api.Methods))
c.APIs = append(c.APIs, *api) // All APIs (versioned within)
}
}
Expand Down Expand Up @@ -567,6 +577,11 @@ func (c *APISpecification) processMethod(api *APIGroup, pathItem *spec.PathItem,
navigationName = o.Summary
}

sortOrder := o.Summary
Copy link
Collaborator

@zxchris zxchris Oct 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work as expected if you don't define x-navigateMethodsByName: true (or set it to false), as the entries are still ordered by Summary, it it probably should be ordered by operationName (I tried this out on the standard petstore example which isn't really REST, but was good enough to see the issue).

See next comment though....

if o.Extensions["x-sortOrder"] != nil {
sortOrder = "!" + o.Extensions["x-sortOrder"].(string)
}

method := &Method{
ID: CamelToKebab(id),
Name: o.Summary,
Expand All @@ -576,6 +591,7 @@ func (c *APISpecification) processMethod(api *APIGroup, pathItem *spec.PathItem,
Responses: make(map[int]Response),
NavigationName: navigationName,
OperationName: operationName,
SortOrder: sortOrder,
APIGroup: api,
}
if len(o.Consumes) > 0 {
Expand Down