Skip to content

Fix fieldName to strip JSON tag options, matching JSONSchema - #219

Merged
cinar merged 2 commits into
mainfrom
fix-fieldname-json-tag-options
Sep 5, 2026
Merged

Fix fieldName to strip JSON tag options, matching JSONSchema#219
cinar merged 2 commits into
mainfrom
fix-fieldname-json-tag-options

Conversation

@cinar

@cinar cinar commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Problem

checker.go's fieldName uses a field's json tag value verbatim for the CheckErrors map key:

if jsonTag, ok := field.Tag.Lookup("json"); ok {
	name = jsonTag
}

A field tagged json:"email,omitempty" produces the error key "email,omitempty" instead of "email". A field tagged json:"-" produces the literal key "-".

schema.go's jsonPropertyName already handles this correctly (strips ,omitempty-style options via strings.Cut, treats "-" as "no JSON name"), so CheckStruct's error keys and JSONSchema's property names disagreed for any field with tag options — verified this before writing the fix.

Fix

fieldName now calls the existing jsonPropertyName helper instead of duplicating (and getting wrong) its own parsing. When jsonPropertyName reports false (the json:"-" case), fieldName falls back to the Go field name — a CheckErrors key always needs some name, whereas a schema property can simply be omitted for such a field, so the two call sites intentionally handle that case differently.

Testing

  • Added TestCheckStructCustomNameWithOptions (json:"name,omitempty" → key "name") and TestCheckStructJSONIgnoredFieldFallsBackToFieldName (json:"-" → key "Name").
  • Full suite passes, 100% coverage maintained.
  • go vet, gosec clean.

Fixes #194

🤖 Generated with Claude Code

https://claude.ai/code/session_01FLdVmP5daHiknrTW4Geh2i

fieldName used a field's json tag value as-is for the CheckErrors map
key. A field tagged json:"name,omitempty" produced the error key
"name,omitempty" instead of "name", and json:"-" produced the literal
key "-" instead of falling back to the Go field name. schema.go's
jsonPropertyName already parsed json tags correctly (strips options,
treats "-" as "no JSON name"), so CheckStruct's error keys and
JSONSchema's property names disagreed for any field with tag options.

fieldName now calls the same jsonPropertyName helper JSONSchema uses,
falling back to the Go field name when the tag says json:"-" (a
CheckErrors key always needs some name, unlike a schema property which
can simply be omitted for such a field).

Fixes #194

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FLdVmP5daHiknrTW4Geh2i
@codecov

codecov Bot commented Sep 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (05bb006) to head (4005bfa).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #219   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           52        52           
  Lines          896       896           
=========================================
  Hits           896       896           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cinar
cinar enabled auto-merge (squash) September 5, 2026 16:31
@cinar
cinar merged commit ce3aaf2 into main Sep 5, 2026
5 checks passed
@cinar
cinar deleted the fix-fieldname-json-tag-options branch September 5, 2026 16:33
cinar added a commit that referenced this pull request Sep 5, 2026
## Problem

Every PR added its own bullet to `CHANGELOG.md`'s `## [Unreleased]`
section, always inserting at (or near) the same spot. With PRs landing
close together, that made this file a near-guaranteed merge conflict on
every concurrent pull request — the exact problem `DOC.md` caused before
#180 dropped it in favor of pkg.go.dev.

This wasn't theoretical: it's what just happened resolving conflicts on
#219#222. Each conflict resolution round immediately conflicted again
as the next PR merged, because every remaining branch's `### Fixed`
entry inserted into the same place.

## Fix

- Freeze the current `## [Unreleased]` section as a one-time snapshot
(documenting the six P0 bugfixes plus the earlier accumulated
Added/Changed/Removed entries) and add a note explaining the new policy
at the top of the file.
- Going forward, unreleased changes are covered by [GitHub
Releases](https://github.com/cinar/checker/releases), whose notes are
generated automatically from merged pull requests — no file to conflict
on. `CHANGELOG.md` only gains a new entry when a version is actually
tagged, added by hand from that release's generated notes (a maintainer
decision, not a per-PR one).
- Updated `CLAUDE.md`'s conventions section: PRs no longer touch
`CHANGELOG.md`.

No code changes; docs/process only.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01FLdVmP5daHiknrTW4Geh2i

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
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.

fieldName doesn't strip JSON tag options, producing wrong error keys

1 participant