From c94de71cccf28595f7d2b0c67f5e6c1a87e3eb2a Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Thu, 10 Dec 2020 16:15:44 +0100 Subject: [PATCH] Improve support for group references Rename properties only if they refer to the same group --- XmlSchemaClassGenerator.Tests/XmlTests.cs | 88 +++++++++++------------ XmlSchemaClassGenerator/ModelBuilder.cs | 11 ++- 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 8ba35e45..a19a7a59 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -222,7 +222,7 @@ public void TestListWithPublicPropertySettersWithoutConstructors() Assert.Null(collectionPropertyInfo.GetValue(myClassInstance)); } } - + [Fact] public void TestListWithPublicPropertySettersWithoutConstructorsSpecified() { @@ -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}); @@ -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 @@ -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); @@ -1533,13 +1533,13 @@ public void EnumWithNonUniqueEntriesTest() public void RenameInterfacePropertyInDerivedClassTest() { const string xsd = @" - - + @@ -1548,7 +1548,7 @@ public void RenameInterfacePropertyInDerivedClassTest() - + @@ -1560,19 +1560,19 @@ public void RenameInterfacePropertyInDerivedClassTest() - + - - + + - + "; @@ -1612,44 +1612,44 @@ public void RenameInterfacePropertyInDerivedClassTest() public void DoNotGenerateSamePropertiesInDerivedInterfacesClassTest() { const string xsd = @" - - + - + - + - - + + - - - + + + - + "; @@ -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()); @@ -1692,7 +1692,7 @@ public void DoNotGenerateSamePropertiesInDerivedInterfacesClassTest() public void NillableWithDefaultValueTest() { const string xsd = @" - @@ -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); @@ -1742,11 +1742,11 @@ public void GenerateXmlRootAttributeForEnumTest() elementFormDefault=""qualified"" attributeFormDefault=""unqualified""> - + - + "; @@ -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().FirstOrDefault(); @@ -1779,11 +1779,11 @@ public void AmbiguousTypesTest() elementFormDefault=""qualified"" attributeFormDefault=""unqualified""> - + - + "; @@ -1792,11 +1792,11 @@ public void AmbiguousTypesTest() elementFormDefault=""qualified"" attributeFormDefault=""unqualified""> - + - + "; @@ -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().FirstOrDefault(); @@ -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().FirstOrDefault(); @@ -2201,14 +2201,14 @@ public void AirspaceServicesTest1() /* var testFiles = new Dictionary { - { "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" }, }; diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index 1df50ad7..76adce34 100644 --- a/XmlSchemaClassGenerator/ModelBuilder.cs +++ b/XmlSchemaClassGenerator/ModelBuilder.cs @@ -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; @@ -829,12 +832,14 @@ private IEnumerable 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) {