From d71f708d00cb8c41c5ed445baffa1a1f14d83e6a Mon Sep 17 00:00:00 2001 From: Bratchikov Igor Date: Wed, 23 Dec 2020 23:49:22 +0500 Subject: [PATCH] Fix MergeIndividualAndBatchPreferences method (#2377) * Fix MergeIndividualAndBatchPreferences method for same Prefer headers case (bug with an unnecessary comma at the end of line) * Add test merging same Prefer header while processing batch. Co-authored-by: Tsar Nikolay --- .../Batch/ODataBatchReaderExtensions.cs | 5 +++++ .../Batch/DefaultODataBatchHandlerTest.cs | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Microsoft.AspNetCore.OData/Batch/ODataBatchReaderExtensions.cs b/src/Microsoft.AspNetCore.OData/Batch/ODataBatchReaderExtensions.cs index 638eb6af6e..e054e0fa57 100644 --- a/src/Microsoft.AspNetCore.OData/Batch/ODataBatchReaderExtensions.cs +++ b/src/Microsoft.AspNetCore.OData/Batch/ODataBatchReaderExtensions.cs @@ -332,6 +332,11 @@ private static string MergeIndividualAndBatchPreferences(string individualPrefer .Where(pref => !individualPreferenceNames.Contains(pref.Split('=').FirstOrDefault())); string filteredBatchPreferences = string.Join(",", filteredBatchList); + if (string.IsNullOrEmpty(filteredBatchPreferences)) + { + return individualPreferences; + } + return string.Join(",", individualPreferences, filteredBatchPreferences); } diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Batch/DefaultODataBatchHandlerTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Batch/DefaultODataBatchHandlerTest.cs index 0ed98616be..4b7a6fcf0b 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Batch/DefaultODataBatchHandlerTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Batch/DefaultODataBatchHandlerTest.cs @@ -561,6 +561,16 @@ public async Task SendAsync_Works_ForBatchRequestWithInsertedEntityReferencedInA "DELETE,ContentType=,ContentLength=,Prefer=wait=100,handling=lenient", "POST,ContentType=text/plain; charset=utf-8,ContentLength=3,Prefer=" }, + { + // should not concatenate preferences that should not be inherited + new [] + { + "wait=100,handling=lenient" + }, + "GET,ContentType=,ContentLength=,Prefer=", + "DELETE,ContentType=,ContentLength=,Prefer=wait=100,handling=lenient", + "POST,ContentType=text/plain; charset=utf-8,ContentLength=3,Prefer=" + }, { // inheritable preferences should be copied over // and combined with the individual request's own preferences if any