-
-
Notifications
You must be signed in to change notification settings - Fork 2
fix: MeaiFunction type handling added more robust codes #29
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
Conversation
…vidual methods to Tools.
…emaAttribute.cs and FunctionToolAttribute.cs
# Conflicts: # src/tests/CSharpToJsonSchema.AotTests/JsonSerializationTests.cs
Replaced direct dictionary access with TryGetValue to prevent potential KeyNotFound exceptions. This ensures safer and more robust handling of missing keys when fetching "mainFunction_Desc".
Removed unnecessary try-catch block around async call invocation and adjusted formatting for better code clarity. Minor whitespace and indentation improvements were also made to enhance consistency and maintainability.
Added detailed XML documentation for MeaiFunction class and its members, improving code readability and maintainability. Introduced support for strict mode and updated method implementations to enhance functionality. These changes provide better structure and guidance for future development.
Replaced GenericFunctionTool with GoogleFunctionTool to align with the updated namespace and class structure. This ensures compatibility with the latest library changes and maintains consistency in functionality.
# Conflicts: # CSharpToJsonSchema.sln # src/libs/CSharpToJsonSchema/MeaiFunction.cs
Generated snapshot files for ToolsJsonSerializerContext to support integration tests for various tool methods and their serialization. This includes handling for different types of arguments like string, void, and async inputs.
The `MethodFunction` test method is commented out, likely due to being unused or obsolete. This change improves code clarity by removing an inactive test without deleting it entirely, preserving it for potential future use.
# Conflicts: # CSharpToJsonSchema.sln # src/tests/AotConsole/Services/StudentRecordService.cs
refactor: Simplified JsonSchema generation codes
# Conflicts: # src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Tools.generated.verified.cs # src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Tools.generated.verified.cs # src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Tools.generated.verified.cs
|
Gunpal Jain seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
WalkthroughThe changes update the solution’s Visual Studio version and project configurations, add a new conversion method in the model generator, and modify the instantiation of AI tool functions with an extra JSON serializer options parameter. The constructor of a key function class now accepts optional JSON options, with an added internal method to initialize default settings. Additionally, several auto-generated snapshot and serialization files in the tests have been removed, and the test helper now automatically verifies outputs. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant MeaiFunction
participant SerializerOptions
Note over Caller,MeaiFunction: Instance Creation
Caller->>MeaiFunction: new MeaiFunction(tool, call, [options])
alt Options Provided
MeaiFunction->>MeaiFunction: Store provided options in _options
else Options Null
MeaiFunction->>MeaiFunction: Call InitializeReflectionOptions()
MeaiFunction-->>SerializerOptions: Create default JSON options
MeaiFunction->>MeaiFunction: Assign default options to _options
end
Caller->>MeaiFunction: Invoke GetArgsString(arguments)
MeaiFunction->>SerializerOptions: Serialize arguments with _options
sequenceDiagram
participant Test as UnitTest
participant TestHelper
participant Verifier
Note over Test,TestHelper: Automatic Verification Process
Test->>TestHelper: CheckSourceAsync(resource)
TestHelper->>Verifier: verifier.Verify(diagnostics)
TestHelper->>Verifier: verifier.Verify(driver)
Verifier-->>TestHelper: Verification results
TestHelper-->>Test: Return verification outcome
Poem
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/tests/CSharpToJsonSchema.MeaiTests/Meai_Tests.cs (1)
50-53: AI client implementation switched from OpenAI to GoogleThe code now uses Google's GenerativeAIChatClient instead of OpenAI, which aligns with the package addition in the project file. The environment variable access pattern remains consistent with the rest of the code.
Consider removing commented code once the new implementation is confirmed to be working correctly.
-//Microsoft.Extensions.AI.OpenAIChatClient openAiClient = new OpenAIChatClient(client.GetChatClient("gpt-4o-mini")); - var chatClient = new GenerativeAIChatClient(Environment.GetEnvironmentVariable("GOOGLE_API_KEY",EnvironmentVariableTarget.User)); -//var chatClient = new Microsoft.Extensions.AI.FunctionInvokingChatClient(openAiClient);src/libs/CSharpToJsonSchema/MeaiFunction.cs (1)
123-156: Enhanced type handling for JSON serializationThe implementation now handles various JSON types (JsonValue, JsonObject, JsonArray) and has a more robust fallback for complex types. This significantly improves the flexibility of the function.
However, there's a potential issue with null handling in the type check.
Consider improving the null handling in the type check. The current code might throw a NullReferenceException if args.Value is null when checking if the type is primitive.
- var type = args.Value?.GetType(); - if(type.IsPrimitive) + var type = args.Value?.GetType(); + if(type?.IsPrimitive == true)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (70)
CSharpToJsonSchema.sln(3 hunks)src/libs/CSharpToJsonSchema.Generators/Conversion/ToModels.cs(1 hunks)src/libs/CSharpToJsonSchema.Generators/Sources.Method.MeaiTools.cs(2 hunks)src/libs/CSharpToJsonSchema/MeaiFunction.cs(6 hunks)src/tests/CSharpToJsonSchema.MeaiTests/CSharpToJsonSchema.MeaiTests.csproj(1 hunks)src/tests/CSharpToJsonSchema.MeaiTests/Meai_Tests.cs(2 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/SnapshotTests.cs(1 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.PropertyNames.g.received.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.PropertyNames.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_NoReturnAsyncArgs.g.received.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_NoReturnAsyncArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_NoReturnAsyncArgs.g.received.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_NoReturnAsyncArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_StringArgs.g.received.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_StringArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_StringAsyncArgs.g.received.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_StringAsyncArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_VoidArgs.g.received.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_VoidArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_StringArgs.g.received.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_StringArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_StringAsyncArgs.g.received.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_StringAsyncArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_VoidArgs.g.received.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_VoidArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.String.g.received.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.String.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Boolean.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Calls.generated.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.DateOnly.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.DateTime.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Double.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.GetCurrentWeather3Args.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.GetJsonTypeInfo.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.GetValueArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.GetValueAsyncArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Int32.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Int64.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.PropertyNames.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.SetValueArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.SetValueAsyncArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Single.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Tools.generated.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes_Diagnostics.verified.txt(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Calls.generated.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Double.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.GetCurrentWeatherArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.GetCurrentWeatherAsyncArgs.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.GetJsonTypeInfo.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.PropertyNames.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.String.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Tools.generated.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Unit.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Weather.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Calls.generated.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Double.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.GetCurrentWeather2Args.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.GetCurrentWeatherAsync2Args.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.GetJsonTypeInfo.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.PropertyNames.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.String.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Tools.generated.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Unit2.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Weather2.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.g.verified.cs(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict_Diagnostics.verified.txt(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather_Diagnostics.verified.txt(0 hunks)src/tests/CSharpToJsonSchema.SnapshotTests/TestHelper.cs(1 hunks)
💤 Files with no reviewable changes (62)
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_NoReturnAsyncArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.String.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.PropertyNames.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_VoidArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.PropertyNames.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_VoidArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_StringAsyncArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_StringArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_StringArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_NoReturnAsyncArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather_Diagnostics.verified.txt
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes_Diagnostics.verified.txt
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.PropertyNames.g.received.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.GetJsonTypeInfo.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.SetValueArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.PropertyNames.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict_Diagnostics.verified.txt
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_StringAsyncArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.String.g.received.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.SetValueAsyncArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.GetValueArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Tools.generated.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Double.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_StringAsyncArgs.g.received.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Double.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Double.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Boolean.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_VoidArgs.g.received.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.GetValueAsyncArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_StringAsyncArgs.g.received.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Tools.generated.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_NoReturnAsyncArgs.g.received.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Unit2.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.GetJsonTypeInfo.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Weather2.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_StringArgs.g.received.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.DateOnly.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.GetCurrentWeather3Args.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_NoReturnAsyncArgs.g.received.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.String.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.GetCurrentWeatherArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.GetJsonTypeInfo.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.PropertyNames.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Weather.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Unit.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Single.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.GetCurrentWeatherAsync2Args.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.GetCurrentWeatherAsyncArgs.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.GetCurrentWeather2Args.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Tools.generated.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Calls.generated.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.DateTime.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Calls.generated.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Int32.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Calls.generated.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_Static_StringArgs.g.received.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.String.g.verified.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.MethodFunction#ToolsJsonSerializerContext.SampleFunctionTool_VoidArgs.g.received.cs
- src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Int64.g.verified.cs
🔇 Additional comments (16)
src/tests/CSharpToJsonSchema.SnapshotTests/TestHelper.cs (2)
35-36: Excellent enhancement to test automation!Enabling automatic verification with
.AutoVerify()streamlines the testing workflow by automating the verification step for diagnostic outputs.
41-42: Good consistency in automation approach!Applying the same
.AutoVerify()pattern to the driver verification maintains consistency and ensures both verification processes follow the same automated approach.src/tests/CSharpToJsonSchema.SnapshotTests/SnapshotTests.cs (1)
6-10: Great addition to test coverage!The new
MethodFunction()test method follows the established pattern of other tests in this class and properly tests the method function capabilities mentioned in the PR objectives. This improves test coverage for the "MeaiFunction type handling" feature.src/tests/CSharpToJsonSchema.MeaiTests/CSharpToJsonSchema.MeaiTests.csproj (1)
12-12: Package addition looks good for Google Generative AI supportThe added Google_GenerativeAI.Microsoft package aligns with the code changes in the test file where OpenAI has been replaced with Google's Generative AI implementation.
src/libs/CSharpToJsonSchema.Generators/Conversion/ToModels.cs (1)
139-269: Well-structured implementation for type conversionThe new
ToParameterDatamethod is a comprehensive implementation for converting C# type symbols to OpenAPI schema representations. It handles various TypeKind cases (Enum, Structure, Class, Interface, Array) appropriately, determining the correct schema type and format for each.The method effectively:
- Processes primitive types with appropriate formats
- Handles complex object types by recursively processing their properties
- Manages array types and their item schemas
- Preserves enum values
- Correctly determines nullability
src/tests/CSharpToJsonSchema.MeaiTests/Meai_Tests.cs (1)
3-3: Added namespace for Google's Generative AIThe import is consistent with the transition from OpenAI to Google's Generative AI implementation.
CSharpToJsonSchema.sln (3)
2-3: Updated Visual Studio versionThe solution has been updated to Visual Studio 17.12, which is appropriate for using the latest IDE features.
27-27: Added Dependabot configurationAdding Dependabot configuration is a good practice for automated dependency management.
84-89: Added configuration settings for projectsRelease configurations have been added for the CSharpToJsonSchema.MeaiTests project, and both Debug and Release configurations for the AotConsole project. This ensures build configurations are complete for all projects.
src/libs/CSharpToJsonSchema.Generators/Sources.Method.MeaiTools.cs (2)
33-33: Updated MeaiFunction constructor to include serialization optionsThe constructor call now includes the serialization options from the generated JsonSerializerContext. This aligns with the updated MeaiFunction constructor signature and ensures consistent JSON serialization behavior across the application.
62-62: Updated MeaiFunction constructor in interface extension methodSimilar to the previous change, this properly updates the MeaiFunction constructor call to include serialization options from the generated context, ensuring consistent JSON handling across different extension methods.
src/libs/CSharpToJsonSchema/MeaiFunction.cs (5)
4-5: Added necessary JSON serialization namespacesAdded imports for JSON serialization features that are required for the enhanced type handling functionality.
35-35: Added private field for JSON serialization optionsThis field will store the JSON serialization options passed to the constructor, enabling consistent serialization behavior throughout the class.
47-62: Updated constructor to accept optional serialization optionsThe constructor now accepts a JsonSerializerOptions parameter, allowing for customization of JSON serialization behavior. This is a valuable enhancement that improves flexibility.
104-104: Changed GetArgsString to protected virtualMaking this method protected and virtual is a good design decision as it allows derived classes to customize the JSON serialization behavior for arguments.
65-81:Details
❓ Verification inconclusive
Added fallback initialization method for JSON serialization options
This method provides a fallback for creating serialization options when not explicitly provided, which is good for backward compatibility. However, the comment about breaking AOT compatibility is concerning.
The code includes a warning about AOT compatibility issues when using reflection. Verify that this won't cause problems in environments that rely on AOT compilation, such as mobile or serverless platforms.
🏁 Script executed:
#!/bin/bash # Check for AOT compilation directives or mentions in the project echo "Checking for AOT compilation directives in the project..." rg -i "aot|nativeaot|trimming|PublishAot" --type csproj --type csLength of output: 223
Let's re-run the search using glob patterns so that both C# and csproj files are examined. This should avoid the "unrecognized file type" error.
🏁 Script executed:
#!/bin/bash echo "Re-checking for AOT-related keywords in *.cs and *.csproj files..." rg -i "aot|nativeaot|trimming|PublishAot" -g "*.csproj" -g "*.cs"Length of output: 1879
AOT Compatibility Verification Required: Update Fallback Initialization for JSON Options
The fallback method for initializing JSON serialization options maintains backward compatibility but relies on reflection (e.g., using
JsonStringEnumConverter), which is known to break in AOT environments. Notably, there’s an inline comment insrc/libs/CSharpToJsonSchema/MeaiFunction.csand diagnostics in other parts of the repository (e.g., in the source generator diagnostic descriptors and AOT-specific project configurations) that confirm these concerns.
- Verify that this fallback mechanism won’t cause runtime issues on AOT-based platforms (mobile/serverless).
- Consider enforcing the provision of an explicit
JsonSerializerOptionsinstance in AOT scenarios or add runtime checks to fail fast in incompatible environments.
| if (_options == null) | ||
| { | ||
| //Fallback to Reflection | ||
| //This will break the AOT, Hoping for the best, IChatClient implementation only send JSON classes | ||
| //Or Developer is using the code generator | ||
| _options = InitializeReflectionOptions(); | ||
| } |
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.
💡 Verification agent
🧩 Analysis chain
Added reflection fallback with explicit warning
The code includes a fallback to reflection-based serialization with explicit warning comments about AOT compatibility issues. While this provides good backward compatibility, it may lead to runtime errors in AOT environments.
Verify whether this reflection fallback is actually needed or if all scenarios can be covered by passing proper JsonSerializerOptions with type information. If reflection is truly needed, consider adding a feature flag to disable it in AOT environments.
🌐 Web query:
What are best practices for handling System.Text.Json serialization in AOT compilation environments?
💡 Result:
To handle System.Text.Json serialization effectively in AOT compilation environments, follow these best practices:
1. Use Source Generators
Leverage System.Text.Json source generators to avoid reflection, which isn't AOT-compatible. Source generators precompute serialization logic at compile time, ensuring type metadata isn't trimmed.
Example setup:
[JsonSerializable(typeof(MyModel))]
public partial class MyContext : JsonSerializerContext { }
var options = new JsonSerializerOptions {
TypeInfoResolver = MyContext.Default
};
var json = JsonSerializer.Serialize(model, options);This eliminates runtime reflection and supports trimming/Native AOT[1][3][7][10].
2. Define a JsonSerializerContext
Create a context class annotated with [JsonSerializable] for all serializable types:
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Book))]
[JsonSerializable(typeof(Bookcase))]
public partial class AppJsonContext : JsonSerializerContext { }Register it globally in ASP.NET Core:
builder.Services.ConfigureHttpJsonOptions(options => {
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonContext.Default);
});This ensures type information is preserved during trimming[2][13][14].
3. Avoid Reflection-Based APIs
Disable reflection defaults to enforce AOT safety:
<PropertyGroup>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>This throws InvalidOperationException if reflection is accidentally used[10].
4. Handle Polymorphism with Attributes
Use [JsonPolymorphic] and [JsonDerivedType] for AOT-safe inheritance:
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[JsonDerivedType(typeof(TextItem), "text")]
[JsonDerivedType(typeof(ImageItem), "image")]
public abstract class ItemModel { }
// Context must include all derived types
[JsonSerializable(typeof(ItemModel))]
[JsonSerializable(typeof(TextItem))]
public partial class ItemContext : JsonSerializerContext { }This replaces reflection-based type resolution[12][7].
5. Configure Serialization Options via Attributes
Set options at compile time using JsonSourceGenerationOptions:
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
)]
[JsonSerializable(typeof(WeatherForecast))]
public partial class WeatherContext : JsonSerializerContext { }This avoids runtime configuration overhead[10][13].
6. Handle Enums as Strings
Use JsonStringEnumConverter<TEnum> for AOT-compatible enum serialization:
[JsonConverter(typeof(JsonStringEnumConverter<MyEnum>))]
public enum MyEnum { Value1, Value2 }
[JsonSerializable(typeof(MyEnum))]
public partial class EnumContext : JsonSerializerContext { }Avoid the non-generic JsonStringEnumConverter[7][10].
7. Follow Model Constraints
- Use partial classes: Required for source-generated types[1][9].
- Parameterless constructors: Mandatory for deserialization[1][9].
- Limit collections: Stick to
List<T>,HashSet<T>, andDictionary<TKey, TValue>[1]. - Avoid generics: Not supported by some AOT-focused libraries like Chickensoft Serialization[1].
8. Performance and Size Optimization
- Metadata mode: Reduces generated code size by omitting serialization logic[8].
- Trimming: Ensure all runtime-accessed types are explicitly included in contexts[4][9].
- Migrate from Newtonsoft.Json: Replace reflection-heavy serializers with
System.Text.Json[5][9].
Example: AOT-Compatible Serialization
// Model
[JsonSerializable(typeof(Book))]
public partial class BookContext : JsonSerializerContext { }
public record Book(
[property: JsonPropertyName("title")] string Title,
[property: JsonPropertyName("author")] string Author
);
// Serialization
var options = new JsonSerializerOptions {
TypeInfoResolver = BookContext.Default
};
string json = JsonSerializer.Serialize(book, options);Output includes AOT-safe type discriminators:
{
"$type": "book",
"title": "The Book",
"author": "The Author"
}By adopting these practices, you ensure compatibility with AOT/trimmed environments while maintaining performance and type safety[1][7][10][13].
Citations:
- 1: https://github.com/chickensoft-games/Serialization
- 2: https://blog.ostebaronen.dk/2025/01/jsonserializercontext.html
- 3: https://infinum.com/handbook/dotnet/best-practices/json-serialization-and-deserialization
- 4: https://github.com/getsentry/sentry.engineering/blob/main/data/blog/should-you-could-you-aot.md
- 5: https://sentry.engineering/blog/should-you-could-you-aot
- 6: Eliminate Trim/AOT Warnings related to System.Text.Json dotnet/aspnetcore#45527
- 7: https://devblogs.microsoft.com/dotnet/system-text-json-in-dotnet-8/
- 8: https://okyrylchuk.dev/blog/intro-to-serialization-with-source-generation-in-system-text-json/
- 9: https://devblogs.microsoft.com/dotnet/creating-aot-compatible-libraries/
- 10: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation
- 11: https://isadorasophia.com/articles/serialization/
- 12: https://dev.to/1ce/how-to-deserialize-internally-tagged-json-in-c-with-aot-compilation-3plc
- 13: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/native-aot?view=aspnetcore-9.0
- 14: [Native-AOT] Using Json Serialize and Deserialize with Native AOT? dotnet/runtimelab#635
Action Required: Address Reflection Fallback for AOT Compatibility
The reflection-based fallback in use at src/libs/CSharpToJsonSchema/MeaiFunction.cs (lines 144–150) poses a risk in AOT environments where runtime reflection is unsupported. Given best practices for System.Text.Json in AOT scenarios—such as leveraging source generators and predefining a JsonSerializerContext—relying on reflection can lead to runtime errors. If you must retain this fallback for backward compatibility, please consider one of the following actions:
- Remove the reflection fallback entirely: Ensure all serialization scenarios use properly configured
JsonSerializerOptions(e.g., with source-generated type metadata) to eliminate the risk in AOT environments. - Gate the fallback with a feature flag: If reflection-based serialization is necessary in some contexts, add a feature flag (or conditional compilation) to disable it when the application is running in AOT scenarios.
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.
Actionable comments posted: 0
♻️ Duplicate comments (1)
src/libs/CSharpToJsonSchema/MeaiFunction.cs (1)
147-154: 💡 Verification agent🧩 Analysis chain
Consider adding feature flag for AOT environments.
The reflection fallback mechanism with explicit warnings provides good backward compatibility, but it may lead to runtime errors in AOT environments.
Consider adding a feature flag or conditional compilation to completely disable reflection in AOT-targeted builds:
if (_options == null) { + #if !ENABLE_AOT #pragma warning disable IL2026, IL3050 // Reflection is used only when enabled //Fallback to Reflection //This will break the AOT, Hoping for the best, IChatClient implementation only send JSON classes //Or Developer is using the code generator _options = InitializeReflectionOptions(); - #pragma warning disable IL2026, IL3050 // Reflection is used only when enabled + #pragma warning restore IL2026, IL3050 // Reflection is used only when enabled + #else + throw new InvalidOperationException("Reflection-based serialization is disabled in AOT environments. Please provide JsonSerializerOptions with proper TypeInfoResolver."); + #endif }Let's see if there are similar patterns elsewhere in the codebase:
🏁 Script executed:
#!/bin/bash # Check for AOT-related conditionals or feature flags in the codebase rg -A 2 -B 2 'AOT|Native AOT|NativeAOT|ENABLE_AOT|#if.*AOT|#if.*REFLECTION|JsonSerializer\.IsReflectionEnabledByDefault'Length of output: 19449
AOT Feature Flag for Reflection Fallback Required
The reflection fallback block in
src/libs/CSharpToJsonSchema/MeaiFunction.cs(lines 147–154) currently relies on runtime behavior while always attempting to use reflection. Since runtime reflection can cause failures in AOT environments, it’s safer to gate this fallback using a compile-time feature flag. Doing so will disable the reflection path outright for AOT builds and ensure a clear exception is thrown instead.Please update the code as follows:
if (_options == null) { - #pragma warning disable IL2026, IL3050 // Reflection is used only when enabled - //Fallback to Reflection - //This will break the AOT, Hoping for the best, IChatClient implementation only send JSON classes - //Or Developer is using the code generator - _options = InitializeReflectionOptions(); - #pragma warning disable IL2026, IL3050 // Reflection is used only when enabled + #if !ENABLE_AOT + #pragma warning disable IL2026, IL3050 // Reflection is used only when enabled + // Fallback to Reflection + // This approach may break AOT; consider using the code generator or providing JsonSerializerOptions + _options = InitializeReflectionOptions(); + #pragma warning restore IL2026, IL3050 // Reflection is used only when enabled + #else + throw new InvalidOperationException("Reflection-based serialization is disabled in AOT environments. Please provide JsonSerializerOptions with a proper TypeInfoResolver."); + #endif }This adjustment aligns with similar runtime checks in the repository (e.g., the
if(!JsonSerializer.IsReflectionEnabledByDefault)block) while providing a safer, compile-time safeguard against reflection in AOT contexts.
🧹 Nitpick comments (1)
src/libs/CSharpToJsonSchema/MeaiFunction.cs (1)
123-159: Improved JSON type handling with comprehensive type support.The enhanced handling of various JSON types (
JsonNode,JsonValue,JsonObject,JsonArray) provides better support for different argument formats. The fallback mechanism using reflection is properly implemented with warning suppressions.However, there are a few areas for improvement:
- Consider removing the commented-out code (lines 140-143 and 159) as it adds noise without providing value:
- // if(type.IsPrimitive) - // { - // jsonObject[args.Key] = JsonValue.Create(args.Value); - // } - // else - // { if (_options == null) { #pragma warning disable IL2026, IL3050 // Reflection is used only when enabled //Fallback to Reflection //This will break the AOT, Hoping for the best, IChatClient implementation only send JSON classes //Or Developer is using the code generator _options = InitializeReflectionOptions(); #pragma warning disable IL2026, IL3050 // Reflection is used only when enabled } var typeInfo = _options.GetTypeInfo(type); var str = JsonSerializer.Serialize(args.Value, typeInfo); jsonObject[args.Key] = JsonNode.Parse(str); - //}
- The warning directive on line 153 should be
#pragma warning restoreinstead of#pragma warning disableto properly close the warning suppression block.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/libs/CSharpToJsonSchema/MeaiFunction.cs(6 hunks)src/libs/CSharpToJsonSchema/TypeToSchemaHelpers.cs(2 hunks)
✅ Files skipped from review due to trivial changes (1)
- src/libs/CSharpToJsonSchema/TypeToSchemaHelpers.cs
🔇 Additional comments (5)
src/libs/CSharpToJsonSchema/MeaiFunction.cs (5)
4-5: Good addition of required imports.The added imports for JSON serialization and metadata support the enhanced type handling capabilities being introduced.
35-35: LGTM: Field for JSON serialization options.Adding this field allows for storing and using custom serialization options throughout the class.
47-47: Well-implemented parameter addition with backward compatibility.The optional JSON serialization options parameter preserves backward compatibility while adding flexibility. The assignment on line 61 properly stores the provided options for later use.
Also applies to: 61-61
66-82: Properly handles reflection-based serialization with explicit warnings.The method correctly initializes default serialization options with appropriate configurations and includes pragma directives to suppress AOT-related warnings. The explicit check for reflection availability on line 69 is a good safeguard.
106-106: Good enhancement to method accessibility and extensibility.Changing the method from private to protected virtual allows derived classes to access and override this functionality, which improves the extensibility of the class.
Summary by CodeRabbit