From bf267dca522ff79bd75ae6dd3a7bc0aaa5ad1d77 Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Wed, 13 Feb 2019 12:16:16 +0100 Subject: [PATCH] Fix bugs creating Uris when uri string is null or empty --- XmlSchemaClassGenerator.Tests/XmlTests.cs | 2 +- XmlSchemaClassGenerator/CodeUtilities.cs | 2 ++ XmlSchemaClassGenerator/ModelBuilder.cs | 16 ++++++++-------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/XmlSchemaClassGenerator.Tests/XmlTests.cs b/XmlSchemaClassGenerator.Tests/XmlTests.cs index 46e58f20..6ddd507e 100644 --- a/XmlSchemaClassGenerator.Tests/XmlTests.cs +++ b/XmlSchemaClassGenerator.Tests/XmlTests.cs @@ -736,7 +736,7 @@ public void AssemblyVisibleIsInternalInterface() var contents = ConvertXml(nameof(ComplexTypeWithAttributeGroupExtension), xsd, generator); var content = Assert.Single(contents); - Assert.Contains("internal interface INamedElement", content); + Assert.Contains("internal partial interface INamedElement", content); } private static void CompareOutput(string expected, string actual) diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index fc7d515b..8b4886c8 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -295,5 +295,7 @@ internal static string NormalizeNewlines(string text) "using", "using static", "virtual", "void", "volatile", "while" }; + + internal static Uri CreateUri(string uri) => string.IsNullOrEmpty(uri) ? null : new Uri(uri); } } \ No newline at end of file diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index 81347c5e..dc58ac3a 100644 --- a/XmlSchemaClassGenerator/ModelBuilder.cs +++ b/XmlSchemaClassGenerator/ModelBuilder.cs @@ -47,14 +47,14 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set) foreach (var globalType in set.GlobalTypes.Values.Cast()) { var schema = globalType.GetSchema(); - var source = string.IsNullOrEmpty(schema?.SourceUri) ? null : new Uri(schema.SourceUri); + var source = CodeUtilities.CreateUri(schema?.SourceUri); CreateTypeModel(source, globalType, globalType.QualifiedName); } foreach (var rootElement in set.GlobalElements.Values.Cast()) { var rootSchema = rootElement.GetSchema(); - var source = !string.IsNullOrEmpty(rootSchema.SourceUri) ? new Uri(rootElement.GetSchema().SourceUri) : default(Uri); + var source = CodeUtilities.CreateUri(rootSchema.SourceUri); var qualifiedName = rootElement.ElementSchemaType.QualifiedName; if (qualifiedName.IsEmpty) { qualifiedName = rootElement.QualifiedName; } var type = CreateTypeModel(source, rootElement.ElementSchemaType, qualifiedName); @@ -165,7 +165,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaGroup group, NamespaceMod var properties = CreatePropertiesForElements(source, interfaceModel, particle, items.Where(i => !(i.XmlParticle is XmlSchemaGroupRef))); interfaceModel.Properties.AddRange(properties); var interfaces = items.Select(i => i.XmlParticle).OfType() - .Select(i => (InterfaceModel)CreateTypeModel(new Uri(i.SourceUri), Groups[i.RefName], i.RefName)); + .Select(i => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(i.SourceUri), Groups[i.RefName], i.RefName)); interfaceModel.Interfaces.AddRange(interfaces); return interfaceModel; @@ -192,7 +192,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAttributeGroup attributeG var properties = CreatePropertiesForAttributes(source, interfaceModel, items.OfType()); interfaceModel.Properties.AddRange(properties); var interfaces = items.OfType() - .Select(a => (InterfaceModel)CreateTypeModel(new Uri(a.SourceUri), AttributeGroups[a.RefName], a.RefName)); + .Select(a => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(a.SourceUri), AttributeGroups[a.RefName], a.RefName)); interfaceModel.Interfaces.AddRange(interfaces); return interfaceModel; @@ -254,7 +254,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType, if (_configuration.GenerateInterfaces) { var interfaces = items.Select(i => i.XmlParticle).OfType() - .Select(i => (InterfaceModel)CreateTypeModel(new Uri(i.SourceUri), Groups[i.RefName], i.RefName)); + .Select(i => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(i.SourceUri), Groups[i.RefName], i.RefName)); classModel.Interfaces.AddRange(interfaces); } @@ -283,7 +283,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType, if (_configuration.GenerateInterfaces) { var attributeInterfaces = attributes.OfType() - .Select(i => (InterfaceModel)CreateTypeModel(new Uri(i.SourceUri), AttributeGroups[i.RefName], i.RefName)); + .Select(i => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(i.SourceUri), AttributeGroups[i.RefName], i.RefName)); classModel.Interfaces.AddRange(attributeInterfaces); } } @@ -463,7 +463,7 @@ private IEnumerable CreatePropertiesForAttributes(Uri source, Typ { if (_configuration.GenerateInterfaces) { - CreateTypeModel(new Uri(attributeGroupRef.SourceUri), AttributeGroups[attributeGroupRef.RefName], attributeGroupRef.RefName); + CreateTypeModel(CodeUtilities.CreateUri(attributeGroupRef.SourceUri), AttributeGroups[attributeGroupRef.RefName], attributeGroupRef.RefName); } var groupItems = AttributeGroups[attributeGroupRef.RefName].Attributes; @@ -552,7 +552,7 @@ private IEnumerable CreatePropertiesForElements(Uri source, TypeM { if (_configuration.GenerateInterfaces) { - CreateTypeModel(new Uri(groupRef.SourceUri), Groups[groupRef.RefName], groupRef.RefName); + CreateTypeModel(CodeUtilities.CreateUri(groupRef.SourceUri), Groups[groupRef.RefName], groupRef.RefName); } var groupItems = GetElements(groupRef.Particle);