Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Harden the PathString converter #857

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/Microsoft.AspNetCore.Http.Abstractions/PathString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,25 +443,35 @@ public override int GetHashCode()
/// </summary>
/// <param name="s"></param>
public static implicit operator PathString(string s)
{
return new PathString(s);
}
=> ConvertFromString(s);

/// <summary>
/// Implicitly calls ToString().
/// </summary>
/// <param name="path"></param>
public static implicit operator string(PathString path)
{
return path.ToString();
}
=> path.ToString();

internal static PathString ConvertFromString(string s)
=> string.IsNullOrEmpty(s) ? new PathString(s) : FromUriComponent(s);
}

internal class PathStringConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string)
? true
: base.CanConvertFrom(context, sourceType);

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return new PathString((string)value);
}
=> value is string
? PathString.ConvertFromString((string)value)
: base.ConvertFrom(context, culture, value);

public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
=> destinationType == typeof(string)
? value.ToString()
: base.ConvertTo(context, culture, value, destinationType);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -208,11 +208,27 @@ public void ToUriComponentEscapeCorrectly(string category, string input, string
}

[Fact]
public void PathStringConvertsFromString()
public void PathStringConvertsOnlyToAndFromString()
{
var converter = TypeDescriptor.GetConverter(typeof(PathString));
PathString result = (PathString)converter.ConvertFromInvariantString("/foo");
Assert.Equal("/foo", result.ToString());
Assert.Equal("/foo", converter.ConvertTo(result, typeof(string)));
Assert.True(converter.CanConvertFrom(typeof(string)));
Assert.False(converter.CanConvertFrom(typeof(int)));
Assert.False(converter.CanConvertFrom(typeof(bool)));
Assert.True(converter.CanConvertTo(typeof(string)));
Assert.False(converter.CanConvertTo(typeof(int)));
Assert.False(converter.CanConvertTo(typeof(bool)));
}

[Fact]
public void PathStringStaysEqualAfterAssignments()
{
PathString p1 = "/?";
string s1 = p1;
PathString p2 = s1;
Assert.Equal(p1, p2);
}
}
}