Skip to content

Conversation

@eiixy
Copy link
Contributor

@eiixy eiixy commented Jul 10, 2025

This PR implements $ref and $defs support in the JSON Schema validator.
Key features include:

  • Resolves $ref references recursively.
  • Supports nested $defs and path resolution.

@codecov
Copy link

codecov bot commented Jul 10, 2025

Codecov Report

Attention: Patch coverage is 90.76923% with 6 lines in your changes missing coverage. Please review.

Project coverage is 86.30%. Comparing base (c125ae2) to head (b1e53c7).
Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
jsonschema/json.go 72.72% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1030      +/-   ##
==========================================
+ Coverage   85.83%   86.30%   +0.46%     
==========================================
  Files          43       43              
  Lines        2316     2387      +71     
==========================================
+ Hits         1988     2060      +72     
+ Misses        305      302       -3     
- Partials       23       25       +2     

☔ View full report in Codecov by Sentry.
📢 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.

Copy link
Owner

@sashabaranov sashabaranov left a comment

Choose a reason for hiding this comment

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

Wow, this is super cool! Thanks!

@sashabaranov sashabaranov requested a review from Copilot July 10, 2025 10:30
Copy link
Contributor

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 adds full support for $ref and $defs resolution to the JSON Schema validator, updates schema generation to emit reusable definitions, and extends tests accordingly.

  • Implements CollectDefs to gather all $defs into a map for lookup
  • Updates Validate and VerifySchemaAndUnmarshal to accept and use resolved definitions
  • Enhances GenerateSchemaForType and reflection logic to emit $ref for named structs and populate $defs
  • Adds tests for reference resolution, $defs collection, and schema generation with $ref/$defs

Reviewed Changes

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

File Description
jsonschema/validate.go Added CollectDefs, extended Validate to handle $ref, and updated VerifySchemaAndUnmarshal to wire in definitions
jsonschema/validate_test.go Added tests for recursive $ref validation and CollectDefs functionality
jsonschema/json.go Extended GenerateSchemaForType and reflection routines to emit $ref/$defs
jsonschema/json_test.go Added schema-generation test verifying $ref and $defs output
Comments suppressed due to low confidence (1)

jsonschema/validate.go:37

  • Unmarshalling into &v writes into the local interface variable rather than the caller’s target. Change this to json.Unmarshal(content, v) so the provided pointer is populated correctly.
	return json.Unmarshal(content, &v)

@sashabaranov sashabaranov merged commit c650976 into sashabaranov:master Jul 10, 2025
3 checks passed
@sashabaranov
Copy link
Owner

@eiixy seems like this PR broke an integration test https://github.com/sashabaranov/go-openai/actions/runs/16192799414/job/45712241252

@eiixy
Copy link
Contributor Author

eiixy commented Jul 10, 2025

@eiixy seems like this PR broke an integration test https://github.com/sashabaranov/go-openai/actions/runs/16192799414/job/45712241252

The issue is that using a $ref on the root schema (in a JSON Schema parsing/validation context) breaks proper subschema resolution. I’ll fix this later today—removing the root $ref and updating the parser/validator to align with standard reference resolution.

Rosenberg96 pushed a commit to lunarway/go-openai that referenced this pull request Aug 18, 2025
* support $ref and $defs in JSON Schema

* update
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