Skip to content

Commit

Permalink
fix: better kebab casing of operation IDs, fixes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Jan 30, 2021
1 parent 069c761 commit 4aa30c3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/amzn/ion-go v1.1.0
github.com/andybalholm/brotli v1.0.1
github.com/creack/pty v1.1.11 // indirect
github.com/danielgtaylor/casing v0.0.0-20210126043903-4e55e6373ac3
github.com/danielgtaylor/go-jmespath-plus v0.0.0-20200228063638-e0b6f132acba
github.com/danielgtaylor/openapi-cli-generator v0.0.0-20200904035210-47cad525a9e0
github.com/dlclark/regexp2 v1.4.0 // indirect
Expand Down Expand Up @@ -41,7 +42,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.0
github.com/tent/http-link-go v0.0.0-20130702225549-ac974c61c2f9
github.com/twpayne/httpcache v1.0.0 // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/danielgtaylor/casing v0.0.0-20210126043903-4e55e6373ac3 h1:qDsADtCM9A6UfvHje3eD91dufI9nVSwHWEqqhAvh28U=
github.com/danielgtaylor/casing v0.0.0-20210126043903-4e55e6373ac3/go.mod h1:eFdYmNxcuLDrRNW0efVoxSaApmvGXfHZ9k2CT/RSUF0=
github.com/danielgtaylor/go-jmespath-plus v0.0.0-20200228063638-e0b6f132acba h1:COT94fQgUPh7CG42x3RTfab3V9jK9x4i2GLpubq7eZM=
github.com/danielgtaylor/go-jmespath-plus v0.0.0-20200228063638-e0b6f132acba/go.mod h1:A57wu2YKZM9dwidFjak6swlRJPKbg+TfHsFm0py9/i8=
github.com/danielgtaylor/openapi-cli-generator v0.0.0-20200904035210-47cad525a9e0 h1:JnHz1kEd1ie38Qj90pIxhOoN9vu53HQJn4iXhY7E4vc=
Expand Down Expand Up @@ -415,6 +417,8 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
Expand Down
16 changes: 11 additions & 5 deletions openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sort"
"strings"

"github.com/danielgtaylor/casing"
"github.com/danielgtaylor/openapi-cli-generator/shorthand"
"github.com/danielgtaylor/restish/cli"
"github.com/getkin/kin-openapi/openapi3"
Expand Down Expand Up @@ -202,17 +203,22 @@ func openapiOperation(cmd *cobra.Command, method string, uriTemplate *url.URL, p
}
}

name := slug.Make(op.OperationID)
if override := extStr(op.ExtensionProps, ExtName); override != "" {
name = override
}

var aliases []string
if op.Extensions[ExtAliases] != nil {
// We need to decode the raw extension value into our string slice.
json.Unmarshal(op.Extensions[ExtAliases].(json.RawMessage), &aliases)
}

name := casing.Kebab(op.OperationID)
if override := extStr(op.ExtensionProps, ExtName); override != "" {
name = override
} else if oldName := slug.Make(op.OperationID); oldName != name {
// For backward-compatibility, add the old naming scheme as an alias
// if it is different. See https://github.com/danielgtaylor/restish/issues/29
// for additional context; we prefer kebab casing for readability.
aliases = append(aliases, oldName)
}

desc := op.Description
if override := extStr(op.ExtensionProps, ExtDescription); override != "" {
desc = override
Expand Down
9 changes: 6 additions & 3 deletions openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ func TestLoadOpenAPI(t *testing.T) {
},
Operations: []cli.Operation{
{
Name: "createpets",
Name: "create-pets",
Aliases: []string{"createpets"},
Short: "Create a pet",
Long: "\n## Response 201\n\nNull response\n\n## Response default (application/json)\n\nunexpected error\n\n```schema\n{\n code*: (integer format:int32) \n message*: (string) \n}\n```\n",
Method: "POST",
Expand All @@ -174,7 +175,8 @@ func TestLoadOpenAPI(t *testing.T) {
HeaderParams: []*cli.Param{},
},
{
Name: "listpets",
Name: "list-pets",
Aliases: []string{"listpets"},
Short: "List all pets",
Long: "\n## Response 200 (application/json)\n\nA paged array of pets\n\n```schema\n[\n {\n id*: (integer format:int64) \n name*: (string) \n tag: (string) \n }\n]\n```\n\n## Response default (application/json)\n\nunexpected error\n\n```schema\n{\n code*: (integer format:int32) \n message*: (string) \n}\n```\n",
Method: "GET",
Expand All @@ -190,7 +192,8 @@ func TestLoadOpenAPI(t *testing.T) {
HeaderParams: []*cli.Param{},
},
{
Name: "showpetbyid",
Name: "show-pet-by-id",
Aliases: []string{"showpetbyid"},
Short: "Info for a specific pet",
Long: "\n## Response 200 (application/json)\n\nExpected response to a valid request\n\n```schema\n{\n id*: (integer format:int64) \n name*: (string) \n tag: (string) \n}\n```\n\n## Response default (application/json)\n\nunexpected error\n\n```schema\n{\n code*: (integer format:int32) \n message*: (string) \n}\n```\n",
Method: "GET",
Expand Down

0 comments on commit 4aa30c3

Please sign in to comment.