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

Check for empty list instead of null value. #630

Merged
merged 4 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ public override void WriteResponseHeaders(OutputFormatterWriteContext context)
// Set the character set.
MediaTypeHeaderValue currentContentType = GetContentType(response.Headers[HeaderNames.ContentType].FirstOrDefault());
RequestHeaders requestHeader = request.GetTypedHeaders();
if (requestHeader != null && requestHeader.AcceptCharset != null)
// Starting from ASP .NET Core 3.0 AcceptCharset returns an empty collection instead of null.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

if (requestHeader?.AcceptCharset.Count > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (requestHeader?.AcceptCharset.Count > 0)
if (requestHeader?.AcceptCharset?.Count > 0)

Let's be compatible with both versions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIR this repo is for ASP .NET Core 3+. So it can't be ran with ASP .NET Core 2.x. This change make sense for OData/WebApi repo. Correct me if I'm wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also lean in favor of the AcceptCharset?.Count check

Copy link
Contributor

@habbes habbes Jul 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the standard lib explicitly marks the properties that can be null as nullable in newer .NET versions e.g.: https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.headers.requestheaders.cachecontrol?view=aspnetcore-6.0

I don't think that was the case for .NET Core 3.1 so it's hard to tell from just looking at the property whether it's safe to assume it can't be null.

{
IEnumerable<string> acceptCharsetValues = requestHeader.AcceptCharset.Select(cs => cs.Value.Value);

Expand Down
26 changes: 26 additions & 0 deletions test/Microsoft.AspNetCore.OData.E2E.Tests/Enums/EnumsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,32 @@ public async Task QueryEntitySetCount(string requestUri, int expectedCount)
Assert.Equal<int>(expectedCount, int.Parse(count));
}

[Theory]
[InlineData("/convention/Employees/$count", true)]
[InlineData("/convention/Employees/$count", false)]
public async Task QueryEntitySetCountEncoding(string requestUri, bool sendAcceptCharset)
{
// Arrange
await ResetDatasource();
HttpClient client = CreateClient();
client.DefaultRequestHeaders.AcceptCharset.Clear();
if (sendAcceptCharset)
{
client.DefaultRequestHeaders.AcceptCharset.ParseAdd("utf-8");
}

// Act
HttpResponseMessage response = await client.GetAsync(requestUri);

// Assert
Assert.True(response.IsSuccessStatusCode);
var blob = await response.Content.ReadAsByteArrayAsync();
Assert.Equal("utf-8", response.Content.Headers.ContentType.CharSet, StringComparer.OrdinalIgnoreCase);
Assert.False((blob[0] == 0xEF) && (blob[1] == 0xBB) && (blob[2] == 0xBF));
var count = Encoding.UTF8.GetString(blob);
Assert.Equal(3, int.Parse(count));
}

[Theory]
[InlineData("/convention/Employees(1)/SkillSet/$count", 2)]
[InlineData("/convention/Employees(1)/SkillSet/$count?$filter=$it eq Microsoft.AspNetCore.OData.E2E.Tests.Enums.Skill'Sql'", 1)]
Expand Down