Skip to content

Commit 16c11fe

Browse files
committed
Code coverage for DateTimeWithTimeZone configuration
1 parent eb08849 commit 16c11fe

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

XmlSchemaClassGenerator.Tests/DateOnlyTimeOnlyTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,60 @@ public void WhenDefaultValueIsPresent_CodeIsGenerated()
134134
Assert.Contains("System.DateOnly.Parse(\"2023-10-27\")", code);
135135
Assert.Contains("System.TimeOnly.Parse(\"12:34:56\")", code);
136136
}
137+
[Fact]
138+
public void WhenUseDateOnlyIsFalse_AndDateTimeWithTimeZoneIsTrue_DateTimeOffsetIsGeneratedForTime()
139+
{
140+
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
141+
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
142+
<xs:complexType name=""document"">
143+
<xs:sequence>
144+
<xs:element name=""someTime"" type=""xs:time"" />
145+
</xs:sequence>
146+
</xs:complexType>
147+
</xs:schema>";
148+
149+
var generatedType = ConvertXml(
150+
xsd, new()
151+
{
152+
NamespaceProvider = new()
153+
{
154+
GenerateNamespace = _ => "Test"
155+
},
156+
UseDateOnly = false,
157+
DateTimeWithTimeZone = true
158+
});
159+
160+
var code = string.Join(Environment.NewLine, generatedType);
161+
162+
Assert.Contains("public System.DateTimeOffset SomeTime", code);
163+
Assert.Contains("DataType=\"time\"", code);
164+
}
165+
[Fact]
166+
public void WhenUseDateOnlyIsFalse_AndDateTimeWithTimeZoneIsTrue_DateTimeOffsetIsGeneratedForDate()
167+
{
168+
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
169+
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
170+
<xs:complexType name=""document"">
171+
<xs:sequence>
172+
<xs:element name=""someDate"" type=""xs:date"" />
173+
</xs:sequence>
174+
</xs:complexType>
175+
</xs:schema>";
176+
177+
var generatedType = ConvertXml(
178+
xsd, new()
179+
{
180+
NamespaceProvider = new()
181+
{
182+
GenerateNamespace = _ => "Test"
183+
},
184+
UseDateOnly = false,
185+
DateTimeWithTimeZone = true
186+
});
187+
188+
var code = string.Join(Environment.NewLine, generatedType);
189+
190+
Assert.Contains("public System.DateTimeOffset SomeDate", code);
191+
Assert.Contains("DataType=\"date\"", code);
192+
}
137193
}

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfig
170170
XmlTypeCode.AnyUri or XmlTypeCode.GDay or XmlTypeCode.GMonth or XmlTypeCode.GMonthDay or XmlTypeCode.GYear or XmlTypeCode.GYearMonth => typeof(string),
171171
XmlTypeCode.Duration => configuration.NetCoreSpecificCode ? type.ValueType : typeof(string),
172172
XmlTypeCode.Time => configuration.UseDateOnly ? typeof(TimeOnly) : (configuration.DateTimeWithTimeZone ? typeof(DateTimeOffset) : typeof(DateTime)),
173-
XmlTypeCode.Date => configuration.UseDateOnly ? typeof(DateOnly) : typeof(DateTime),
173+
XmlTypeCode.Date => configuration.UseDateOnly ? typeof(DateOnly) : (configuration.DateTimeWithTimeZone ? typeof(DateTimeOffset) : typeof(DateTime)),
174174
XmlTypeCode.DateTime => configuration.DateTimeWithTimeZone ? typeof(DateTimeOffset) : typeof(DateTime),
175175
XmlTypeCode.Idref => typeof(string),
176176
XmlTypeCode.Integer or XmlTypeCode.NegativeInteger or XmlTypeCode.NonNegativeInteger or XmlTypeCode.NonPositiveInteger or XmlTypeCode.PositiveInteger => GetIntegerDerivedType(type, configuration, restrictions),

0 commit comments

Comments
 (0)