Skip to content

Commit 939ffe6

Browse files
twsouthwickstephentoub
authored andcommitted
Use Mono workaround to parse internal relationships in packages (dotnet#25275)
1 parent 1520d0b commit 939ffe6

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/System.IO.Packaging/src/System/IO/Packaging/InternalRelationshipCollection.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ namespace System.IO.Packaging
2727
/// </summary>
2828
internal class InternalRelationshipCollection : IEnumerable<PackageRelationship>
2929
{
30+
// Mono will parse a URI starting with '/' as an absolute URI, while .NET Core and
31+
// .NET Framework will parse this as relative. This will break internal relationships
32+
// in packaging. For more information, see
33+
// http://www.mono-project.com/docs/faq/known-issues/urikind-relativeorabsolute/
34+
private static readonly UriKind DotNetRelativeOrAbsolute = Type.GetType ("Mono.Runtime") == null ? UriKind.RelativeOrAbsolute : (UriKind)300;
35+
3036
#region IEnumerable
3137
/// <summary>
3238
/// Returns an enumerator over all the relationships for a Package or a PackagePart
@@ -354,7 +360,7 @@ private void ProcessRelationshipAttributes(XmlCompatibilityReader reader)
354360
if (string.IsNullOrEmpty(targetAttributeValue))
355361
throw new XmlException(SR.Format(SR.RequiredRelationshipAttributeMissing, s_targetAttributeName), null, reader.LineNumber, reader.LinePosition);
356362

357-
Uri targetUri = new Uri(targetAttributeValue, UriKind.RelativeOrAbsolute);
363+
Uri targetUri = new Uri(targetAttributeValue, DotNetRelativeOrAbsolute);
358364

359365
// Attribute : Type
360366
string typeAttributeValue = reader.GetAttribute(s_typeAttributeName);

src/System.IO.Packaging/tests/Tests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,31 @@ public void T036_CreateRelationshipWithId()
12791279
packagePath1.Delete();
12801280
}
12811281

1282+
[Fact]
1283+
public void OpenInternalTargetRelationships()
1284+
{
1285+
// This is to test different behavior on Mono vs .NET Core
1286+
using (var ms = new MemoryStream())
1287+
{
1288+
using (var package = Package.Open(ms, FileMode.OpenOrCreate, FileAccess.ReadWrite))
1289+
{
1290+
package.CreateRelationship(new Uri("/target", UriKind.Relative), TargetMode.Internal, "type");
1291+
}
1292+
1293+
ms.Position = 0;
1294+
1295+
using (var package = Package.Open(ms, FileMode.OpenOrCreate, FileAccess.ReadWrite))
1296+
{
1297+
var relationships = package.GetRelationships();
1298+
1299+
var relationship = Assert.Single(relationships);
1300+
1301+
Assert.Equal(new Uri("/", UriKind.Relative), relationship.SourceUri);
1302+
Assert.Equal(new Uri("/target", UriKind.Relative), relationship.TargetUri);
1303+
Assert.Equal(TargetMode.Internal, relationship.TargetMode);
1304+
}
1305+
}
1306+
}
12821307

12831308
[Fact]
12841309
public void T035_ModifyAllPackageProperties()

0 commit comments

Comments
 (0)