From 6c5b29e89474d2e99a050a047917f3df88e1dec6 Mon Sep 17 00:00:00 2001 From: Nobuyuki Yokomizu Date: Thu, 10 Feb 2022 22:03:27 +0900 Subject: [PATCH 1/3] Support relative context URI starting with "$metadata#" --- .../JsonLight/ODataJsonLightDeserializer.cs | 6 ++++-- .../JsonLight/ODataJsonLightDeserializerTests.cs | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs b/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs index e0df4a832c..f288d3ef7f 100644 --- a/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs +++ b/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs @@ -249,10 +249,12 @@ internal void ReadPayloadStart( { // If the Base uri string is http://odata.org/test // The MetadataDocumentUri will be http://odata.org/test/$metadata - // If the contextUriAnnotation value is Customers(1)/Name + // If the contextUriAnnotation value is Customers(1)/Name or $metadata#Customers(1)/Name // The generated context uri will be http://odata.org/test/$metadata#Customers(1)/Name ODataUri oDataUri = new ODataUri() { ServiceRoot = this.BaseUri }; - contextUriAnnotationValue = oDataUri.MetadataDocumentUri.ToString() + ODataConstants.ContextUriFragmentIndicator + contextUriAnnotationValue; + contextUriAnnotationValue = contextUriAnnotationValue.StartsWith("$metadata", StringComparison.OrdinalIgnoreCase) + ? new Uri(this.BaseUri, contextUriAnnotationValue).ToString() + : oDataUri.MetadataDocumentUri.ToString() + ODataConstants.ContextUriFragmentIndicator + contextUriAnnotationValue; } ODataJsonLightContextUriParseResult parseResult = null; diff --git a/test/FunctionalTests/Microsoft.OData.Core.Tests/JsonLight/ODataJsonLightDeserializerTests.cs b/test/FunctionalTests/Microsoft.OData.Core.Tests/JsonLight/ODataJsonLightDeserializerTests.cs index b7c9a716a7..2c4ed43c23 100644 --- a/test/FunctionalTests/Microsoft.OData.Core.Tests/JsonLight/ODataJsonLightDeserializerTests.cs +++ b/test/FunctionalTests/Microsoft.OData.Core.Tests/JsonLight/ODataJsonLightDeserializerTests.cs @@ -878,6 +878,12 @@ public void TopLevelPropertyShouldReadContextUriAsRelativeUri() Assert.NotNull(property); Assert.Equal("http://odata.org/test/$metadata#Customers(1)/Name", deserializer.ContextUriParseResult.ContextUri.ToString()); + + deserializer = new ODataJsonLightPropertyAndValueDeserializer(this.CreateJsonLightInputContext("{\"@odata.context\":\"$metadata#Customers(1)/Name\",\"value\":\"Joe\"}", model)); + property = deserializer.ReadTopLevelProperty(primitiveTypeRef); + + Assert.NotNull(property); + Assert.Equal("http://odata.org/test/$metadata#Customers(1)/Name", deserializer.ContextUriParseResult.ContextUri.ToString()); } From a4f012ec4aad6b342ddb8013766d37ca50c7749b Mon Sep 17 00:00:00 2001 From: Nobuyuki Yokomizu Date: Fri, 11 Feb 2022 07:48:16 +0900 Subject: [PATCH 2/3] Remove redundant Uri constructor usage --- .../JsonLight/ODataJsonLightDeserializer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs b/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs index f288d3ef7f..098c830eec 100644 --- a/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs +++ b/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs @@ -253,7 +253,7 @@ internal void ReadPayloadStart( // The generated context uri will be http://odata.org/test/$metadata#Customers(1)/Name ODataUri oDataUri = new ODataUri() { ServiceRoot = this.BaseUri }; contextUriAnnotationValue = contextUriAnnotationValue.StartsWith("$metadata", StringComparison.OrdinalIgnoreCase) - ? new Uri(this.BaseUri, contextUriAnnotationValue).ToString() + ? this.BaseUri + contextUriAnnotationValue : oDataUri.MetadataDocumentUri.ToString() + ODataConstants.ContextUriFragmentIndicator + contextUriAnnotationValue; } From 94806316a3eeba87006218e2debe02229fcba041 Mon Sep 17 00:00:00 2001 From: nobyk Date: Fri, 18 Feb 2022 05:27:22 +0900 Subject: [PATCH 3/3] Update src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs Check relative context URI including "#". Co-authored-by: Kennedy Kang'ethe --- .../JsonLight/ODataJsonLightDeserializer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs b/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs index 098c830eec..a3ed74cc89 100644 --- a/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs +++ b/src/Microsoft.OData.Core/JsonLight/ODataJsonLightDeserializer.cs @@ -252,7 +252,7 @@ internal void ReadPayloadStart( // If the contextUriAnnotation value is Customers(1)/Name or $metadata#Customers(1)/Name // The generated context uri will be http://odata.org/test/$metadata#Customers(1)/Name ODataUri oDataUri = new ODataUri() { ServiceRoot = this.BaseUri }; - contextUriAnnotationValue = contextUriAnnotationValue.StartsWith("$metadata", StringComparison.OrdinalIgnoreCase) + contextUriAnnotationValue = contextUriAnnotationValue.StartsWith("$metadata#", StringComparison.OrdinalIgnoreCase) ? this.BaseUri + contextUriAnnotationValue : oDataUri.MetadataDocumentUri.ToString() + ODataConstants.ContextUriFragmentIndicator + contextUriAnnotationValue; }