Skip to content

Commit

Permalink
write -> create-or-update
Browse files Browse the repository at this point in the history
  • Loading branch information
averche committed Mar 30, 2023
1 parent cb5aa04 commit 7aacc2c
Showing 4 changed files with 65 additions and 27 deletions.
20 changes: 15 additions & 5 deletions sdk/framework/openapi.go
Original file line number Diff line number Diff line change
@@ -305,6 +305,13 @@ func documentPath(p *Path, specialPaths *logical.Paths, requestResponsePrefix st
return strings.ToLower(pi.Parameters[i].Name) < strings.ToLower(pi.Parameters[j].Name)
})

for opType := range operations {
if opType == logical.CreateOperation {
pi.CreateSupported = true
break
}
}

// Process each supported operation by building up an Operation object
// with descriptions, properties and examples from the framework.Path data.
for opType, opHandler := range operations {
@@ -314,8 +321,6 @@ func documentPath(p *Path, specialPaths *logical.Paths, requestResponsePrefix st
}

if opType == logical.CreateOperation {
pi.CreateSupported = true

// If both Create and Update are defined, only process Update.
if operations[logical.UpdateOperation] != nil {
continue
@@ -335,6 +340,7 @@ func documentPath(p *Path, specialPaths *logical.Paths, requestResponsePrefix st
p.DisplayAttrs,
opType,
props.DisplayAttrs,
pi.CreateSupported,
requestResponsePrefix,
)

@@ -599,6 +605,7 @@ func constructOperationID(
pathAttributes *DisplayAttributes,
operation logical.Operation,
operationAttributes *DisplayAttributes,
createSupported bool,
defaultPrefix string,
) string {
var (
@@ -677,9 +684,12 @@ func constructOperationID(
}

if needVerb {
if operation == logical.UpdateOperation {
verb = "write"
} else {
switch {
case operation == logical.UpdateOperation && createSupported == true:
verb = "create-or-update"
case operation == logical.UpdateOperation && createSupported == false:
verb = "update"
default:
verb = string(operation)
}
}
60 changes: 44 additions & 16 deletions sdk/framework/openapi_test.go
Original file line number Diff line number Diff line change
@@ -659,6 +659,7 @@ func TestOpenAPI_constructOperationID(t *testing.T) {
pathAttributes *DisplayAttributes
operation logical.Operation
operationAttributes *DisplayAttributes
createSupported bool
defaultPrefix string
expected string
}{
@@ -668,6 +669,7 @@ func TestOpenAPI_constructOperationID(t *testing.T) {
pathAttributes: nil,
operation: logical.Operation(""),
operationAttributes: nil,
createSupported: false,
defaultPrefix: "",
expected: "",
},
@@ -677,24 +679,37 @@ func TestOpenAPI_constructOperationID(t *testing.T) {
pathAttributes: nil,
operation: logical.ReadOperation,
operationAttributes: nil,
createSupported: false,
defaultPrefix: "test",
expected: "test-read-path-to-thing",
},
"simple-write": {
"simple-update": {
path: "path/to/thing",
pathIndex: 0,
pathAttributes: nil,
operation: logical.UpdateOperation,
operationAttributes: nil,
createSupported: false,
defaultPrefix: "test",
expected: "test-write-path-to-thing",
expected: "test-update-path-to-thing",
},
"simple-create-or-update": {
path: "path/to/thing",
pathIndex: 0,
pathAttributes: nil,
operation: logical.UpdateOperation,
operationAttributes: nil,
createSupported: true,
defaultPrefix: "test",
expected: "test-create-or-update-path-to-thing",
},
"operation-verb": {
path: "path/to/thing",
pathIndex: 0,
pathAttributes: &DisplayAttributes{OperationVerb: "do-something"},
operation: logical.UpdateOperation,
operationAttributes: nil,
createSupported: false,
defaultPrefix: "test",
expected: "do-something",
},
@@ -704,6 +719,7 @@ func TestOpenAPI_constructOperationID(t *testing.T) {
pathAttributes: &DisplayAttributes{OperationVerb: "do-something"},
operation: logical.UpdateOperation,
operationAttributes: &DisplayAttributes{OperationVerb: "do-something-else"},
createSupported: false,
defaultPrefix: "test",
expected: "do-something-else",
},
@@ -713,51 +729,57 @@ func TestOpenAPI_constructOperationID(t *testing.T) {
pathAttributes: &DisplayAttributes{OperationPrefix: "my-prefix"},
operation: logical.UpdateOperation,
operationAttributes: nil,
createSupported: false,
defaultPrefix: "test",
expected: "my-prefix-write-path-to-thing",
expected: "my-prefix-update-path-to-thing",
},
"operation-prefix-override": {
path: "path/to/thing",
pathIndex: 0,
pathAttributes: &DisplayAttributes{OperationPrefix: "my-prefix"},
operation: logical.UpdateOperation,
operation: logical.DeleteOperation,
operationAttributes: &DisplayAttributes{OperationPrefix: "better-prefix"},
createSupported: false,
defaultPrefix: "test",
expected: "better-prefix-write-path-to-thing",
expected: "better-prefix-delete-path-to-thing",
},
"operation-prefix-and-suffix": {
path: "path/to/thing",
pathIndex: 0,
pathAttributes: &DisplayAttributes{OperationPrefix: "my-prefix", OperationSuffix: "my-suffix"},
operation: logical.UpdateOperation,
operation: logical.ListOperation,
operationAttributes: nil,
createSupported: false,
defaultPrefix: "test",
expected: "my-prefix-write-my-suffix",
expected: "my-prefix-list-my-suffix",
},
"operation-prefix-and-suffix-override": {
path: "path/to/thing",
pathIndex: 0,
pathAttributes: &DisplayAttributes{OperationPrefix: "my-prefix", OperationSuffix: "my-suffix"},
operation: logical.UpdateOperation,
operation: logical.ReadOperation,
operationAttributes: &DisplayAttributes{OperationPrefix: "better-prefix", OperationSuffix: "better-suffix"},
createSupported: false,
defaultPrefix: "test",
expected: "better-prefix-write-better-suffix",
expected: "better-prefix-read-better-suffix",
},
"operation-prefix-verb-suffix": {
path: "path/to/thing",
pathIndex: 0,
pathAttributes: &DisplayAttributes{OperationPrefix: "my-prefix", OperationSuffix: "my-suffix", OperationVerb: "Create"},
operation: logical.UpdateOperation,
operationAttributes: &DisplayAttributes{OperationPrefix: "better-prefix", OperationSuffix: "better-suffix"},
createSupported: false,
defaultPrefix: "test",
expected: "better-prefix-create-better-suffix",
},
"operation-prefix-verb-suffix-override": {
path: "path/to/thing",
pathIndex: 0,
pathAttributes: &DisplayAttributes{OperationPrefix: "my-prefix", OperationSuffix: "my-suffix", OperationVerb: "Create"},
pathAttributes: &DisplayAttributes{OperationPrefix: "my-prefix", OperationSuffix: "my-suffix", OperationVerb: "create"},
operation: logical.UpdateOperation,
operationAttributes: &DisplayAttributes{OperationPrefix: "better-prefix", OperationSuffix: "better-suffix", OperationVerb: "Login"},
operationAttributes: &DisplayAttributes{OperationPrefix: "better-prefix", OperationSuffix: "better-suffix", OperationVerb: "login"},
createSupported: false,
defaultPrefix: "test",
expected: "better-prefix-login-better-suffix",
},
@@ -766,7 +788,8 @@ func TestOpenAPI_constructOperationID(t *testing.T) {
pathIndex: 0,
pathAttributes: nil,
operation: logical.UpdateOperation,
operationAttributes: &DisplayAttributes{OperationPrefix: "better-prefix", OperationVerb: "Login"},
operationAttributes: &DisplayAttributes{OperationPrefix: "better-prefix", OperationVerb: "login"},
createSupported: false,
defaultPrefix: "test",
expected: "better-prefix-login",
},
@@ -775,7 +798,8 @@ func TestOpenAPI_constructOperationID(t *testing.T) {
pathIndex: 0,
pathAttributes: nil,
operation: logical.UpdateOperation,
operationAttributes: &DisplayAttributes{OperationVerb: "Login", OperationSuffix: "better-suffix"},
operationAttributes: &DisplayAttributes{OperationVerb: "login", OperationSuffix: "better-suffix"},
createSupported: false,
defaultPrefix: "test",
expected: "login-better-suffix",
},
@@ -785,26 +809,29 @@ func TestOpenAPI_constructOperationID(t *testing.T) {
pathAttributes: nil,
operation: logical.UpdateOperation,
operationAttributes: &DisplayAttributes{OperationPrefix: "better-prefix", OperationSuffix: "suffix0|suffix1"},
createSupported: false,
defaultPrefix: "test",
expected: "better-prefix-write-suffix0",
expected: "better-prefix-update-suffix0",
},
"pipe-delimited-suffix-1": {
path: "path/to/thing",
pathIndex: 1,
pathAttributes: nil,
operation: logical.UpdateOperation,
operationAttributes: &DisplayAttributes{OperationPrefix: "better-prefix", OperationSuffix: "suffix0|suffix1"},
createSupported: true,
defaultPrefix: "test",
expected: "better-prefix-write-suffix1",
expected: "better-prefix-create-or-update-suffix1",
},
"pipe-delimited-suffix-2-fallback": {
path: "path/to/thing",
pathIndex: 2,
pathAttributes: nil,
operation: logical.UpdateOperation,
operationAttributes: &DisplayAttributes{OperationPrefix: "better-prefix", OperationSuffix: "suffix0|suffix1"},
createSupported: false,
defaultPrefix: "test",
expected: "better-prefix-write-path-to-thing",
expected: "better-prefix-update-path-to-thing",
},
}

@@ -818,6 +845,7 @@ func TestOpenAPI_constructOperationID(t *testing.T) {
test.pathAttributes,
test.operation,
test.operationAttributes,
test.createSupported,
test.defaultPrefix,
)
if actual != test.expected {
6 changes: 3 additions & 3 deletions sdk/framework/testdata/legacy.json
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@
}
},
"post": {
"operationId": "kv-write-lookup-id",
"operationId": "kv-update-lookup-id",
"summary": "Synopsis",
"tags": [
"secrets"
@@ -46,7 +46,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/kv-write-lookup-id-request"
"$ref": "#/components/schemas/kv-update-lookup-id-request"
}
}
}
@@ -61,7 +61,7 @@
},
"components": {
"schemas": {
"kv-write-lookup-id-request": {
"kv-update-lookup-id-request": {
"type": "object",
"properties": {
"token": {
6 changes: 3 additions & 3 deletions sdk/framework/testdata/operations.json
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@
]
},
"post": {
"operationId": "kv-write-foo-id",
"operationId": "kv-create-or-update-foo-id",
"tags": [
"secrets"
],
@@ -71,7 +71,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/kv-write-foo-id-request"
"$ref": "#/components/schemas/kv-create-or-update-foo-id-request"
}
}
}
@@ -86,7 +86,7 @@
},
"components": {
"schemas": {
"kv-write-foo-id-request": {
"kv-create-or-update-foo-id-request": {
"type": "object",
"required": [
"age"

0 comments on commit 7aacc2c

Please sign in to comment.