Skip to content

Commit f0c2cdd

Browse files
committed
Review fixes
1 parent a98f55d commit f0c2cdd

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ Options:
9191
ignored.
9292
-o, --output=FOLDER the FOLDER to write the resulting .cs files to
9393
-d, --datetime-offset map xs:datetime and derived types to System.
94-
DateTimeOffset instead of System.DateTime
94+
DateTimeOffset instead of System.DateTime
95+
--do, --dateOnly map xs:date and xs:time and derived types to
96+
System.DateOnly and System.TimeOnly instead of
97+
System.DateTime
9598
-i, --integer=TYPE map xs:integer and derived types to TYPE instead
9699
of automatic approximation
97100
TYPE can be i[nt], l[ong], or d[ecimal]

XmlSchemaClassGenerator.Tests/DateOnlyTimeOnlyTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,35 @@ public void WhenUseDateOnlyIsFalse_AndDateTimeWithTimeZoneIsTrue_DateTimeOffsetI
190190
Assert.Contains("public System.DateTimeOffset SomeDate", code);
191191
Assert.DoesNotContain("DataType=\"date\"", code);
192192
}
193+
[Fact]
194+
public void WhenUseDateOnlyIsTrue_AndDateTimeWithTimeZoneIsTrue_DateOnlyAndTimeOnlyAreGenerated_WithDataTypeAttribute()
195+
{
196+
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
197+
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
198+
<xs:complexType name=""document"">
199+
<xs:sequence>
200+
<xs:element name=""someDate"" type=""xs:date"" />
201+
<xs:element name=""someTime"" type=""xs:time"" />
202+
</xs:sequence>
203+
</xs:complexType>
204+
</xs:schema>";
205+
206+
var generatedType = ConvertXml(
207+
xsd, new()
208+
{
209+
NamespaceProvider = new()
210+
{
211+
GenerateNamespace = _ => "Test"
212+
},
213+
UseDateOnly = true,
214+
DateTimeWithTimeZone = true
215+
});
216+
217+
var code = string.Join(Environment.NewLine, generatedType);
218+
219+
Assert.Contains("public System.DateOnly SomeDate", code);
220+
Assert.Contains("public System.TimeOnly SomeTime", code);
221+
Assert.Contains("DataType=\"date\"", code);
222+
Assert.Contains("DataType=\"time\"", code);
223+
}
193224
}

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public static string ToBackingField(this string propertyName, string privateFiel
7878
public static bool? IsDataTypeAttributeAllowed(this XmlSchemaDatatype type, GeneratorConfiguration configuration) => type.TypeCode switch
7979
{
8080
XmlTypeCode.AnyAtomicType => false,// union
81+
XmlTypeCode.Date or XmlTypeCode.Time when configuration.UseDateOnly => true,
8182
XmlTypeCode.DateTime or XmlTypeCode.Date or XmlTypeCode.Time => !configuration.DateTimeWithTimeZone,
8283
XmlTypeCode.Base64Binary or XmlTypeCode.HexBinary => true,
8384
_ => false,

0 commit comments

Comments
 (0)