Skip to content

Fix NullReferenceException in ConfigurableResponseNegotiator when Accept-Charset header is absent#3

Merged
gimlichael merged 2 commits intov1.0.0/initialfrom
copilot/sub-pr-1-again
Mar 1, 2026
Merged

Fix NullReferenceException in ConfigurableResponseNegotiator when Accept-Charset header is absent#3
gimlichael merged 2 commits intov1.0.0/initialfrom
copilot/sub-pr-1-again

Conversation

Copy link

Copilot AI commented Mar 1, 2026

GetTypedHeaders().AcceptCharset returns null when no Accept-Charset header is present in the request, causing a NullReferenceException on .Count.

Change

Applied null-conditional operator in ConfigurableResponseNegotiator.GetEncoding:

// Before
if (acceptCharset.Count > 0)

// After
if (acceptCharset?.Count > 0)

When AcceptCharset is null, ?.Count yields null, the condition evaluates to false, and the method falls through to GetDefaultEncoding() as intended.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…on AcceptCharset.Count

Co-authored-by: gimlichael <8550919+gimlichael@users.noreply.github.com>
Copilot AI changed the title [WIP] Address feedback from review on V1.0.0 initial pull request Fix NullReferenceException in ConfigurableResponseNegotiator when Accept-Charset header is absent Mar 1, 2026
@gimlichael gimlichael marked this pull request as ready for review March 1, 2026 22:39
Copilot AI review requested due to automatic review settings March 1, 2026 22:39
@gimlichael gimlichael merged commit fe63839 into v1.0.0/initial Mar 1, 2026
2 checks passed
@gimlichael gimlichael deleted the copilot/sub-pr-1-again branch March 1, 2026 22:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a NullReferenceException in ConfigurableResponseNegotiator.GetEncoding when a request does not include an Accept-Charset header, so the negotiator falls back to the default encoding as intended.

Changes:

  • Updated the Accept-Charset count check in GetEncoding to be null-aware.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

{
var acceptCharset = request.GetTypedHeaders().AcceptCharset;
if (acceptCharset.Count > 0)
if (acceptCharset?.Count > 0)
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

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

acceptCharset?.Count > 0 produces a nullable boolean (because ?.Count is int?), which cannot be used directly as an if condition. This will fail to compile. Use a non-nullable condition (e.g., coalesce the count to 0, compare to true, or use a property pattern like checking acceptCharset is { Count: > 0 }).

Suggested change
if (acceptCharset?.Count > 0)
if (acceptCharset is { Count: > 0 })

Copilot uses AI. Check for mistakes.
Comment on lines 70 to +71
var acceptCharset = request.GetTypedHeaders().AcceptCharset;
if (acceptCharset.Count > 0)
if (acceptCharset?.Count > 0)
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

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

Consider adding a unit test for the reported scenario (no Accept-Charset header) to ensure GetEncoding returns the default encoding and does not throw. There are already GetEncoding_* tests in the test project, but none cover the missing-header case.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants