-
Notifications
You must be signed in to change notification settings - Fork 648
fix(go): include Type property in seen struct types
#4216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @hugoaguirre, 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 addresses an issue in the JSON schema inference logic where previously encountered struct types were not explicitly marked as "object" in the generated schema. By adding the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe 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
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 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
|
Type property in seen struct types
There was a problem hiding this 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 aims to fix an issue where the JSON schema for a seen struct type was missing the type: "object" property. The change correctly adds this property in go/internal/base/json.go and updates/adds tests in go/internal/base/json_test.go to verify this behavior.
While the change is an improvement for standard structs, I've identified a significant edge case where it can produce an incorrect schema for structs that implement json.Marshaler. My review includes a comment with a suggested fix to handle this case correctly. Otherwise, the changes are logical and the tests are well-written.
| return &jsonschema.Schema{ | ||
| Type: "object", | ||
| AdditionalProperties: jsonschema.TrueSchema, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change correctly adds Type: "object" for seen structs, making the schema more specific. However, this introduces an issue for struct types that implement json.Marshaler to produce a non-object JSON representation (e.g., a string or number).
For such types, this logic will incorrectly claim the type is object. For example:
type MyStringer struct { Value string }
func (s MyStringer) MarshalJSON() ([]byte, error) { return json.Marshal(s.Value) }
type Container struct {
A MyStringer
B MyStringer
}When inferring the schema for Container, the schema for the second occurrence of MyStringer (field B) will be incorrectly marked as type: "object".
To fix this, we should check if the struct implements json.Marshaler and, if so, fall back to a more generic schema that doesn't specify the type, which is consistent with how jsonschema handles marshalers.
var marshalerType = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
if baseType.Implements(marshalerType) || reflect.PtrTo(baseType).Implements(marshalerType) {
return &jsonschema.Schema{
AdditionalProperties: jsonschema.TrueSchema,
}
}
return &jsonschema.Schema{
Type: "object",
AdditionalProperties: jsonschema.TrueSchema,
}
Checklist (if applicable):