Skip to content

Commit

Permalink
Fix gqlgen truncates tag value with colon (99designs#2759)
Browse files Browse the repository at this point in the history
  • Loading branch information
xzzpig authored Aug 25, 2023
1 parent d6270e4 commit 065aea3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 6 additions & 4 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,17 +568,19 @@ func removeDuplicateTags(t string) string {
continue
}

processed[kv[0]] = true
key := kv[0]
value := strings.Join(kv[1:], ":")
processed[key] = true
if len(returnTags) > 0 {
returnTags = " " + returnTags
}

isContained := containsInvalidSpace(kv[1])
isContained := containsInvalidSpace(value)
if isContained {
panic(fmt.Errorf("tag value should not contain any leading or trailing spaces: %s", kv[1]))
panic(fmt.Errorf("tag value should not contain any leading or trailing spaces: %s", value))
}

returnTags = kv[0] + ":" + kv[1] + returnTags
returnTags = key + ":" + value + returnTags
}

return returnTags
Expand Down
16 changes: 16 additions & 0 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,22 @@ func TestRemoveDuplicate(t *testing.T) {
want: "gorm:\"unique;not null\" json:\"name,name2\"",
wantPanic: false,
},
{
name: "Test gorm tag with colon",
args: args{
t: "gorm:\"type:varchar(63);unique_index\"",
},
want: "gorm:\"type:varchar(63);unique_index\"",
wantPanic: false,
},
{
name: "Test mix use of gorm and duplicate json tags with colon",
args: args{
t: "json:\"name0\" gorm:\"type:varchar(63);unique_index\" json:\"name,name2\"",
},
want: "gorm:\"type:varchar(63);unique_index\" json:\"name,name2\"",
wantPanic: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 065aea3

Please sign in to comment.