fix: handle nullable numeric types in gRPC request mapping
When a C# property is nullable (int?, long?, etc.), protobuf3 defaults to 0 when the field is not set. The generator now treats 0 as null for nullable numeric properties, allowing proper optional field semantics. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
179b06374d
commit
46e739eead
@ -2009,10 +2009,40 @@ namespace Svrnty.CQRS.Grpc.Generators
|
||||
return $"({prop.Type})(int){accessor}";
|
||||
}
|
||||
|
||||
// Handle nullable numeric types (int?, long?, etc.)
|
||||
// In protobuf3, numeric fields default to 0 when not set.
|
||||
// For nullable C# types, we treat 0 as null (unset).
|
||||
if (prop.IsNullable && IsNullableNumericType(prop.Type))
|
||||
{
|
||||
return $"{accessor} == 0 ? null : {accessor}";
|
||||
}
|
||||
|
||||
// Default: direct assignment (works for compatible types like int, long, string, bool)
|
||||
return accessor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the type is a nullable numeric type (int?, long?, short?, etc.)
|
||||
/// </summary>
|
||||
private static bool IsNullableNumericType(string typeName)
|
||||
{
|
||||
// Check for common nullable numeric types
|
||||
var nullableNumericTypes = new[]
|
||||
{
|
||||
"int?", "System.Int32?", "global::System.Int32?",
|
||||
"long?", "System.Int64?", "global::System.Int64?",
|
||||
"short?", "System.Int16?", "global::System.Int16?",
|
||||
"byte?", "System.Byte?", "global::System.Byte?",
|
||||
"uint?", "System.UInt32?", "global::System.UInt32?",
|
||||
"ulong?", "System.UInt64?", "global::System.UInt64?",
|
||||
"ushort?", "System.UInt16?", "global::System.UInt16?",
|
||||
"sbyte?", "System.SByte?", "global::System.SByte?",
|
||||
"float?", "System.Single?", "global::System.Single?",
|
||||
"double?", "System.Double?", "global::System.Double?",
|
||||
};
|
||||
return nullableNumericTypes.Contains(typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a type is a collection type and returns the element type if so
|
||||
/// </summary>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user