Skip to content

Commit d59fd57

Browse files
authored
fix: do not abort check on ignored fields (#88)
1 parent 4026f8f commit d59fd57

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

musttag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (c *checker) checkStruct(styp *types.Struct, tag string) (valid bool) {
222222

223223
// Do not recurse into ignored fields.
224224
if tagValue == "-" {
225-
return true
225+
continue
226226
}
227227

228228
if valid := c.checkType(field.Type(), tag); !valid {

testdata/src/tests/tests.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ func ignoredNestedType() {
165165
json.Marshal(&Foo{}) // no error
166166
}
167167

168+
func ignoredNestedTypeWithSubsequentNoTagField() {
169+
type Nested struct {
170+
NoTag string
171+
}
172+
type Foo struct {
173+
Ignored Nested `json:"-"`
174+
Exported string `json:"exported"`
175+
NoTag string
176+
}
177+
var foo Foo
178+
json.Marshal(foo) // want "the given struct should be annotated with the `json` tag"
179+
json.Marshal(&foo) // want "the given struct should be annotated with the `json` tag"
180+
json.Marshal(Foo{}) // want "the given struct should be annotated with the `json` tag"
181+
json.Marshal(&Foo{}) // want "the given struct should be annotated with the `json` tag"
182+
}
183+
168184
func interfaceSliceType() {
169185
type WithMarshallableSlice struct {
170186
List []Marshaler `json:"marshallable"`

0 commit comments

Comments
 (0)