Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit ef3a704

Browse files
authored
Support default value for time span (#18761)
* Support the defaultvalue in timespace. Use duration as the qualified name for timespan. * Update based on the code review feedback. * Add more default value scenario. * Update verification
1 parent 1618e8c commit ef3a704

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

src/System.Private.Xml/src/System/Xml/Serialization/CodeGenerator.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,22 @@ internal void Ldc(object o)
845845
case TypeCode.Empty:
846846
case TypeCode.DBNull:
847847
default:
848-
Debug.Assert(false, "UnknownConstantType");
849-
throw new NotSupportedException(SR.Format(SR.UnknownConstantType, valueType.AssemblyQualifiedName));
848+
if (valueType == typeof(TimeSpan))
849+
{
850+
ConstructorInfo TimeSpan_ctor = typeof(TimeSpan).GetConstructor(
851+
CodeGenerator.InstanceBindingFlags,
852+
null,
853+
new Type[] { typeof(Int64) },
854+
null
855+
);
856+
Ldc(((TimeSpan)o).Ticks); // ticks
857+
New(TimeSpan_ctor);
858+
break;
859+
}
860+
else
861+
{
862+
throw new NotSupportedException(SR.Format(SR.UnknownConstantType, valueType.AssemblyQualifiedName));
863+
}
850864
}
851865
}
852866
}

src/System.Private.Xml/src/System/Xml/Serialization/Types.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ static TypeScope()
538538

539539
AddNonXsdPrimitive(typeof(Guid), "guid", UrtTypes.Namespace, "Guid", new XmlQualifiedName("string", XmlSchema.Namespace), new XmlSchemaFacet[] { guidPattern }, TypeFlags.CanBeAttributeValue | TypeFlags.CanBeElementValue | TypeFlags.XmlEncodingNotRequired | TypeFlags.IgnoreDefault);
540540
AddNonXsdPrimitive(typeof(char), "char", UrtTypes.Namespace, "Char", new XmlQualifiedName("unsignedShort", XmlSchema.Namespace), new XmlSchemaFacet[0], TypeFlags.CanBeAttributeValue | TypeFlags.CanBeElementValue | TypeFlags.HasCustomFormatter | TypeFlags.IgnoreDefault);
541-
AddNonXsdPrimitive(typeof(TimeSpan), "TimeSpan", UrtTypes.Namespace, "TimeSpan", new XmlQualifiedName("string", XmlSchema.Namespace), new XmlSchemaFacet[0], TypeFlags.CanBeAttributeValue | TypeFlags.CanBeElementValue | TypeFlags.XmlEncodingNotRequired | TypeFlags.IgnoreDefault);
541+
AddNonXsdPrimitive(typeof(TimeSpan), "TimeSpan", UrtTypes.Namespace, "TimeSpan", new XmlQualifiedName("duration", XmlSchema.Namespace), new XmlSchemaFacet[0], TypeFlags.CanBeAttributeValue | TypeFlags.CanBeElementValue | TypeFlags.XmlEncodingNotRequired);
542542

543543
AddSoapEncodedTypes(Soap.Encoding);
544544

src/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,18 @@ public static void Xml_TypeWithTimeSpanProperty()
16581658
Assert.StrictEqual(obj.TimeSpanProperty, deserializedObj.TimeSpanProperty);
16591659
}
16601660

1661+
[Fact]
1662+
public static void Xml_TypeWithDefaultTimeSpanProperty()
1663+
{
1664+
var obj = new TypeWithDefaultTimeSpanProperty { TimeSpanProperty2 = new TimeSpan(0, 1, 0) };
1665+
var deserializedObj = SerializeAndDeserialize(obj,
1666+
@"<?xml version=""1.0""?>
1667+
<TypeWithDefaultTimeSpanProperty xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><TimeSpanProperty2>PT1M</TimeSpanProperty2></TypeWithDefaultTimeSpanProperty>");
1668+
Assert.NotNull(deserializedObj);
1669+
Assert.Equal(obj.TimeSpanProperty, deserializedObj.TimeSpanProperty);
1670+
Assert.Equal(obj.TimeSpanProperty2, deserializedObj.TimeSpanProperty2);
1671+
}
1672+
16611673
[Fact]
16621674
public static void Xml_TypeWithByteProperty()
16631675
{

src/System.Runtime.Serialization.Xml/tests/SerializationTypes.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Xml.Serialization;
1616
using System.IO;
1717
using System.Text;
18+
using System.Reflection;
1819

1920
namespace SerializationTypes
2021
{
@@ -3157,6 +3158,38 @@ public class TypeWithTimeSpanProperty
31573158
public TimeSpan TimeSpanProperty;
31583159
}
31593160

3161+
public class TypeWithDefaultTimeSpanProperty
3162+
{
3163+
public TypeWithDefaultTimeSpanProperty()
3164+
{
3165+
TimeSpanProperty = GetDefaultValue("TimeSpanProperty");
3166+
TimeSpanProperty2 = GetDefaultValue("TimeSpanProperty2");
3167+
}
3168+
3169+
[DefaultValue(typeof(TimeSpan), "00:01:00")]
3170+
public TimeSpan TimeSpanProperty { get; set; }
3171+
3172+
[DefaultValue(typeof(TimeSpan), "00:00:01")]
3173+
public TimeSpan TimeSpanProperty2 { get; set; }
3174+
3175+
public TimeSpan GetDefaultValue(string propertyName)
3176+
{
3177+
var property = this.GetType().GetProperty(propertyName);
3178+
3179+
var attribute = property.GetCustomAttribute(typeof(DefaultValueAttribute))
3180+
as DefaultValueAttribute;
3181+
3182+
if (attribute != null)
3183+
{
3184+
return (TimeSpan)attribute.Value;
3185+
}
3186+
else
3187+
{
3188+
return new TimeSpan(0, 0, 0);
3189+
}
3190+
}
3191+
}
3192+
31603193
public class TypeWithByteProperty
31613194
{
31623195
public byte ByteProperty;

0 commit comments

Comments
 (0)