Skip to content

Additional JSON Schema Changes#123

Merged
djthorpe merged 4 commits into
mainfrom
djt/0331/jsonschema-2
Mar 31, 2026
Merged

Additional JSON Schema Changes#123
djthorpe merged 4 commits into
mainfrom
djt/0331/jsonschema-2

Conversation

@djthorpe
Copy link
Copy Markdown
Member

This pull request improves how JSON schema generation handles Go slice fields and enhances how example values are parsed for struct field tags. The main changes ensure that slice fields are never marked as nullable in the generated schema, aligning with JSON API conventions, and that example values are parsed more robustly.

Schema generation improvements:

  • Slice fields in struct types now have the "null" type removed from their schema definitions, ensuring they are always represented as arrays and never as nullable fields. This matches typical JSON API expectations, where absent slices are omitted or shown as empty arrays, but never as null.

  • Added a new test, TestFor_SliceField_NoNull, to verify that slice fields ([]string, []int, etc.) are not marked as nullable in the generated schema.

Example value parsing:

  • Improved parsing of the example struct tag: now attempts to unmarshal the tag value as raw JSON first, falling back to the previous method only if that fails. This allows more flexible and accurate example specification in struct tags.

@djthorpe djthorpe self-assigned this Mar 31, 2026
Copilot AI review requested due to automatic review settings March 31, 2026 12:21
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the pkg/jsonschema wrapper around the upstream JSON Schema generator to better align generated schemas with expected JSON API conventions (notably around slice nullability) and to make example struct-tag parsing more flexible.

Changes:

  • Strip "null" from generated schema types for Go slice struct fields.
  • Enhance example tag handling by attempting raw-JSON parsing before falling back to the existing typed parsing.
  • Add a regression test to ensure slice struct fields are not marked nullable.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pkg/jsonschema/jsonschema.go Adjusts schema enrichment: removes "null" for slice fields; improves example tag parsing via raw JSON attempt.
pkg/jsonschema/jsonschema_test.go Adds a test asserting slice field schemas don’t include "null".
Comments suppressed due to low confidence (1)

pkg/jsonschema/jsonschema.go:364

  • Parsing the example tag as raw JSON first can produce examples whose type doesn’t match the property type (e.g., a string field with example:"true" will now store boolean true, example:"null" becomes nil). Consider only accepting the raw-JSON path when it results in a value compatible with the schema type (or when the tag is explicitly a JSON string/object/array), otherwise fall back to marshalDefault to preserve expected typing/backward compatibility.
		if v := field.Tag.Get("example"); v != "" {
			var parsed any
			if err := json.Unmarshal([]byte(v), &parsed); err == nil {
				prop.Examples = append(prop.Examples, parsed)
			} else if raw := marshalDefault(field.Type, v); raw != nil {
				var decoded any
				if err := json.Unmarshal(raw, &decoded); err == nil {
					prop.Examples = append(prop.Examples, decoded)
				}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/jsonschema/jsonschema.go
Comment thread pkg/jsonschema/jsonschema_test.go
@djthorpe djthorpe merged commit 2d48317 into main Mar 31, 2026
@djthorpe djthorpe deleted the djt/0331/jsonschema-2 branch March 31, 2026 13:27
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.

2 participants