Skip to content

Commit 61e8894

Browse files
Copilotkevwan
andauthored
Fix swagger generation: info block and server tags not included (#5215)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: kevwan <[email protected]>
1 parent 7a6c3c8 commit 61e8894

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

tools/goctl/api/swagger/annotation.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ func getBoolFromKVOrDefault(properties map[string]string, key string, def bool)
1818
}
1919
//I think this function and those below should handle error, but they didn't.
2020
//Since a default value (def) is provided, any parsing errors will result in the default being returned.
21-
str, err := strconv.Unquote(val[0])
22-
if err != nil || len(str) == 0 {
21+
str := val[0]
22+
// Try to unquote if the string is quoted, otherwise use as-is
23+
if unquoted, err := strconv.Unquote(str); err == nil {
24+
str = unquoted
25+
}
26+
if len(str) == 0 {
2327
return def
2428
}
2529
res, _ := strconv.ParseBool(str)
@@ -35,8 +39,12 @@ func getStringFromKVOrDefault(properties map[string]string, key string, def stri
3539
if len(val) == 0 {
3640
return def
3741
}
38-
str, err := strconv.Unquote(val[0])
39-
if err != nil || len(str) == 0 {
42+
str := val[0]
43+
// Try to unquote if the string is quoted, otherwise use as-is
44+
if unquoted, err := strconv.Unquote(str); err == nil {
45+
str = unquoted
46+
}
47+
if len(str) == 0 {
4048
return def
4149
}
4250
return str
@@ -52,8 +60,12 @@ func getListFromInfoOrDefault(properties map[string]string, key string, def []st
5260
return def
5361
}
5462

55-
str, err := strconv.Unquote(val[0])
56-
if err != nil || len(str) == 0 {
63+
str := val[0]
64+
// Try to unquote if the string is quoted, otherwise use as-is
65+
if unquoted, err := strconv.Unquote(str); err == nil {
66+
str = unquoted
67+
}
68+
if len(str) == 0 {
5769
return def
5870
}
5971
resp := util.FieldsAndTrimSpace(str, commaRune)

tools/goctl/api/swagger/annotation_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ func Test_getBoolFromKVOrDefault(t *testing.T) {
2121
assert.False(t, getBoolFromKVOrDefault(properties, "empty_value", false))
2222
assert.False(t, getBoolFromKVOrDefault(nil, "nil", false))
2323
assert.False(t, getBoolFromKVOrDefault(map[string]string{}, "empty", false))
24+
25+
// Test with unquoted values (as stored by RawText())
26+
unquotedProperties := map[string]string{
27+
"enabled": "true",
28+
"disabled": "false",
29+
"invalid": "notabool",
30+
"empty_value": "",
31+
}
32+
33+
assert.True(t, getBoolFromKVOrDefault(unquotedProperties, "enabled", false))
34+
assert.False(t, getBoolFromKVOrDefault(unquotedProperties, "disabled", true))
35+
assert.False(t, getBoolFromKVOrDefault(unquotedProperties, "invalid", false))
36+
assert.False(t, getBoolFromKVOrDefault(unquotedProperties, "empty_value", false))
2437
}
2538

2639
func Test_getStringFromKVOrDefault(t *testing.T) {
@@ -34,6 +47,17 @@ func Test_getStringFromKVOrDefault(t *testing.T) {
3447
assert.Equal(t, "default", getStringFromKVOrDefault(properties, "missing", "default"))
3548
assert.Equal(t, "default", getStringFromKVOrDefault(nil, "nil", "default"))
3649
assert.Equal(t, "default", getStringFromKVOrDefault(map[string]string{}, "empty", "default"))
50+
51+
// Test with unquoted values (as stored by RawText())
52+
unquotedProperties := map[string]string{
53+
"name": "example",
54+
"title": "Demo API",
55+
"empty": "",
56+
}
57+
58+
assert.Equal(t, "example", getStringFromKVOrDefault(unquotedProperties, "name", "default"))
59+
assert.Equal(t, "Demo API", getStringFromKVOrDefault(unquotedProperties, "title", "default"))
60+
assert.Equal(t, "default", getStringFromKVOrDefault(unquotedProperties, "empty", "default"))
3761
}
3862

3963
func Test_getListFromInfoOrDefault(t *testing.T) {
@@ -50,4 +74,18 @@ func Test_getListFromInfoOrDefault(t *testing.T) {
5074
assert.Equal(t, []string{"default"}, getListFromInfoOrDefault(map[string]string{
5175
"foo": ",,",
5276
}, "foo", []string{"default"}))
77+
78+
// Test with unquoted values (as stored by RawText())
79+
unquotedProperties := map[string]string{
80+
"list": "a, b, c",
81+
"schemes": "http,https",
82+
"tags": "query",
83+
"empty": "",
84+
}
85+
86+
// Note: FieldsAndTrimSpace doesn't actually trim the spaces from returned values
87+
assert.Equal(t, []string{"a", " b", " c"}, getListFromInfoOrDefault(unquotedProperties, "list", []string{"default"}))
88+
assert.Equal(t, []string{"http", "https"}, getListFromInfoOrDefault(unquotedProperties, "schemes", []string{"default"}))
89+
assert.Equal(t, []string{"query"}, getListFromInfoOrDefault(unquotedProperties, "tags", []string{"default"}))
90+
assert.Equal(t, []string{"default"}, getListFromInfoOrDefault(unquotedProperties, "empty", []string{"default"}))
5391
}

0 commit comments

Comments
 (0)