Testing in Go: strict JSON parsing
We had a discussion today about JSON parsing in our golang app. We had some code like this:
// lenient parsing will ignore unrecognized propertiesvar user User
err = json.Unmarshal([]byte(jsonString), &user)
assert.Nil(t, err)
The goal was to verify that jsonString
has the correct contents that matched the User
type, but the test always passed, as long as jsonString
is valid JSON.
In testing, we want to be sure the JSON string has no extra properties. So here, a strict JSON parser is more appropriate.
// will fail if the string has unrecognized propertiesvar user User
decoder := json.NewDecoder(strings.NewReader(jsonString))
decoder.DisallowUnknownFields()
err = decoder.Decode(&user)
In certain parts of my production code, like when passing untainted messages between microservices, I’m okay with ignoring properties that are unrecognized in some cases. This helps me if the provided JSON gets some new properties that I’m not yet equipped to handle, but want to parse the existing JSON using the schema that I do know about. However, in test code it’s appropriate to fail if the JSON string has an unrecognized property — this will help me catch a problem with construction of the JSON string, or perhaps a missing field within the type that I’m deserializing into. Hope this helps!