Skip to content

Commit

Permalink
fix: json omitempty struct tag attribute broke pact struct tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jun 19, 2021
1 parent 06d674c commit 8ebc234
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion matchers/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func match(srcType reflect.Type, params params) Matcher {

for i := 0; i < srcType.NumField(); i++ {
field := srcType.Field(i)
result[field.Tag.Get("json")] = match(field.Type, pluckParams(field.Type, field.Tag.Get("pact")))
result[strings.Split(field.Tag.Get("json"), ",")[0]] = match(field.Type, pluckParams(field.Type, field.Tag.Get("pact")))
}
return result
case reflect.String:
Expand Down
12 changes: 12 additions & 0 deletions matchers/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@ func ExampleEachLike() {
}

func TestMatch(t *testing.T) {
type jsonStruct struct {
ValueWithOmitEmpty string `json:"value,omitempty"`
}
type wordDTO struct {
Word string `json:"word"`
Length int `json:"length"`
Expand Down Expand Up @@ -620,6 +623,15 @@ func TestMatch(t *testing.T) {
"length": Like(1),
},
},
{
name: "recursive case - struct with json omitempty tags",
args: args{
src: jsonStruct{},
},
want: StructMatcher{
"value": Like("string"),
},
},
{
name: "recursive case - struct with custom string tag",
args: args{
Expand Down

0 comments on commit 8ebc234

Please sign in to comment.