Skip to content

Commit

Permalink
Fix bugs creating Uris when uri string is null or empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Feb 13, 2019
1 parent 9fba85a commit bf267dc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
16 changes: 8 additions & 8 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set)
foreach (var globalType in set.GlobalTypes.Values.Cast<XmlSchemaType>())
{
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<XmlSchemaElement>())
{
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);
Expand Down Expand Up @@ -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<XmlSchemaGroupRef>()
.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;
Expand All @@ -192,7 +192,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaAttributeGroup attributeG
var properties = CreatePropertiesForAttributes(source, interfaceModel, items.OfType<XmlSchemaAttribute>());
interfaceModel.Properties.AddRange(properties);
var interfaces = items.OfType<XmlSchemaAttributeGroupRef>()
.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;
Expand Down Expand Up @@ -254,7 +254,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
if (_configuration.GenerateInterfaces)
{
var interfaces = items.Select(i => i.XmlParticle).OfType<XmlSchemaGroupRef>()
.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);
}

Expand Down Expand Up @@ -283,7 +283,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
if (_configuration.GenerateInterfaces)
{
var attributeInterfaces = attributes.OfType<XmlSchemaAttributeGroupRef>()
.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);
}
}
Expand Down Expand Up @@ -463,7 +463,7 @@ private IEnumerable<PropertyModel> 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;
Expand Down Expand Up @@ -552,7 +552,7 @@ private IEnumerable<PropertyModel> 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);
Expand Down

0 comments on commit bf267dc

Please sign in to comment.