Skip to content

Omit zero-valued data from marshalled APIResponse - #46

Merged
johnmaguire merged 4 commits into
mainfrom
fix-error-response-data
Oct 19, 2026
Merged

johnmaguire merged 4 commits into
mainfrom
fix-error-response-data

Conversation

@johnmaguire

@johnmaguire johnmaguire commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

What

Tags APIResponse.Data with omitzero and APIResponse.Errors with omitempty, 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 data or errors - success responses have no errors key, and error responses have no data key. Go test servers marshalling an error-only APIResponse emitted a zero-valued data object alongside the errors. Its empty network.curve then failed NetworkCurve validation when the client decoded the response, turning the intended API errors into:

unexpected error during enrollment: error decoding JSON response: invalid network curve
body: {"data":{...},"errors":[{"code":"ERR_UNEXPECTED_HOSTNAME",...}]}

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.
@johnmaguire
johnmaguire marked this pull request as ready for review July 29, 2026 17:58
@johnmaguire
johnmaguire requested a review from nbrownus July 29, 2026 17:59
nbrownus
nbrownus previously approved these changes Oct 16, 2026

@nbrownus nbrownus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 jasikpark left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@johnmaguire
johnmaguire requested a review from jasikpark October 19, 2026 21:53

@jasikpark jasikpark left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — EqualError pins it. LGTM.

— Caleb + Cache

@johnmaguire
johnmaguire merged commit 295bb7f into main Oct 19, 2026
3 checks passed
@johnmaguire
johnmaguire deleted the fix-error-response-data branch October 19, 2026 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants