Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove beta label
  • Loading branch information
Michael Ganss committed Jul 25, 2023
1 parent 7e5b04a commit 0bcf3c6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
35 changes: 35 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private static IEnumerable<string> ConvertXml(string name, string xsd, Generator
const string EppPattern = "xsd/epp/*.xsd";
const string GraphMLPattern = "xsd/graphml/ygraphml.xsd";
const string UnionPattern = "xsd/union/union.xsd";
const string GuidPattern = "xsd/guid/*.xsd";
const string NullableReferenceAttributesPattern = "xsd/nullablereferenceattributes/nullablereference.xsd";

// IATA test takes too long to perform every time
Expand Down Expand Up @@ -156,6 +157,40 @@ public void TestClient()
SharedTestFunctions.TestSamples(Output, "Client", ClientPattern);
}

[Fact, TestPriority(1)]
[UseCulture("en-US")]
public void TestGuid()
{
var assembly = Compiler.Generate("Guid", GuidPattern);
var testType = assembly.GetType("Guid.Test");
var idProperty = testType.GetProperty("Id");
var elementIdProperty = testType.GetProperty("ElementId");

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

var serializer = new XmlSerializer(testType);

var test = Activator.CreateInstance(testType);
var idGuid = Guid.NewGuid();
var elementGuid = Guid.NewGuid();

idProperty.SetValue(test, idGuid);
elementIdProperty.SetValue(test, elementGuid);

var sw = new StringWriter();

serializer.Serialize(sw, test);

var xml = sw.ToString();
var sr = new StringReader(xml);

var o = serializer.Deserialize(sr);

Assert.Equal(idGuid, idProperty.GetValue(o));
Assert.Equal(elementGuid, elementIdProperty.GetValue(o));
}

[Fact, TestPriority(1)]
[UseCulture("en-US")]
public void TestUnion()
Expand Down
14 changes: 14 additions & 0 deletions XmlSchemaClassGenerator.Tests/xsd/guid/guid.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://microsoft.com/wsdl/types/">
<xs:simpleType name="guid">
<xs:annotation>
<xs:documentation xml:lang="en">
The representation of a GUID, generally the id of an element.
</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:pattern value="[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
18 changes: 18 additions & 0 deletions XmlSchemaClassGenerator.Tests/xsd/guid/guidtest.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:s="http://microsoft.com/wsdl/types/"
>
<xs:import namespace="http://microsoft.com/wsdl/types/" schemaLocation="guid.xsd"></xs:import>
<xs:element name="Test">
<xs:complexType>
<xs:sequence>
<xs:element name="ElementId" type="s:guid"></xs:element>
</xs:sequence>
<xs:attribute name="Id" type="s:guid"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
7 changes: 7 additions & 0 deletions XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ private static Type GetIntegerDerivedType(XmlSchemaDatatype xml, GeneratorConfig
Type FromFallback() => configuration.UseIntegerDataTypeAsFallback && configuration.IntegerDataType != null ? configuration.IntegerDataType : typeof(string);
}

private static readonly XmlQualifiedName GuidQualifiedName = new("guid", "http://microsoft.com/wsdl/types/");

public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfiguration configuration, IEnumerable<RestrictionModel> restrictions, XmlSchemaType schemaType, bool attribute = false)
{
var resultType = type.TypeCode switch
Expand All @@ -112,6 +114,11 @@ public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfig
_ => type.ValueType,
};

if (schemaType.QualifiedName == GuidQualifiedName)
{
resultType = typeof(Guid);
}

if (type.Variety == XmlSchemaDatatypeVariety.List)
{
if (resultType.IsArray)
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2.0.{build}-beta
version: 2.0.{build}
skip_tags: true
image: Visual Studio 2022
environment:
Expand Down

0 comments on commit 0bcf3c6

Please sign in to comment.