From 7fc90d717c451f12ebafaed2d73bb7aa16e76f74 Mon Sep 17 00:00:00 2001 From: dermotblairca <81159972+dermotblairca@users.noreply.github.com> Date: Sun, 20 Feb 2022 14:14:02 +0000 Subject: [PATCH 1/4] Use UriKind.RelativeOrAbsolute to allow deserializing of relative URIs --- Source/Common/ValuesJsonConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Common/ValuesJsonConverter.cs b/Source/Common/ValuesJsonConverter.cs index 66e5ed8a..1f5dac07 100644 --- a/Source/Common/ValuesJsonConverter.cs +++ b/Source/Common/ValuesJsonConverter.cs @@ -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)) From 1f6617c799d55f5db3d18a60093355ab2bfe7341 Mon Sep 17 00:00:00 2001 From: dermotblairca <81159972+dermotblairca@users.noreply.github.com> Date: Mon, 21 Feb 2022 17:06:07 +0000 Subject: [PATCH 2/4] Adding new test and adding to existing test --- Tests/Schema.NET.Test/ValuesJsonConverterTest.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs b/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs index 7880e782..98adba9e 100644 --- a/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs +++ b/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs @@ -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>(json); + Assert.Equal(new Uri("/Thing", UriKind.Relative), result.Value2.First()); + } + [Fact] public void ReadJson_Values_SingleValue_ThingInterface() { @@ -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\":\"/images/book/1\"," + "\"author\":{" + "\"@type\":\"Person\"," + "\"name\":\"J.D. Salinger\"" + @@ -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("/images/book/1", UriKind.Relative), (Uri)actual.Image!); var author = Assert.Single(actual.Author.Value2); Assert.Equal("J.D. Salinger", author.Name); } From 4eb9a5b3ab7453ef2de3145eba8b181c67540b23 Mon Sep 17 00:00:00 2001 From: dermotblairca <81159972+dermotblairca@users.noreply.github.com> Date: Tue, 22 Feb 2022 17:33:32 +0000 Subject: [PATCH 3/4] Update ValuesJsonConverterTest.cs Seeing if RelativeOrAbsolute fixes unit tests --- Tests/Schema.NET.Test/ValuesJsonConverterTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs b/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs index 98adba9e..c3dce8fd 100644 --- a/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs +++ b/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs @@ -268,7 +268,7 @@ public void ReadJson_ParseValueToken_RelativeUriAsString() { var json = "{\"Property\":\"/Thing\"}"; var result = DeserializeObject>(json); - Assert.Equal(new Uri("/Thing", UriKind.Relative), result.Value2.First()); + Assert.Equal(new Uri("/Thing", UriKind.RelativeOrAbsolute), result.Value2.First()); } [Fact] @@ -294,7 +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("/images/book/1", UriKind.Relative), (Uri)actual.Image!); + Assert.Equal(new Uri("/images/book/1", UriKind.RelativeOrAbsolute), (Uri)actual.Image!); var author = Assert.Single(actual.Author.Value2); Assert.Equal("J.D. Salinger", author.Name); } From 5d8834655cee4010513c15c4e066bf5d39c83188 Mon Sep 17 00:00:00 2001 From: dermotblairca <81159972+dermotblairca@users.noreply.github.com> Date: Tue, 22 Feb 2022 17:47:38 +0000 Subject: [PATCH 4/4] Update ValuesJsonConverterTest.cs Removing slash so it doesn't think it's a file on some environments. --- Tests/Schema.NET.Test/ValuesJsonConverterTest.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs b/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs index c3dce8fd..61103413 100644 --- a/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs +++ b/Tests/Schema.NET.Test/ValuesJsonConverterTest.cs @@ -266,9 +266,9 @@ public void ReadJson_ParseValueToken_UriAsString() [Fact] public void ReadJson_ParseValueToken_RelativeUriAsString() { - var json = "{\"Property\":\"/Thing\"}"; + var json = "{\"Property\":\"thing\"}"; var result = DeserializeObject>(json); - Assert.Equal(new Uri("/Thing", UriKind.RelativeOrAbsolute), result.Value2.First()); + Assert.Equal(new Uri("thing", UriKind.Relative), result.Value2.First()); } [Fact] @@ -281,7 +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\":\"/images/book/1\"," + + "\"image\":\"book1.jpg\"," + "\"author\":{" + "\"@type\":\"Person\"," + "\"name\":\"J.D. Salinger\"" + @@ -294,7 +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("/images/book/1", UriKind.RelativeOrAbsolute), (Uri)actual.Image!); + 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); }