Skip to content

fix: preserve JSONSchemaExtend description; support {m,n} in tag patterns#191

Open
nghiack7 wants to merge 1 commit into
invopop:mainfrom
nghiack7:fix/tag-parsing-issues
Open

fix: preserve JSONSchemaExtend description; support {m,n} in tag patterns#191
nghiack7 wants to merge 1 commit into
invopop:mainfrom
nghiack7:fix/tag-parsing-issues

Conversation

@nghiack7

@nghiack7 nghiack7 commented May 9, 2026

Copy link
Copy Markdown

Two related tag-parsing bugs, both in reflect.go:


Bug 1: JSONSchemaExtend description overwritten (#169)

structKeywordsFromTags unconditionally assigned jsonschema_description to t.Description, even when the tag was absent:

// before
t.Description = f.Tag.Get("jsonschema_description")  // "" when absent → erases prior description

When a type implements JSONSchemaExtend and sets a field's description, the property schema is first built by refOrReflectTypeToSchema (which calls JSONSchemaExtend), then structKeywordsFromTags runs and silently clears the description with an empty string.

Fix: only assign when the tag is non-empty:

if desc := f.Tag.Get("jsonschema_description"); desc != "" {
    t.Description = desc
}

Bug 2: Regex quantifiers {m,n} truncated in pattern tags (#181)

splitOnUnescapedCommas split on all unescaped commas, including those inside regex quantifiers like {0,2}:

// jsonschema:"pattern=^[A-Z]{0,3}$,maxLength=10"
// → pattern value truncated to "^[A-Z]{0"

The old backslash-escape convention (\,) still worked, but required users to manually escape every comma inside quantifiers, which was non-obvious and undocumented.

Fix: rewrite splitOnUnescapedCommas to track brace-nesting depth and skip commas inside {…}. The backslash-escape convention is still supported for backwards compatibility.


Tests added

  • TestSplitOnUnescapedCommas: 3 new cases covering {m,n}, {1,{2,3}} nesting
  • TestDescriptionPreservedFromJSONSchemaExtend: verifies description from JSONSchemaExtend survives structKeywordsFromTags
  • TestPatternWithQuantifierComma: end-to-end schema generation with pattern=^[A-Z]{0,3}$

All existing tests pass.

Two related bugs in tag parsing:

1. (invopop#169) structKeywordsFromTags always assigned jsonschema_description
   to t.Description, even when the tag was absent (empty string). This
   silently overwrote descriptions set earlier by JSONSchemaExtend.
   Fix: only assign when the tag is non-empty.

2. (invopop#181) splitOnUnescapedCommas split on commas inside regex quantifiers
   like {0,2}, truncating the pattern value. The existing backslash-escape
   convention (\,) worked, but required users to manually escape every comma
   in their regex, which is unintuitive.
   Fix: rewrite the function to track brace-nesting depth and skip commas
   inside {…}. The backslash-escape convention is still supported.

Add tests:
- TestSplitOnUnescapedCommas: three new cases covering {m,n} quantifiers
- TestDescriptionPreservedFromJSONSchemaExtend: verifies description from
  JSONSchemaExtend is not overwritten by an absent jsonschema_description tag
- TestPatternWithQuantifierComma: end-to-end test generating a schema with
  pattern=^[A-Z]{0,3}$ and verifying the full pattern reaches the output
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.

1 participant