Skip to content

Commit a1c63e4

Browse files
committed
Use DataType for date types (see #465)
Apply VS code suggestions
1 parent 4ce4394 commit a1c63e4

File tree

5 files changed

+55
-35
lines changed

5 files changed

+55
-35
lines changed

Diff for: XmlSampleGenerator/XmlSampleGenerator.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
<PropertyGroup>
1717
<SonarQubeExclude>true</SonarQubeExclude>
1818
<RunCodeAnalysis>false</RunCodeAnalysis>
19+
<RunAnalyzersDuringLiveAnalysis>False</RunAnalyzersDuringLiveAnalysis>
20+
<RunAnalyzersDuringBuild>False</RunAnalyzersDuringBuild>
1921
</PropertyGroup>
2022
</Project>

Diff for: XmlSchemaClassGenerator.Tests/DateTimeTypeTests.cs

+28
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,34 @@ public void WhenDateTimeOffsetIsNotUsed_DataTypePropertyIsPresent()
103103
Assert.Contains(expectedXmlSerializationAttribute, generatedProperty);
104104
}
105105

106+
[Fact]
107+
public void WhenDateTimeOffsetIsNotUsed_DataTypePropertyIsPresent2()
108+
{
109+
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
110+
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
111+
<xs:complexType name=""document"">
112+
<xs:sequence>
113+
<xs:element name=""someDate"" type=""xs:date"" />
114+
</xs:sequence>
115+
</xs:complexType>
116+
</xs:schema>";
117+
118+
var generatedType = ConvertXml(
119+
xsd, new()
120+
{
121+
NamespaceProvider = new()
122+
{
123+
GenerateNamespace = _ => "Test"
124+
},
125+
DateTimeWithTimeZone = true
126+
});
127+
128+
var expectedXmlSerializationAttribute = "[System.Xml.Serialization.XmlElementAttribute(\"someDate\", DataType=\"date\")]";
129+
var generatedProperty = generatedType.First();
130+
131+
Assert.Contains(expectedXmlSerializationAttribute, generatedProperty);
132+
}
133+
106134
[Theory]
107135
[InlineData(false, "System.DateTime")]
108136
[InlineData(true, "System.DateTimeOffset")]

Diff for: XmlSchemaClassGenerator/CodeUtilities.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ public static string ToCamelCase(this string s) => string.IsNullOrEmpty(s) ? s
6767
public static string ToBackingField(this string propertyName, string privateFieldPrefix)
6868
=> string.Concat(privateFieldPrefix, propertyName.ToCamelCase());
6969

70-
public static bool? IsDataTypeAttributeAllowed(this XmlSchemaDatatype type) => type.TypeCode switch
70+
public static bool? IsDataTypeAttributeAllowed(this XmlSchemaDatatype type, GeneratorConfiguration configuration) => type.TypeCode switch
7171
{
7272
XmlTypeCode.AnyAtomicType => false,// union
73-
XmlTypeCode.DateTime or XmlTypeCode.Time or XmlTypeCode.Date or XmlTypeCode.Base64Binary or XmlTypeCode.HexBinary => true,
73+
XmlTypeCode.DateTime or XmlTypeCode.Time => !configuration.DateTimeWithTimeZone,
74+
XmlTypeCode.Date or XmlTypeCode.Base64Binary or XmlTypeCode.HexBinary => true,
7475
_ => false,
7576
};
7677

Diff for: XmlSchemaClassGenerator/ModelBuilder.cs

+19-29
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ internal class ModelBuilder
1616
private const string ElementName = "Element";
1717
private readonly GeneratorConfiguration _configuration;
1818
private readonly XmlSchemaSet _set;
19-
private readonly Dictionary<XmlQualifiedName, HashSet<XmlSchemaAttributeGroup>> AttributeGroups = new();
20-
private readonly Dictionary<XmlQualifiedName, HashSet<XmlSchemaGroup>> Groups = new();
21-
private readonly Dictionary<NamespaceKey, NamespaceModel> Namespaces = new();
22-
private readonly Dictionary<string, TypeModel> Types = new();
23-
private readonly Dictionary<XmlQualifiedName, HashSet<Substitute>> SubstitutionGroups = new();
19+
private readonly Dictionary<XmlQualifiedName, HashSet<XmlSchemaAttributeGroup>> AttributeGroups = [];
20+
private readonly Dictionary<XmlQualifiedName, HashSet<XmlSchemaGroup>> Groups = [];
21+
private readonly Dictionary<NamespaceKey, NamespaceModel> Namespaces = [];
22+
private readonly Dictionary<string, TypeModel> Types = [];
23+
private readonly Dictionary<XmlQualifiedName, HashSet<Substitute>> SubstitutionGroups = [];
2424

