Skip to content

Commit 8c58cf2

Browse files
author
Michael Ganss
committed
Generate Guid for types derived from http://microsoft.com/wsdl/types/:guid (see #402)
1 parent 0bcf3c6 commit 8c58cf2

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,12 @@ public void TestGuid()
165165
var testType = assembly.GetType("Guid.Test");
166166
var idProperty = testType.GetProperty("Id");
167167
var elementIdProperty = testType.GetProperty("ElementId");
168+
var headerType = assembly.GetType("Guid.V1.Header");
169+
var referenceProperty = headerType.GetProperty("Reference");
168170

169171
Assert.Equal(typeof(Nullable<>).MakeGenericType(typeof(Guid)), idProperty.PropertyType);
170172
Assert.Equal(typeof(Guid), elementIdProperty.PropertyType);
173+
Assert.Equal(typeof(Guid), referenceProperty.PropertyType);
171174

172175
var serializer = new XmlSerializer(testType);
173176

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3+
id="BerichtHeaderSHV"
4+
targetNamespace="http://tempuri.org/v1"
5+
elementFormDefault="qualified"
6+
xmlns="http://tempuri.org/v1"
7+
version="1.0">
8+
9+
<xs:include schemaLocation="uuid.xsd"/>
10+
11+
<xs:complexType name="Header">
12+
<xs:sequence>
13+
<xs:element name="Reference" type="UUID"/>
14+
<xs:element name="Receiver" type="UUID" minOccurs="0" nillable="true"/>
15+
<xs:element name="CrossReference" type="UUID" minOccurs="0" nillable="true"/>
16+
<xs:element name="ConversationReference" type="UUID" minOccurs="0" nillable="true"/>
17+
<xs:element name="Version" type="xs:string"/>
18+
</xs:sequence>
19+
</xs:complexType>
20+
21+
<xs:element name="Header" type="Header"/>
22+
</xs:schema>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xs:schema xmlns:mxs="http://microsoft.com/wsdl/types/"
3+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
4+
id="UUID"
5+
targetNamespace="http://tempuri.org/v1"
6+
xmlns:t="http://tempuri.org/v1"
7+
elementFormDefault="qualified"
8+
version="1.0">
9+
10+
<xs:import schemaLocation="guid.xsd" namespace="http://microsoft.com/wsdl/types/"/>
11+
<xs:simpleType name="IntermediateUUID">
12+
<xs:restriction base="mxs:guid"/>
13+
</xs:simpleType>
14+
<xs:simpleType name="UUID">
15+
<xs:restriction base="t:IntermediateUUID" />
16+
</xs:simpleType>
17+
</xs:schema>

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfig
114114
_ => type.ValueType,
115115
};
116116

117-
if (schemaType.QualifiedName == GuidQualifiedName)
117+
if (schemaType.IsDerivedFrom(GuidQualifiedName))
118118
{
119119
resultType = typeof(Guid);
120120
}

XmlSchemaClassGenerator/Extensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Xml;
45
using System.Xml.Schema;
56

67
namespace XmlSchemaClassGenerator
@@ -28,5 +29,16 @@ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TS
2829
}
2930

3031
public static string QuoteIfNeeded(this string text) => !string.IsNullOrEmpty(text) && text.Contains(" ") ? "\"" + text + "\"" : text;
32+
33+
public static bool IsDerivedFrom(this XmlSchemaType type, XmlQualifiedName qualifiedName)
34+
{
35+
while (type != null)
36+
{
37+
if (type.QualifiedName == qualifiedName) return true;
38+
type = type.BaseXmlSchemaType;
39+
}
40+
41+
return false;
42+
}
3143
}
3244
}

0 commit comments

Comments
 (0)