Skip to content

Commit

Permalink
Generate Guid for types derived from http://microsoft.com/wsdl/types/…
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Jul 26, 2023
1 parent 0bcf3c6 commit 8c58cf2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ public void TestGuid()
var testType = assembly.GetType("Guid.Test");
var idProperty = testType.GetProperty("Id");
var elementIdProperty = testType.GetProperty("ElementId");
var headerType = assembly.GetType("Guid.V1.Header");
var referenceProperty = headerType.GetProperty("Reference");

Assert.Equal(typeof(Nullable<>).MakeGenericType(typeof(Guid)), idProperty.PropertyType);
Assert.Equal(typeof(Guid), elementIdProperty.PropertyType);
Assert.Equal(typeof(Guid), referenceProperty.PropertyType);

var serializer = new XmlSerializer(testType);

Expand Down
22 changes: 22 additions & 0 deletions XmlSchemaClassGenerator.Tests/xsd/guid/header.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
id="BerichtHeaderSHV"
targetNamespace="http://tempuri.org/v1"
elementFormDefault="qualified"
xmlns="http://tempuri.org/v1"
version="1.0">

<xs:include schemaLocation="uuid.xsd"/>

<xs:complexType name="Header">
<xs:sequence>
<xs:element name="Reference" type="UUID"/>
<xs:element name="Receiver" type="UUID" minOccurs="0" nillable="true"/>
<xs:element name="CrossReference" type="UUID" minOccurs="0" nillable="true"/>
<xs:element name="ConversationReference" type="UUID" minOccurs="0" nillable="true"/>
<xs:element name="Version" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:element name="Header" type="Header"/>
</xs:schema>
17 changes: 17 additions & 0 deletions XmlSchemaClassGenerator.Tests/xsd/guid/uuid.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:mxs="http://microsoft.com/wsdl/types/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
id="UUID"
targetNamespace="http://tempuri.org/v1"
xmlns:t="http://tempuri.org/v1"
elementFormDefault="qualified"
version="1.0">

<xs:import schemaLocation="guid.xsd" namespace="http://microsoft.com/wsdl/types/"/>
<xs:simpleType name="IntermediateUUID">
<xs:restriction base="mxs:guid"/>
</xs:simpleType>
<xs:simpleType name="UUID">
<xs:restriction base="t:IntermediateUUID" />
</xs:simpleType>
</xs:schema>
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfig
_ => type.ValueType,
};

if (schemaType.QualifiedName == GuidQualifiedName)
if (schemaType.IsDerivedFrom(GuidQualifiedName))
{
resultType = typeof(Guid);
}
Expand Down
12 changes: 12 additions & 0 deletions XmlSchemaClassGenerator/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Schema;

namespace XmlSchemaClassGenerator
Expand Down Expand Up @@ -28,5 +29,16 @@ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TS
}

public static string QuoteIfNeeded(this string text) => !string.IsNullOrEmpty(text) && text.Contains(" ") ? "\"" + text + "\"" : text;

public static bool IsDerivedFrom(this XmlSchemaType type, XmlQualifiedName qualifiedName)
{
while (type != null)
{
if (type.QualifiedName == qualifiedName) return true;
type = type.BaseXmlSchemaType;
}

return false;
}
}
}

0 comments on commit 8c58cf2

Please sign in to comment.