Skip to content

add Go struct and JSON schema sync test#173

Open
pradhyum6144 wants to merge 1 commit intomodelpack:mainfrom
pradhyum6144:feat-schema-struct-sync-checker
Open

add Go struct and JSON schema sync test#173
pradhyum6144 wants to merge 1 commit intomodelpack:mainfrom
pradhyum6144:feat-schema-struct-sync-checker

Conversation

@pradhyum6144
Copy link
Contributor

@pradhyum6144 pradhyum6144 commented Feb 24, 2026

Adds a test that uses Go reflection to automatically detect drift between Go struct fields and JSON schema properties. The test compares all five struct types (Model, ModelDescriptor, ModelConfig, ModelCapabilities, ModelFS) against their schema definitions and reports missing fields, extra properties, or type mismatches. This also fixes the missing reward and languages fields in the ModelCapabilities schema that the test caught. The test runs automatically with go test in CI, so any future struct changes without matching schema updates will be caught immediately.

Closes #91

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @pradhyum6144, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces an automated mechanism to maintain consistency between Go struct definitions and their JSON schema representations. By adding a new Go test that uses reflection, it ensures that any changes to core Go structs are reflected in the JSON schema, preventing schema drift. The PR also includes a fix for existing discrepancies in the ModelCapabilities schema, demonstrating the immediate value of this new validation.

Highlights

  • Automated Schema Validation Test Added: A new Go test (schema/sync_test.go) was introduced to automatically detect and prevent drift between Go struct definitions and their corresponding JSON schema properties.
  • Go Reflection for Schema Comparison: The test leverages Go reflection to compare five key struct types (Model, ModelDescriptor, ModelConfig, ModelCapabilities, ModelFS) against their schema definitions, checking for missing fields, extra properties, or type mismatches.
  • Schema Discrepancies Fixed: The schema/config-schema.json file was updated to include the previously missing reward (boolean) and languages (array of strings) fields within the ModelCapabilities schema, which were identified by this new test.
  • CI Integration: This new schema synchronization test is integrated into the CI pipeline, ensuring that any future inconsistencies between Go structs and their JSON schema definitions are caught automatically.
Changelog
  • schema/config-schema.json
    • Added reward property of type boolean to ModelCapabilities.
    • Added languages property of type array of strings to ModelCapabilities.
  • schema/sync_test.go
    • Added a new Go test file sync_test.go containing TestStructSchemaSync.
    • Implemented functions to parse JSON schema, map Go types to expected schema types, and compare them.
    • The test iterates through v1.Model, v1.ModelDescriptor, v1.ModelConfig, v1.ModelCapabilities, and v1.ModelFS to verify their schema synchronization.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a valuable test to ensure synchronization between Go structs and their corresponding JSON schema definitions, which is a great step for maintainability. The new test has already proven its worth by identifying and helping fix missing reward and languages fields in the ModelCapabilities schema. However, I've found a logical issue in the test's type-matching function that could cause it to miss certain kinds of schema drift. I've provided a suggestion to correct this, ensuring the test is more robust.

Comment on lines +140 to +149
if p.Type != e.typ {
return false
}
if e.format != "" && p.Format != e.format {
return false
}
if e.typ == "array" && e.items != nil && p.Items != nil {
return matchesExpected(*p.Items, *e.items)
}
return true
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The type matching logic in matchesExpected has a couple of issues that could cause it to incorrectly report a match, undermining the purpose of this test:

  1. Format Check: The check for format is one-sided. It will fail if the Go type expects a format and the schema doesn't have one, but it will incorrectly pass if the schema has a format that the Go type doesn't expect.
  2. Array Items Check: The check for array items is incomplete. If the Go type is an array of a specific type (e.g., []string), but the schema just defines "type": "array" without an items block, the function will incorrectly report a match.

This could lead to a false sense of security, as some schema drifts might not be detected. The suggested change corrects both of these issues by ensuring the format matches exactly and by properly validating the presence and type of array items.

	if p.Type != e.typ || p.Format != e.format {
		return false
	}
	if e.typ == "array" {
		if (p.Items == nil) != (e.items == nil) {
			return false // Mismatch: one defines items, the other doesn't.
		}
		if p.Items != nil { // implies e.items is also not nil
			return matchesExpected(*p.Items, *e.items)
		}
	}
	return true

@pradhyum6144 pradhyum6144 force-pushed the feat-schema-struct-sync-checker branch 3 times, most recently from e726a11 to 998e54c Compare February 25, 2026 08:49
@aftersnow
Copy link
Contributor

Hi @pradhyum6144, thanks for working on this! The idea of using Go reflection to catch struct-schema drift is really nice and directly addresses #91.

Before we can merge, there are a couple of things that need to be addressed:

  1. Please rebase onto the latest main. The reward and languages fields (along with additionalProperties: false and the pattern constraint on languages) were already added to the schema in a prior commit. The schema changes in this PR would actually revert those additions, so please drop the config-schema.json changes and only keep the new test file.

  2. CI is currently failing — please fix the Lint and Classify PR checks. Also, Gemini flagged a valid issue with matchesExpected: the format check is one-sided, which could cause false passes. Worth fixing that too.

Looking forward to the updated version!

@aftersnow aftersnow added the enhancement New feature or request label Mar 20, 2026
@pradhyum6144 pradhyum6144 force-pushed the feat-schema-struct-sync-checker branch from 998e54c to 81d31cb Compare March 20, 2026 11:09
… JSON schema

Signed-off-by: pradhyum6144 <pradhyum314@gmail.com>
@pradhyum6144 pradhyum6144 force-pushed the feat-schema-struct-sync-checker branch from 81d31cb to 1a38473 Compare March 20, 2026 11:12
@pradhyum6144
Copy link
Contributor Author

Hi @pradhyum6144, thanks for working on this! The idea of using Go reflection to catch struct-schema drift is really nice and directly addresses #91.

Before we can merge, there are a couple of things that need to be addressed:

1. **Please rebase onto the latest `main`.** The `reward` and `languages` fields (along with `additionalProperties: false` and the `pattern` constraint on `languages`) were already added to the schema in a prior commit. The schema changes in this PR would actually revert those additions, so please drop the `config-schema.json` changes and only keep the new test file.

2. **CI is currently failing** — please fix the Lint and Classify PR checks. Also, Gemini flagged a valid issue with `matchesExpected`: the `format` check is one-sided, which could cause false passes. Worth fixing that too.

Looking forward to the updated version!

Hi @aftersnow sir, I've addressed your review feedback:

Rebased onto latest main and dropped the config-schema.json changes only sync_test.go remains in this PR now.
Fixed the matchesExpected function the $ref check is now bidirectional (e.ref != "" || p.Ref != "") so it catches drift in both directions, as Gemini flagged.
CI checks are passing now.
Please take another look when you get a chance. Thanks for the review!

pradhyum6144 added a commit to pradhyum6144/model-spec that referenced this pull request Mar 23, 2026
Extends the 2-way sync checker from PR modelpack#173 to catch drift in three
directions at once:

1. Go structs ↔ JSON Schema: reflection-based field/type comparison
2. JSON Schema ↔ Doc example: verifies docs/config.md JSON example
   covers every schema field (and vice versa)
3. Doc descriptions ↔ JSON Schema: parses markdown property
   definitions and cross-checks against schema properties

15 subtests across 3 test functions. Prevents silent spec mismatches
when fields are added, removed, or renamed in any of the three sources.

Signed-off-by: pradhyum6144 <pradhyum314@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ci should not pass when new fields are added in GO structs but json schema is not corresponding changed

2 participants