From 1e7f22a211c922b714d4c530666dc3c0d7635b3c Mon Sep 17 00:00:00 2001 From: Frederik Gheysels Date: Wed, 13 Dec 2023 21:53:39 +0100 Subject: [PATCH] Do not generate DataType property in Xml serialization property --- .../DateTimeTypeTests.cs | 59 ++++++++++++++++++- XmlSchemaClassGenerator/TypeModel.cs | 4 +- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/XmlSchemaClassGenerator.Tests/DateTimeTypeTests.cs b/XmlSchemaClassGenerator.Tests/DateTimeTypeTests.cs index e51ed7b..5cd62ce 100644 --- a/XmlSchemaClassGenerator.Tests/DateTimeTypeTests.cs +++ b/XmlSchemaClassGenerator.Tests/DateTimeTypeTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using System.Xml.Schema; @@ -46,10 +47,66 @@ private static IEnumerable ConvertXml(string xsd, Generator generatorPro return writer.Content; } + [Fact] + public void WhenDateTimeOffsetIsUsed_NoDataTypePropertyIsPresent() + { + var xsd = @$" + + + + + + +"; + + var generatedType = ConvertXml( + xsd, new() + { + NamespaceProvider = new() + { + GenerateNamespace = _ => "Test" + }, + DateTimeWithTimeZone = true + }); + + var expectedXmlSerializationAttribute = "[System.Xml.Serialization.XmlElementAttribute(\"someDate\")]"; + var generatedProperty = generatedType.First(); + + Assert.Contains(expectedXmlSerializationAttribute, generatedProperty); + } + + [Fact] + public void WhenDateTimeOffsetIsNotUsed_DataTypePropertyIsPresent() + { + var xsd = @$" + + + + + + +"; + + var generatedType = ConvertXml( + xsd, new() + { + NamespaceProvider = new() + { + GenerateNamespace = _ => "Test" + }, + DateTimeWithTimeZone = false + }); + + var expectedXmlSerializationAttribute = "[System.Xml.Serialization.XmlElementAttribute(\"someDate\", DataType=\"dateTime\")]"; + var generatedProperty = generatedType.First(); + + Assert.Contains(expectedXmlSerializationAttribute, generatedProperty); + } + [Theory] [InlineData(false, "System.DateTime")] [InlineData(true, "System.DateTimeOffset")] - public void TestFallbackType(bool dateTimeWithTimeZone, string expectedType) + public void TestCorrectDateTimeDataType(bool dateTimeWithTimeZone, string expectedType) { var xsd = @$" diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index 2e954fc..5dcfacc 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -1092,7 +1092,9 @@ private IEnumerable GetAttributes(bool isArray, TypeMo while (xmlSchemaType != null) { var qualifiedName = xmlSchemaType.GetQualifiedName(); - if (qualifiedName.Namespace == XmlSchema.Namespace && qualifiedName.Name != "anySimpleType") + + if ((qualifiedName.Namespace == XmlSchema.Namespace && qualifiedName.Name != "anySimpleType") && + (xmlSchemaType.Datatype.ValueType == typeof(DateTime) && configuration.DateTimeWithTimeZone) == false) { args.Add(new("DataType", new CodePrimitiveExpression(qualifiedName.Name))); break;