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

Add operationId field to OpenAPI output #5876

Merged
merged 4 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 54 additions & 0 deletions logical/framework/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func NewOASOperation() *OASOperation {
type OASOperation struct {
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
OperationID string `json:"operationId,omitempty"`
Tags []string `json:"tags,omitempty"`
Parameters []OASParameter `json:"parameters,omitempty"`
RequestBody *OASRequestBody `json:"requestBody,omitempty"`
Expand Down Expand Up @@ -611,3 +612,56 @@ func cleanResponse(resp *logical.Response) (*cleanedResponse, error) {

return &r, nil
}

// CreateOperationIDs generates unique operationIds for all paths/methods.
// The transform will convert path/method into camelcase. e.g.:
//
// /sys/tools/random/{urlbytes} -> postSysToolsRandomUrlbytes
//
// In the unlikely case of a duplicate ids, a numeric suffix is added:
// postSysToolsRandomUrlbytes_2
//
// An optional user-provided suffix ("context") may also be appended.
func (d *OASDocument) CreateOperationIDs(context string) {
nonWordRe := regexp.MustCompile(`[^\w]+`)
kalafut marked this conversation as resolved.
Show resolved Hide resolved
operationIDs := make(map[string]int)

for path, pi := range d.Paths {
for _, method := range []string{"get", "post", "delete"} {
var oasOperation *OASOperation
switch method {
case "get":
oasOperation = pi.Get
case "post":
oasOperation = pi.Post
case "delete":
oasOperation = pi.Delete
}

if oasOperation == nil {
continue
}

// Space-split on non-words, title case everything, recombine
opID := nonWordRe.ReplaceAllString(strings.ToLower(path), " ")
opID = strings.Title(opID)
opID = method + strings.Replace(opID, " ", "", -1)

// deduplicate operationIds. This is a safeguard, since generated IDs should
// already be unique given our current path naming conventions.
n := operationIDs[opID]
if n > 0 {
operationIDs[opID] += 1
opID = fmt.Sprintf("%s_%d", opID, n)
} else {
operationIDs[opID] = 2
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be 1 instead? Otherwise the above += 1 will result in 3 for the next duplicate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's behaves as expected (since n is used in the printf), but the code is awkward. I just pushed a change that simplifies it.

}

if context != "" {
opID += "_" + context
}

oasOperation.OperationID = opID
}
}
}
4 changes: 4 additions & 0 deletions vault/logical_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -3057,6 +3057,8 @@ func (b *SystemBackend) pathInternalOpenAPI(ctx context.Context, req *logical.Re
return nil, err
}

context := d.Get("context").(string)

// Set up target document and convert to map[string]interface{} which is what will
// be received from plugin backends.
doc := framework.NewOASDocument()
Expand Down Expand Up @@ -3137,6 +3139,8 @@ func (b *SystemBackend) pathInternalOpenAPI(ctx context.Context, req *logical.Re
return nil, err
}

doc.CreateOperationIDs(context)

buf, err := json.Marshal(doc)
if err != nil {
return nil, err
Expand Down
9 changes: 8 additions & 1 deletion vault/logical_system_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,15 @@ func (b *SystemBackend) internalPaths() []*framework.Path {
return []*framework.Path{
{
Pattern: "internal/specs/openapi",
Fields: map[string]*framework.FieldSchema{
"context": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Context string appended to every operationId",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathInternalOpenAPI,
logical.ReadOperation: b.pathInternalOpenAPI,
logical.UpdateOperation: b.pathInternalOpenAPI,
},
},
{
Expand Down