Fix XML serialization for nullable value types#9736
Conversation
- Modified CreateXmlWriteValueStatement to use NullableStructValue() for DateTimeOffset, TimeSpan, and byte[] types - Added .Value accessor for nullable value types when calling WriteStringValue and WriteBase64StringValue - Added unit tests for nullable DateTimeOffset and TimeSpan properties - All existing tests pass (31 XML serialization tests) Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
| { | ||
| Type t when (t == typeof(DateTimeOffset) || t == typeof(TimeSpan)) && serializationFormat.ToFormatSpecifier() is string formatSpecifier | ||
| => _xmlWriterSnippet.WriteStringValue(value, formatSpecifier), | ||
| => _xmlWriterSnippet.WriteStringValue(value.NullableStructValue(valueType), formatSpecifier), |
There was a problem hiding this comment.
@copilot since this can apply to other types aside from DateTimeOffset, and this method returns the ValueExpression if the type is not nullable, lets apply this across all types when we write the value.
There was a problem hiding this comment.
Done in ed670a4. Applied NullableStructValue() to all value types by modifying CreateXmlSerializeValueExpression to call it before passing to CreateXmlSerializePrimitiveExpression. This ensures all nullable value types (int?, bool?, etc.) correctly access .Value.
| Assert.IsTrue(methodBody.Contains("Duration.Value") && methodBody.Contains("WriteStringValue"), | ||
| $"Nullable TimeSpan property should be serialized with WriteStringValue and .Value accessor. Actual:\n{methodBody}"); | ||
| } | ||
|
|
There was a problem hiding this comment.
@copilot lets also add an additional unit test to exercise the case where a datetime property is customized to be nullable via custom code.
There was a problem hiding this comment.
Added in ed670a4. Created test CanSerializeCustomizedNullableDateTimeOffset that verifies a DateTimeOffset property customized to be nullable via CodeGenMember attribute correctly uses .Value accessor in the generated XML serialization code.
…zed nullable DateTimeOffset - Modified CreateXmlSerializeValueExpression to apply NullableStructValue before calling CreateXmlSerializePrimitiveExpression - This ensures all nullable value types (not just DateTimeOffset/TimeSpan) correctly access .Value - Added test CanSerializeCustomizedNullableDateTimeOffset for customized nullable properties - All 1150 tests pass Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Generator was passing nullable value types directly to XML serialization methods instead of accessing their underlying
.Valueproperty, causing compilation errors.Changes
CreateXmlWriteValueStatementto callNullableStructValue()on values before passing toWriteStringValueandWriteBase64StringValueCreateXmlSerializeValueExpressionto callNullableStructValue()before passing toCreateXmlSerializePrimitiveExpression, ensuring comprehensive coverage for all nullable value typesDateTimeOffset?,TimeSpan?,int?,bool?,byte[]?, etc.CanSerializeCustomizedNullableDateTimeOffsetto verify nullable properties customized viaCodeGenMemberattribute are correctly serializedExample
Before:
After:
Non-nullable types unaffected -
NullableStructValue()returns the value unchanged when not a nullable value type.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.