In C# codegen, represent optional RPC params as optional C# method params, not just nullable values#733
Conversation
…rams, not just nullable values
There was a problem hiding this comment.
Pull request overview
This PR updates the C# RPC code generator so that non-required JSON-RPC parameters are emitted as optional C# method parameters (with default values), avoiding source-breaking changes when new optional RPC params are introduced.
Changes:
- Sort generated method parameters so required params come before optional params (C# requirement when defaults are present).
- Emit optional RPC params as
paramType paramName = nullin generated C# method signatures. - Regenerate affected RPC method signatures in
dotnet/src/Generated/Rpc.csto reflect the new optional-parameter behavior.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| scripts/codegen/csharp.ts | Updates C# RPC method signature generation to sort required/optional params and assign = null defaults for optional params. |
| dotnet/src/Generated/Rpc.cs | Updates generated RPC APIs to make optional params truly optional at the call site (e.g., prompt = null). |
SDK Consistency Review: ✅ No Cross-Language Issues FoundI've reviewed PR #733 for cross-language SDK consistency. This PR modifies the C# code generator to represent optional RPC parameters as optional C# method parameters (with Summary of ChangesThe PR updates
These changes affect the generated
Cross-SDK Consistency AnalysisEach SDK handles optional RPC parameters differently based on language idioms:
✅ Consistency VerdictThis change is appropriate and maintains cross-SDK consistency. While each language uses different mechanisms:
Minor Note: Server RPC MethodsThe same sorting logic added to However, I verified this is fine: the existing server RPC methods in the schema don't have mixed required/optional parameters that would violate C#'s ordering requirement. The sorting was only needed for session methods like Recommendation: ✅ Approve - This PR correctly improves C# SDK consistency with how optional parameters work across all SDK implementations.
|
Otherwise adding a new optional RPC param will be a source-breaking change in all usages.
Note that even with this improvement, adding a new optional RPC param will still be a binary-breaking change for C#. At the moment I think that's not an issue based on our usage model, but if this changes, things will get a lot more complex as codegen would have to keep track of historical overloads we need to preserve.
Also note that even with this improvement, adding a new optional RPC param will be source-breaking if people are already passing positional args as optional params.
cc @stephentoub in case you have any thoughts on how to handle this better. What comes to mind for me is hand-authoring partial class methods that act as back-compat overloads. It does complicate the codegen approach though.
The other main thing we could do is not have positional method parameters at all, and instead represent everything as options objects like we do in other languages. That feels slightly cumbersome for callers in C# but is more faithful to the RPC contract. Obviously it would also be a one-time breaking change too.