-
Notifications
You must be signed in to change notification settings - Fork 614
Fix JsonSerializerOptions without TypeInfoResolver #1221
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
Co-authored-by: GeorgiPopovIT <69841500+GeorgiPopovIT@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
| if (customOptions.TypeInfoResolver is null) | ||
| { | ||
| Debug.Assert(!customOptions.IsReadOnly, "If no resolver is present then the options must still be editable."); | ||
| customOptions.TypeInfoResolver = McpJsonUtilities.DefaultOptions.TypeInfoResolver; |
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.
@eiriktsarpalis presumably there's a race condition here, where by the time we go to set TypeInfoResolver, it's already been set and/or the instance has already been made read-only? Is the right answer for that to a) just accept/document that concurrent use may result in such overwrites and b) catch whatever exception would emerge when trying to set this on an already read-only instance?
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.
It's possible, although modifying the JSO while concurrently passing it to a serializer is a known source of errors and is not recommended. What bothers me most though is that this method could race with, say, JsonSerializer.Serialize which will try to assign it the default reflection-based resolver, or any other method that could try to set its own resolver following this convention.
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.
What do you think we should do, @eiriktsarpalis ?
Is going back to the original solution of creating a clone JSO the least-bad option?
Passing
JsonSerializerOptionswithout aTypeInfoResolvertoMcpServerTool.CreatethrowsInvalidOperationException. Users expect this to work like standardJsonSerializer.Serializemethods which silently populate the resolver.Based on review feedback from #1203 by @eiriktsarpalis.
Changes
GetSerializerOptionshelper inAIFunctionMcpServerToolthat populatesTypeInfoResolverfromMcpJsonUtilities.DefaultOptionswhen null, then callsMakeReadOnly()Example
Co-authored-by: GeorgiPopovIT 69841500+GeorgiPopovIT@users.noreply.github.com
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.