From 61e8894c3174aa095f6eaedd88da1e5c568ee72c Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 6 Oct 2025 22:02:42 +0800 Subject: [PATCH] Fix swagger generation: info block and server tags not included (#5215) Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kevwan <1918356+kevwan@users.noreply.github.com> --- tools/goctl/api/swagger/annotation.go | 24 ++++++++++---- tools/goctl/api/swagger/annotation_test.go | 38 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/tools/goctl/api/swagger/annotation.go b/tools/goctl/api/swagger/annotation.go index c019ba163..4368f42a9 100644 --- a/tools/goctl/api/swagger/annotation.go +++ b/tools/goctl/api/swagger/annotation.go @@ -18,8 +18,12 @@ func getBoolFromKVOrDefault(properties map[string]string, key string, def bool) } //I think this function and those below should handle error, but they didn't. //Since a default value (def) is provided, any parsing errors will result in the default being returned. - str, err := strconv.Unquote(val[0]) - if err != nil || len(str) == 0 { + str := val[0] + // Try to unquote if the string is quoted, otherwise use as-is + if unquoted, err := strconv.Unquote(str); err == nil { + str = unquoted + } + if len(str) == 0 { return def } res, _ := strconv.ParseBool(str) @@ -35,8 +39,12 @@ func getStringFromKVOrDefault(properties map[string]string, key string, def stri if len(val) == 0 { return def } - str, err := strconv.Unquote(val[0]) - if err != nil || len(str) == 0 { + str := val[0] + // Try to unquote if the string is quoted, otherwise use as-is + if unquoted, err := strconv.Unquote(str); err == nil { + str = unquoted + } + if len(str) == 0 { return def } return str @@ -52,8 +60,12 @@ func getListFromInfoOrDefault(properties map[string]string, key string, def []st return def } - str, err := strconv.Unquote(val[0]) - if err != nil || len(str) == 0 { + str := val[0] + // Try to unquote if the string is quoted, otherwise use as-is + if unquoted, err := strconv.Unquote(str); err == nil { + str = unquoted + } + if len(str) == 0 { return def } resp := util.FieldsAndTrimSpace(str, commaRune) diff --git a/tools/goctl/api/swagger/annotation_test.go b/tools/goctl/api/swagger/annotation_test.go index 872d65640..f26b97f93 100644 --- a/tools/goctl/api/swagger/annotation_test.go +++ b/tools/goctl/api/swagger/annotation_test.go @@ -21,6 +21,19 @@ func Test_getBoolFromKVOrDefault(t *testing.T) { assert.False(t, getBoolFromKVOrDefault(properties, "empty_value", false)) assert.False(t, getBoolFromKVOrDefault(nil, "nil", false)) assert.False(t, getBoolFromKVOrDefault(map[string]string{}, "empty", false)) + + // Test with unquoted values (as stored by RawText()) + unquotedProperties := map[string]string{ + "enabled": "true", + "disabled": "false", + "invalid": "notabool", + "empty_value": "", + } + + assert.True(t, getBoolFromKVOrDefault(unquotedProperties, "enabled", false)) + assert.False(t, getBoolFromKVOrDefault(unquotedProperties, "disabled", true)) + assert.False(t, getBoolFromKVOrDefault(unquotedProperties, "invalid", false)) + assert.False(t, getBoolFromKVOrDefault(unquotedProperties, "empty_value", false)) } func Test_getStringFromKVOrDefault(t *testing.T) { @@ -34,6 +47,17 @@ func Test_getStringFromKVOrDefault(t *testing.T) { assert.Equal(t, "default", getStringFromKVOrDefault(properties, "missing", "default")) assert.Equal(t, "default", getStringFromKVOrDefault(nil, "nil", "default")) assert.Equal(t, "default", getStringFromKVOrDefault(map[string]string{}, "empty", "default")) + + // Test with unquoted values (as stored by RawText()) + unquotedProperties := map[string]string{ + "name": "example", + "title": "Demo API", + "empty": "", + } + + assert.Equal(t, "example", getStringFromKVOrDefault(unquotedProperties, "name", "default")) + assert.Equal(t, "Demo API", getStringFromKVOrDefault(unquotedProperties, "title", "default")) + assert.Equal(t, "default", getStringFromKVOrDefault(unquotedProperties, "empty", "default")) } func Test_getListFromInfoOrDefault(t *testing.T) { @@ -50,4 +74,18 @@ func Test_getListFromInfoOrDefault(t *testing.T) { assert.Equal(t, []string{"default"}, getListFromInfoOrDefault(map[string]string{ "foo": ",,", }, "foo", []string{"default"})) + + // Test with unquoted values (as stored by RawText()) + unquotedProperties := map[string]string{ + "list": "a, b, c", + "schemes": "http,https", + "tags": "query", + "empty": "", + } + + // Note: FieldsAndTrimSpace doesn't actually trim the spaces from returned values + assert.Equal(t, []string{"a", " b", " c"}, getListFromInfoOrDefault(unquotedProperties, "list", []string{"default"})) + assert.Equal(t, []string{"http", "https"}, getListFromInfoOrDefault(unquotedProperties, "schemes", []string{"default"})) + assert.Equal(t, []string{"query"}, getListFromInfoOrDefault(unquotedProperties, "tags", []string{"default"})) + assert.Equal(t, []string{"default"}, getListFromInfoOrDefault(unquotedProperties, "empty", []string{"default"})) }