Describe the bug
In TypeSpec, it is possible to define a property of type duration with different encodings as described here: https://typespec.io/docs/libraries/http/encoding/#duration
Example definition:
@encode(DurationKnownEncoding.milliseconds, integer)
audio_end_ms: duration;
This indicates that the audio_end_ms property is an integer (milliseconds). However, the generated C# code represents this as a TimeSpan and serializes it as follows:
if (!Patch.Contains("$.audio_end_ms"u8))
{
writer.WritePropertyName("audio_end_ms"u8);
writer.WriteNumberValue(AudioEndMs.TotalMilliseconds);
}
The issue: TimeSpan.TotalMilliseconds is a double, so this code can serialize values with fractional milliseconds (e.g., 123.45), violating the contract where audio_end_ms must be an integer. This can result in serialization output not matching the expected API contract.
Expected: The code should round TotalMilliseconds up or down (or something like AudioEndMs.Ticks / TimeSpan.TicksPerMillisecond) to ensure only integers are written for the property.
Please consider updating the serialization logic to guarantee integer values when the original TypeSpec contract expects an integer.
Reproduction
Define a property in TypeSpec using:
@encode(DurationKnownEncoding.milliseconds, integer)
audio_end_ms: duration;
Generate the C# client and observe the serialization code for this member: AudioEndMs will use TotalMilliseconds resulting in double output values. Test with a TimeSpan representing fractional milliseconds and see if output is a floating point value. The value should be serialized as an integer.
Checklist
Describe the bug
In TypeSpec, it is possible to define a property of type
durationwith different encodings as described here: https://typespec.io/docs/libraries/http/encoding/#durationExample definition:
This indicates that the
audio_end_msproperty is an integer (milliseconds). However, the generated C# code represents this as aTimeSpanand serializes it as follows:The issue:
TimeSpan.TotalMillisecondsis adouble, so this code can serialize values with fractional milliseconds (e.g.,123.45), violating the contract whereaudio_end_msmust be an integer. This can result in serialization output not matching the expected API contract.Expected: The code should round
TotalMillisecondsup or down (or something likeAudioEndMs.Ticks / TimeSpan.TicksPerMillisecond) to ensure only integers are written for the property.Please consider updating the serialization logic to guarantee integer values when the original TypeSpec contract expects an integer.
Reproduction
Define a property in TypeSpec using:
Generate the C# client and observe the serialization code for this member:
AudioEndMswill useTotalMillisecondsresulting indoubleoutput values. Test with aTimeSpanrepresenting fractional milliseconds and see if output is a floating point value. The value should be serialized as an integer.Checklist