Skip to content

Commit

Permalink
Improve support for group references
Browse files Browse the repository at this point in the history
Rename properties only if they refer to the same group
  • Loading branch information
Michael Ganss committed Dec 10, 2020
1 parent 63976de commit c94de71
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 47 deletions.
88 changes: 44 additions & 44 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void TestListWithPublicPropertySettersWithoutConstructors()
Assert.Null(collectionPropertyInfo.GetValue(myClassInstance));
}
}

[Fact]
public void TestListWithPublicPropertySettersWithoutConstructorsSpecified()
{
Expand Down Expand Up @@ -274,7 +274,7 @@ public void TestListWithPublicPropertySettersWithoutConstructorsSpecified()

foreach (var propertyInfo in publicCollectionPropertyInfos)
{

var collection = Activator.CreateInstance(propertyInfo.PropertyType);
propertyInfo.PropertyType.InvokeMember("Add", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, collection,
new object[] {null});
Expand Down Expand Up @@ -473,7 +473,7 @@ private void DeserializeSampleXml(string pattern, Assembly assembly)
var type = FindType(assembly, rootElement.QualifiedName);
var serializer = new XmlSerializer(type);
var generator = new XmlSampleGenerator(set, rootElement.QualifiedName);

using var xw = XmlWriter.Create(sb, new XmlWriterSettings { Indent = true });

// generate sample xml
Expand Down Expand Up @@ -680,7 +680,7 @@ public void TestBpmn()
}

private void PerformBpmnTest(string name, Generator generator = null)
{
{
var assembly = Compiler.Generate(name, BpmnPattern, generator);
Assert.NotNull(assembly);

Expand Down Expand Up @@ -1533,13 +1533,13 @@ public void EnumWithNonUniqueEntriesTest()
public void RenameInterfacePropertyInDerivedClassTest()
{
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema""
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema""
elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:complexType name=""ClassItemBase"">
<xs:sequence>
<xs:group ref=""Level1""/>
</xs:sequence>
</xs:sequence>
</xs:complexType>
<xs:element name=""ClassItem"">
Expand All @@ -1548,7 +1548,7 @@ public void RenameInterfacePropertyInDerivedClassTest()
<xs:extension base=""ClassItemBase""/>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:element>
<xs:element name=""SomeType1"">
<xs:complexType>
Expand All @@ -1560,19 +1560,19 @@ public void RenameInterfacePropertyInDerivedClassTest()
<xs:choice>
<xs:group ref=""Level2""/>
</xs:choice>
</xs:group>
</xs:group>
<xs:group name=""Level2"">
<xs:choice>
<xs:group ref=""Level3""/>
</xs:choice>
</xs:group>
</xs:group>
<xs:group name=""Level3"">
<xs:choice>
<xs:element name=""ClassItemBase"" type=""xs:string""/>
</xs:choice>
</xs:group>
</xs:group>
</xs:schema>";

Expand Down Expand Up @@ -1612,44 +1612,44 @@ public void RenameInterfacePropertyInDerivedClassTest()
public void DoNotGenerateSamePropertiesInDerivedInterfacesClassTest()
{
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema""
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema""
elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:element name=""ParentClass"">
<xs:complexType>
<xs:group ref=""Level1""/>
</xs:complexType>
</xs:element>
</xs:element>
<xs:group name=""Level1"">
<xs:choice>
<xs:sequence>
<xs:element name=""InterfaceProperty"" type=""xs:string""/>
<xs:group ref=""Level2""/>
</xs:sequence>
</xs:sequence>
</xs:choice>
</xs:group>
</xs:group>
<xs:group name=""Level2"">
<xs:sequence>
<xs:element name=""InterfaceProperty"" type=""xs:string""/>
<xs:group ref=""Level3""/>
</xs:sequence>
</xs:group>
</xs:sequence>
</xs:group>
<xs:group name=""Level22"">
<xs:sequence>
<xs:element name=""InterfaceProperty"" type=""xs:string""/>
<xs:element name=""Level22OwnProperty"" type=""xs:string""/>
<xs:group ref=""Level3""/>
</xs:sequence>
</xs:group>
</xs:sequence>
</xs:group>
<xs:group name=""Level3"">
<xs:choice>
<xs:element name=""InterfaceProperty"" type=""xs:string""/>
</xs:choice>
</xs:group>
</xs:group>
</xs:schema>";

Expand All @@ -1666,13 +1666,13 @@ public void DoNotGenerateSamePropertiesInDerivedInterfacesClassTest()
var content = Assert.Single(contents);

var assembly = Compiler.Compile(nameof(DoNotGenerateSamePropertiesInDerivedInterfacesClassTest), content);

var listType = assembly.GetType("Test.ParentClass");
Assert.NotNull(listType);

var listTypePropertyInfo = listType.GetProperties().FirstOrDefault(p => p.Name == "InterfaceProperty");
Assert.NotNull(listTypePropertyInfo);

var level1Interface = assembly.GetType("Test.ILevel1");
Assert.NotNull(level1Interface);
Assert.Empty(level1Interface.GetProperties());
Expand All @@ -1692,7 +1692,7 @@ public void DoNotGenerateSamePropertiesInDerivedInterfacesClassTest()
public void NillableWithDefaultValueTest()
{
const string xsd = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema""
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema""
elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:complexType name=""TestType"">
Expand All @@ -1714,10 +1714,10 @@ public void NillableWithDefaultValueTest()
var content = Assert.Single(contents);

var assembly = Compiler.Compile(nameof(NillableWithDefaultValueTest), content);

var testType = assembly.GetType("Test.TestType");
Assert.NotNull(testType);

var propertyInfo = testType.GetProperties().FirstOrDefault(p => p.Name == "IntProperty");
Assert.NotNull(propertyInfo);
var testTypeInstance = Activator.CreateInstance(testType);
Expand All @@ -1742,11 +1742,11 @@ public void GenerateXmlRootAttributeForEnumTest()
elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:element name=""EnumTestType"">
<xs:simpleType>
<xs:simpleType>
<xs:restriction base=""xs:string"">
<xs:enumeration value=""EnumValue""/>
</xs:restriction>
</xs:simpleType>
</xs:simpleType>
</xs:element>
</xs:schema>";
Expand All @@ -1762,7 +1762,7 @@ public void GenerateXmlRootAttributeForEnumTest()
var content = Assert.Single(contents);

var assembly = Compiler.Compile(nameof(GenerateXmlRootAttributeForEnumTest), content);

var testType = assembly.GetType("Test.EnumTestType");
Assert.NotNull(testType);
var xmlRootAttribute = testType.GetCustomAttributes<XmlRootAttribute>().FirstOrDefault();
Expand All @@ -1779,11 +1779,11 @@ public void AmbiguousTypesTest()
elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:element name=""EnumTestType"">
<xs:simpleType>
<xs:simpleType>
<xs:restriction base=""xs:string"">
<xs:enumeration value=""EnumValue""/>
</xs:restriction>
</xs:simpleType>
</xs:simpleType>
</xs:element>
</xs:schema>";
Expand All @@ -1792,11 +1792,11 @@ public void AmbiguousTypesTest()
elementFormDefault=""qualified"" attributeFormDefault=""unqualified"">
<xs:element name=""EnumTestType"">
<xs:simpleType>
<xs:simpleType>
<xs:restriction base=""xs:string"">
<xs:enumeration value=""EnumValue""/>
</xs:restriction>
</xs:simpleType>
</xs:simpleType>
</xs:element>
</xs:schema>";
Expand All @@ -1813,7 +1813,7 @@ public void AmbiguousTypesTest()
var content2 = Assert.Single(contents2);

var assembly = Compiler.Compile(nameof(GenerateXmlRootAttributeForEnumTest), content1, content2);

var testType = assembly.GetType("Test_NS1.EnumTestType");
Assert.NotNull(testType);
var xmlRootAttribute = testType.GetCustomAttributes<XmlRootAttribute>().FirstOrDefault();
Expand Down Expand Up @@ -1901,7 +1901,7 @@ public void AmbiguousAnonymousTypesTest()
Assert.Equal(2, contents.Length);

var assembly = Compiler.Compile(nameof(GenerateXmlRootAttributeForEnumTest), contents);

var testType = assembly.GetType("Test_NS1.TestType");
Assert.NotNull(testType);
var xmlRootAttribute = testType.GetCustomAttributes<XmlRootAttribute>().FirstOrDefault();
Expand Down Expand Up @@ -2201,14 +2201,14 @@ public void AirspaceServicesTest1()
/*
var testFiles = new Dictionary<string, string>
{
{ "airport1.xml", "AirportHeliportType" },
{ "airportHeliportTimeSlice.xml", "AirportHeliportTimeSliceType" },
{ "airspace1.xml", "AirspaceType" },
{ "navaid1.xml", "NavaidType" },
{ "navaidTimeSlice.xml", "NavaidTimeSliceType" },
{ "navaidWithAbstractTime.xml", "NavaidWithAbstractTime" },
{ "navaid.xml", "Navaid" },
{ "routesegment1.xml", "RouteSegment" },
{ "airport1.xml", "AirportHeliportType" },
{ "airportHeliportTimeSlice.xml", "AirportHeliportTimeSliceType" },
{ "airspace1.xml", "AirspaceType" },
{ "navaid1.xml", "NavaidType" },
{ "navaidTimeSlice.xml", "NavaidTimeSliceType" },
{ "navaidWithAbstractTime.xml", "NavaidWithAbstractTime" },
{ "navaid.xml", "Navaid" },
{ "routesegment1.xml", "RouteSegment" },
{ "timePeriod.xml", "TimePeriod" },
};
Expand Down
11 changes: 8 additions & 3 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ private void RenameInterfacePropertiesIfRenamedInDerivedClasses()
{
if (implementationClassProperty.Name != implementationClassProperty.OriginalPropertyName
&& implementationClassProperty.OriginalPropertyName == interfaceProperty.Name
)
&& implementationClassProperty.XmlSchemaName == interfaceProperty.XmlSchemaName
&& implementationClassProperty.XmlParent?.Parent is XmlSchemaGroup implementationGroup
&& interfaceProperty.XmlParent?.Parent is XmlSchemaGroup interfaceGroup
&& implementationGroup.QualifiedName == interfaceGroup.QualifiedName)
{
RenameInterfacePropertyInBaseClasses(interfaceModel, implementationClass, interfaceProperty, implementationClassProperty.Name);
interfaceProperty.Name = implementationClassProperty.Name;
Expand Down Expand Up @@ -829,12 +832,14 @@ private IEnumerable<PropertyModel> CreatePropertiesForElements(Uri source, TypeM
{
if (item.XmlParticle is XmlSchemaGroupRef groupRef)
{
var group = Groups[groupRef.RefName];

if (_configuration.GenerateInterfaces)
{
CreateTypeModel(CodeUtilities.CreateUri(groupRef.SourceUri), Groups[groupRef.RefName], groupRef.RefName);
CreateTypeModel(CodeUtilities.CreateUri(groupRef.SourceUri), group, groupRef.RefName);
}

var groupItems = GetElements(groupRef.Particle);
var groupItems = GetElements(group.Particle);
var groupProperties = CreatePropertiesForElements(source, typeModel, item.XmlParticle, groupItems, order: order).ToList();
if (_configuration.EmitOrder)
{
Expand Down

0 comments on commit c94de71

Please sign in to comment.