Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UriKind.RelativeOrAbsolute to allow deserializing of relative URIs #401

Merged
merged 4 commits into from
Feb 22, 2022
Merged
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
2 changes: 1 addition & 1 deletion Source/Common/ValuesJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private static bool TryProcessTokenAsType(ref Utf8JsonReader reader, Type target
}
else if (targetType == typeof(Uri))
{
success = Uri.TryCreate(valueString, UriKind.Absolute, out var localResult);
success = Uri.TryCreate(valueString, UriKind.RelativeOrAbsolute, out var localResult);
result = localResult;
}
else if (targetType == typeof(Guid))
Expand Down
10 changes: 10 additions & 0 deletions Tests/Schema.NET.Test/ValuesJsonConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ public void ReadJson_ParseValueToken_UriAsString()
Assert.Equal(new Uri("https://schema.org/Thing"), result.Value2.First());
}

[Fact]
public void ReadJson_ParseValueToken_RelativeUriAsString()
{
var json = "{\"Property\":\"thing\"}";
var result = DeserializeObject<Values<string, Uri>>(json);
Assert.Equal(new Uri("thing", UriKind.Relative), result.Value2.First());
}

[Fact]
public void ReadJson_Values_SingleValue_ThingInterface()
{
Expand All @@ -273,6 +281,7 @@ public void ReadJson_Values_SingleValue_ThingInterface()
"\"@id\":\"https://example.com/book/1\"," +
"\"name\":\"The Catcher in the Rye\"," +
"\"url\":\"https://www.barnesandnoble.com/store/info/offer/JDSalinger\"," +
"\"image\":\"book1.jpg\"," +
"\"author\":{" +
"\"@type\":\"Person\"," +
"\"name\":\"J.D. Salinger\"" +
Expand All @@ -285,6 +294,7 @@ public void ReadJson_Values_SingleValue_ThingInterface()
Assert.Equal(new Uri("https://example.com/book/1"), ((Book)actual).Id);
Assert.Equal("The Catcher in the Rye", actual.Name);
Assert.Equal(new Uri("https://www.barnesandnoble.com/store/info/offer/JDSalinger"), (Uri)actual.Url!);
Assert.Equal(new Uri("book1.jpg", UriKind.Relative), (Uri)actual.Image!);
var author = Assert.Single(actual.Author.Value2);
Assert.Equal("J.D. Salinger", author.Name);
}
Expand Down