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

Commit 12f89f6

Browse files
committed
Improve PathString <-> string logic
1 parent 835fb60 commit 12f89f6

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

Diff for: src/Microsoft.AspNetCore.Http.Abstractions/PathString.cs

+19-9
Original file line numberDiff line numberDiff line change
@@ -443,25 +443,35 @@ public override int GetHashCode()
443443
/// </summary>
444444
/// <param name="s"></param>
445445
public static implicit operator PathString(string s)
446-
{
447-
return new PathString(s);
448-
}
446+
=> ConvertFromString(s);
449447

450448
/// <summary>
451449
/// Implicitly calls ToString().
452450
/// </summary>
453451
/// <param name="path"></param>
454452
public static implicit operator string(PathString path)
455-
{
456-
return path.ToString();
457-
}
453+
=> path.ToString();
454+
455+
internal static PathString ConvertFromString(string s)
456+
=> string.IsNullOrEmpty(s) ? new PathString(s) : FromUriComponent(s);
458457
}
459458

460459
internal class PathStringConverter : TypeConverter
461460
{
461+
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
462+
=> sourceType == typeof(string)
463+
? true
464+
: base.CanConvertFrom(context, sourceType);
465+
462466
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
463-
{
464-
return new PathString((string)value);
465-
}
467+
=> value is string
468+
? PathString.ConvertFromString((string)value)
469+
: base.ConvertFrom(context, culture, value);
470+
471+
public override object ConvertTo(ITypeDescriptorContext context,
472+
CultureInfo culture, object value, Type destinationType)
473+
=> destinationType == typeof(string)
474+
? value.ToString()
475+
: base.ConvertTo(context, culture, value, destinationType);
466476
}
467477
}

Diff for: test/Microsoft.AspNetCore.Http.Abstractions.Tests/PathStringTests.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -208,11 +208,27 @@ public void ToUriComponentEscapeCorrectly(string category, string input, string
208208
}
209209

210210
[Fact]
211-
public void PathStringConvertsFromString()
211+
public void PathStringConvertsOnlyToAndFromString()
212212
{
213213
var converter = TypeDescriptor.GetConverter(typeof(PathString));
214214
PathString result = (PathString)converter.ConvertFromInvariantString("/foo");
215215
Assert.Equal("/foo", result.ToString());
216+
Assert.Equal("/foo", converter.ConvertTo(result, typeof(string)));
217+
Assert.True(converter.CanConvertFrom(typeof(string)));
218+
Assert.False(converter.CanConvertFrom(typeof(int)));
219+
Assert.False(converter.CanConvertFrom(typeof(bool)));
220+
Assert.True(converter.CanConvertTo(typeof(string)));
221+
Assert.False(converter.CanConvertTo(typeof(int)));
222+
Assert.False(converter.CanConvertTo(typeof(bool)));
223+
}
224+
225+
[Fact]
226+
public void PathStringStaysEqualAfterAssignments()
227+
{
228+
PathString p1 = "/?";
229+
string s1 = p1;
230+
PathString p2 = s1;
231+
Assert.Equal(p1, p2);
216232
}
217233
}
218234
}

0 commit comments

Comments
 (0)