Repository navigation
Omit zero-valued data from marshalled APIResponse - #46
Conversation
The API never sends data alongside errors, but Go test servers marshalling an error-only APIResponse emitted a zero-valued data object. Its empty network curve failed NetworkCurve validation on the client, turning the intended API errors into a JSON decode failure.
nbrownus
left a comment
There was a problem hiding this comment.
Nit: the 5-line comment on APIResponse could be one line, something like // The API sends exactly one of data or errors. The test comments restate the PR description and could go too.
jasikpark
left a comment
There was a problem hiding this comment.
Looks good: omitzero is the right tag for the generic Data T. Ready to approve once the test below is tightened.
One request before merge: TestEnroll's error case (client_test.go:173) gave a false pass on this exact bug. require.Errorf's second argument is a failure message, not an expected value, so it passed on the invalid network curve decode error. ErrorContains wouldn't catch it either, because the decode error echoes the body. Swapping to
require.EqualError(t, err, "unexpected error during enrollment: "+errorMsg)fails on main and passes here, so it pins the fix end-to-end through the client.
— Caleb + Cache
require.Errorf treats its second argument as a failure message, so the error case passed on the decode error that a zero-valued data object produced. EqualError fails on that and passes on the intended API error.
jasikpark
left a comment
There was a problem hiding this comment.
Thanks — EqualError pins it. LGTM.
— Caleb + Cache
What
Tags
APIResponse.DatawithomitzeroandAPIResponse.Errorswithomitempty, so marshalling omits whichever side is unset. Adds round-trip tests for both the error-only and data-only cases.Why
The API sends exactly one of
dataorerrors- success responses have noerrorskey, and error responses have nodatakey. Go test servers marshalling an error-onlyAPIResponseemitted a zero-valueddataobject alongside the errors. Its emptynetwork.curvethen failedNetworkCurvevalidation when the client decoded the response, turning the intended API errors into: