From 04a021c714c1d7ee547848fd6cf8e23db5d8b684 Mon Sep 17 00:00:00 2001 From: "Daniel G. Taylor" Date: Thu, 3 Aug 2023 22:33:08 -0700 Subject: [PATCH] fix: generate operation ID if not present, fixes #201 --- openapi/openapi.go | 5 ++++- openapi/testdata/request/openapi.yaml | 1 - openapi/testdata/request/output.yaml | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/openapi/openapi.go b/openapi/openapi.go index 4b12cb4..5564f39 100644 --- a/openapi/openapi.go +++ b/openapi/openapi.go @@ -309,9 +309,12 @@ func openapiOperation(cmd *cobra.Command, method string, uriTemplate *url.URL, p aliases := getExtSlice(op.Extensions, ExtAliases, []string{}) name := casing.Kebab(op.OperationId) + if name == "" { + name = casing.Kebab(method + "-" + strings.Trim(uriTemplate.Path, "/")) + } if override := getExt(op.Extensions, ExtName, ""); override != "" { name = override - } else if oldName := slug.Make(op.OperationId); oldName != name { + } else if oldName := slug.Make(op.OperationId); oldName != "" && 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. diff --git a/openapi/testdata/request/openapi.yaml b/openapi/testdata/request/openapi.yaml index 9ee634e..3251002 100644 --- a/openapi/testdata/request/openapi.yaml +++ b/openapi/testdata/request/openapi.yaml @@ -43,7 +43,6 @@ paths: foo: type: string delete: - operationId: delete-item responses: "204": description: "" diff --git a/openapi/testdata/request/output.yaml b/openapi/testdata/request/output.yaml index 2acbf37..c5bb225 100644 --- a/openapi/testdata/request/output.yaml +++ b/openapi/testdata/request/output.yaml @@ -1,6 +1,6 @@ short: Test API operations: - - name: delete-item + - name: delete-items-item-id aliases: [] short: "" long: |