Skip to content

Commit

Permalink
Fixed the handling of empty http attributes in Batch Requests (#2705)
Browse files Browse the repository at this point in the history
  • Loading branch information
apiaskowski authored Jul 25, 2023
1 parent 6f3a47b commit 0cf10e3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private void ScanJsonProperties()
while (this.jsonReader.NodeType != JsonNodeType.EndObject)
{
string headerName = this.jsonReader.ReadPropertyName();
string headerValue = this.jsonReader.ReadPrimitiveValue().ToString();
string headerValue = this.jsonReader.ReadPrimitiveValue()?.ToString();

// Throw an ODataException, if a duplicate header was detected
if (headers.ContainsKeyOrdinal(headerName))
Expand Down Expand Up @@ -425,7 +425,7 @@ await this.asynchronousJsonReader.ReadStartObjectAsync()
{
string headerName = await this.asynchronousJsonReader.ReadPropertyNameAsync()
.ConfigureAwait(false);
string headerValue = (await this.asynchronousJsonReader.ReadPrimitiveValueAsync().ConfigureAwait(false)).ToString();
string headerValue = (await this.asynchronousJsonReader.ReadPrimitiveValueAsync().ConfigureAwait(false))?.ToString();

// Throw an ODataException, if a duplicate header was detected
if (headers.ContainsKeyOrdinal(headerName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,78 @@ await DoReadAsync(
isResponse: false);
}

[Fact]
public void ReadBatchRequestWithNullHeaders()
{
var payload = "{\"requests\": [{" +
"\"id\": \"1\"," +
"\"method\": \"POST\"," +
"\"url\": \"http://tempuri.org/Customers\"," +
"\"headers\": {\"odata-version\":\"4.0\",\"content-type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\",\"null-header\":null}, " +
"\"body\": {\"@odata.type\":\"#NS.Customer\",\"Id\":1,\"Name\":\"Customer 1\",\"Type\":\"Retail\"}}]}";

SetupJsonLightBatchReaderAndRunTest(
payload,
(jsonLightBatchReader) =>
{
try
{
while (jsonLightBatchReader.Read())
{
if (jsonLightBatchReader.State == ODataBatchReaderState.Operation)
{
var operationRequestMessage = jsonLightBatchReader.CreateOperationRequestMessage();
// Verify that the Property "null-header" exists and it's value is set to NULL
var nullHeaderProperty = operationRequestMessage.Headers.FirstOrDefault(p => p.Key == "null-header");
Assert.NotNull(nullHeaderProperty.Key);
Assert.Null(nullHeaderProperty.Value);
}
}
}
catch (NullReferenceException ex)
{
Assert.False(true, ex.Message);
}
},
isResponse: false);
}

[Fact]
public async Task ReadBatchRequestWithNullHeadersAsync()
{
var payload = "{\"requests\": [{" +
"\"id\": \"1\"," +
"\"method\": \"POST\"," +
"\"url\": \"http://tempuri.org/Customers\"," +
"\"headers\": {\"odata-version\":\"4.0\",\"content-type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\",\"null-header\":null}, " +
"\"body\": {\"@odata.type\":\"#NS.Customer\",\"Id\":1,\"Name\":\"Customer 1\",\"Type\":\"Retail\"}}]}";

await SetupJsonLightBatchReaderAndRunTestAsync(
payload,
async (jsonLightBatchReader) =>
{
try
{
while (await jsonLightBatchReader.ReadAsync())
{
if (jsonLightBatchReader.State == ODataBatchReaderState.Operation)
{
var operationRequestMessage = await jsonLightBatchReader.CreateOperationRequestMessageAsync();
// Verify that the Property "null-header" exists and it's value is set to NULL
var nullHeaderProperty = operationRequestMessage.Headers.FirstOrDefault(p => p.Key == "null-header");
Assert.NotNull(nullHeaderProperty.Key);
Assert.Null(nullHeaderProperty.Value);
}
}
}
catch (NullReferenceException ex)
{
Assert.False(true, ex.Message);
}
},
isResponse: false);
}

[Fact]
public void ReadBatchRequestWithDuplicateProperties()
{
Expand Down

0 comments on commit 0cf10e3

Please sign in to comment.