Skip to content

fix: handle null values in Vault secrets to prevent NullReference#71

Closed
MarcelaCarvalho wants to merge 1 commit intoMrZoidberg:masterfrom
MarcelaCarvalho:master
Closed

fix: handle null values in Vault secrets to prevent NullReference#71
MarcelaCarvalho wants to merge 1 commit intoMrZoidberg:masterfrom
MarcelaCarvalho:master

Conversation

@MarcelaCarvalho
Copy link
Copy Markdown

This PR fixes an issue where the configuration provider throws a NullReferenceException when Vault secrets contain keys with null values. The root cause was an invalid cast of a null value to JsonElement.

The fix adds a null check and creates a JsonElement representing a JSON null (JsonValueKind.Null) when the value is null. This ensures that:

  • Keys with null values are properly included in the configuration.

  • No exceptions are thrown during configuration loading.

  • Consumers of IConfiguration can safely detect and handle keys with null values.

Issue: NullReferenceException when Vault secret contains null values in keys #70

Copy link
Copy Markdown

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

This PR fixes a NullReferenceException that occurred when Vault secrets contained keys with null values. The issue was that the code attempted to cast a null value directly to JsonElement without proper null checking, causing the application to crash during configuration loading.

Changes:

  • Added null value handling in the SetData method to properly create JsonElement instances representing JSON null values
  • Used JsonDocument.Parse("null") with Clone() to create a standalone JsonElement with JsonValueKind.Null that can be safely processed by SetItemData

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

Comment on lines +179 to +189
JsonElement nestedValue;

if (pair.Value == null)
{
using var doc = JsonDocument.Parse("null");
nestedValue = doc.RootElement.Clone();
}
else
{
nestedValue = (JsonElement)(object)pair.Value!;
}
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

This fix correctly handles null values from Vault secrets and properly creates a JsonElement representing a JSON null value. The use of JsonDocument.Parse with a using statement combined with Clone() is the correct pattern to create a JsonElement that outlives the JsonDocument's disposal. However, there is no test coverage for this specific scenario. Consider adding a test case that verifies null values in Vault secrets are properly handled and can be retrieved from IConfiguration.

Copilot uses AI. Check for mistakes.
@MrZoidberg
Copy link
Copy Markdown
Owner

done in #74 with tests. thanks for contributing!

@MrZoidberg MrZoidberg closed this Mar 2, 2026
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