Additional JSON Schema Changes#123
Merged
Merged
Conversation
There was a problem hiding this comment.
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
exampletag 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
exampletag as raw JSON first can produce examples whose type doesn’t match the property type (e.g., a string field withexample:"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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 asnull.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:
examplestruct 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.