2525
private static readonly XmlQualifiedName AnyType = new("anyType", XmlSchema.Namespace);
2626

@@ -62,7 +62,7 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set)
6262
{
6363
if (!AttributeGroups.ContainsKey(currentAttributeGroup.QualifiedName))
6464
{
65-
AttributeGroups.Add(currentAttributeGroup.QualifiedName, new HashSet<XmlSchemaAttributeGroup>());
65+
AttributeGroups.Add(currentAttributeGroup.QualifiedName, []);
6666
}
6767

6868
AttributeGroups[currentAttributeGroup.QualifiedName].Add(currentAttributeGroup);
@@ -75,7 +75,7 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set)
7575
{
7676
if (!Groups.ContainsKey(currentSchemaGroup.QualifiedName))
7777
{
78-
Groups.Add(currentSchemaGroup.QualifiedName, new HashSet<XmlSchemaGroup>());
78+
Groups.Add(currentSchemaGroup.QualifiedName, []);
7979
}
8080

8181
Groups[currentSchemaGroup.QualifiedName].Add(currentSchemaGroup);
@@ -314,7 +314,7 @@ private void CreateElement(XmlSchemaElement rootElement)
314314
{
315315
if (!SubstitutionGroups.TryGetValue(rootElement.SubstitutionGroup, out var substitutes))
316316
{
317-
substitutes = new HashSet<Substitute>();
317+
substitutes = [];
318318
SubstitutionGroups.Add(rootElement.SubstitutionGroup, substitutes);
319319
}
320320

@@ -422,24 +422,14 @@ private TypeModel CreateTypeModel(XmlQualifiedName qualifiedName, XmlSchemaAnnot
422422
return typeModelBuilder.Create(type);
423423
}
424424

425-
private sealed class TypeModelBuilder
425+
private sealed class TypeModelBuilder(ModelBuilder builder, GeneratorConfiguration configuration, XmlQualifiedName qualifiedName, NamespaceModel namespaceModel, List<DocumentationModel> docs, Uri source)
426426
{
427-
private readonly ModelBuilder builder;
428-
private readonly GeneratorConfiguration _configuration;
429-
private readonly XmlQualifiedName qualifiedName;
430-
private readonly NamespaceModel namespaceModel;
431-
private readonly List<DocumentationModel> docs;
432-
private readonly Uri source;
433-
434-
public TypeModelBuilder(ModelBuilder builder, GeneratorConfiguration configuration, XmlQualifiedName qualifiedName, NamespaceModel namespaceModel, List<DocumentationModel> docs, Uri source)
435-
{
436-
this.builder = builder;
437-
_configuration = configuration;
438-
this.qualifiedName = qualifiedName;
439-
this.namespaceModel = namespaceModel;
440-
this.docs = docs;
441-
this.source = source;
442-
}
427+
private readonly ModelBuilder builder = builder;
428+
private readonly GeneratorConfiguration _configuration = configuration;
429+
private readonly XmlQualifiedName qualifiedName = qualifiedName;
430+
private readonly NamespaceModel namespaceModel = namespaceModel;
431+
private readonly List<DocumentationModel> docs = docs;
432+
private readonly Uri source = source;
443433

444434
internal TypeModel Create(XmlSchemaAnnotated type) => type switch
445435
{
@@ -661,7 +651,7 @@ XmlSchemaSimpleTypeUnion typeUnion when AllMembersHaveFacets(typeUnion, out base
661651
restrictions = CodeUtilities.GetRestrictions(facets, simpleType, _configuration).Where(r => r != null).Sanitize().ToList();
662652
}
663653

664-
return CreateSimpleModel(simpleType, restrictions ?? new());
654+
return CreateSimpleModel(simpleType, restrictions ?? []);
665655

666656
static bool AllMembersHaveFacets(XmlSchemaSimpleTypeUnion typeUnion, out List<IEnumerable<XmlSchemaFacet>> baseFacets)
667657
{
@@ -673,7 +663,7 @@ static bool AllMembersHaveFacets(XmlSchemaSimpleTypeUnion typeUnion, out List<IE
673663

674664
static List<XmlSchemaFacet> MergeRestrictions(XmlSchemaSimpleType type)
675665
{
676-
if (type == null) return new();
666+
if (type == null) return [];
677667
var baseFacets = MergeRestrictions(type.BaseXmlSchemaType as XmlSchemaSimpleType);
678668
if (type.Content is XmlSchemaSimpleTypeRestriction typeRestriction)
679669
{
@@ -908,7 +898,7 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
908898
{
909899
// ElementSchemaType must be non-null. This is not the case when maxOccurs="0".
910900
case XmlSchemaElement element when element.ElementSchemaType != null:
911-
property = PropertyFromElement(owningTypeModel, element, particle, item, substitute, passProperties ? properties : new List<PropertyModel>());
901+
property = PropertyFromElement(owningTypeModel, element, particle, item, substitute, passProperties ? properties : []);
912902
break;
913903
case XmlSchemaAny:
914904
SimpleModel typeModel = new(_configuration)
@@ -1093,7 +1083,7 @@ public IEnumerable<Particle> GetElements(XmlSchemaObject item, XmlSchemaObject p
10931083

10941084
public static List<DocumentationModel> GetDocumentation(XmlSchemaAnnotated annotated)
10951085
{
1096-
return annotated.Annotation == null ? new List<DocumentationModel>()
1086+
return annotated.Annotation == null ? []
10971087
: annotated.Annotation.Items.OfType<XmlSchemaDocumentation>()
10981088
.Where(d => d.Markup?.Length > 0)
10991089
.Select(d => d.Markup.Select(m => new DocumentationModel { Language = d.Language, Text = m.OuterXml }))

Diff for: XmlSchemaClassGenerator/TypeModel.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public override CodeTypeDeclaration Generate()
326326
docs.AddRange(simpleModel.Restrictions.Select(r => new DocumentationModel { Language = English, Text = r.Description }));
327327
text.CustomAttributes.AddRange(simpleModel.GetRestrictionAttributes().ToArray());
328328

329-
if (BaseClass.GetQualifiedName() is { Namespace: XmlSchema.Namespace, Name: var name } && (simpleModel.XmlSchemaType.Datatype.IsDataTypeAttributeAllowed() ?? simpleModel.UseDataTypeAttribute))
329+
if (BaseClass.GetQualifiedName() is { Namespace: XmlSchema.Namespace, Name: var name } && (simpleModel.XmlSchemaType.Datatype.IsDataTypeAttributeAllowed(Configuration) ?? simpleModel.UseDataTypeAttribute))
330330
attribute.Arguments.Add(new CodeAttributeArgument(nameof(XmlTextAttribute.DataType), new CodePrimitiveExpression(name)));
331331
}
332332

@@ -1104,8 +1104,7 @@ private IEnumerable<CodeAttributeDeclaration> GetAttributes(bool isArray, TypeMo
11041104
{
11051105
var qualifiedName = xmlSchemaType.GetQualifiedName();
11061106

1107-
if (qualifiedName.Namespace == XmlSchema.Namespace && qualifiedName.Name != "anySimpleType" &&
1108-
!(xmlSchemaType.Datatype.ValueType == typeof(DateTime) && Configuration.DateTimeWithTimeZone))
1107+
if (qualifiedName.Namespace == XmlSchema.Namespace && qualifiedName.Name != "anySimpleType")
11091108
{
11101109
args.Add(new("DataType", new CodePrimitiveExpression(qualifiedName.Name)));
11111110
break;
@@ -1250,7 +1249,7 @@ public override CodeTypeReference GetReferenceFor(NamespaceModel referencingName
12501249
// XmlSerializer is inconsistent: maps xs:decimal to decimal but xs:integer to string,
12511250
// even though xs:integer is a restriction of xs:decimal
12521251
type = XmlSchemaType.Datatype.GetEffectiveType(Configuration, Restrictions, XmlSchemaType, attribute);
1253-
UseDataTypeAttribute = XmlSchemaType.Datatype.IsDataTypeAttributeAllowed() ?? UseDataTypeAttribute;
1252+
UseDataTypeAttribute = XmlSchemaType.Datatype.IsDataTypeAttributeAllowed(Configuration) ?? UseDataTypeAttribute;
12541253
}
12551254

12561255
if (collection)

0 commit comments

Comments
 (0)