Skip to content

Fix NpgsqlConnectionStringBuilder.Remove and Clear for string properties with defaults (#6724) - #6758

Open
lqkhanh295 wants to merge 1 commit into
npgsql:mainfrom
lqkhanh295:fix/connection-string-builder-remove-default-strings
Open

lqkhanh295 wants to merge 1 commit into
npgsql:mainfrom
lqkhanh295:fix/connection-string-builder-remove-default-strings

Conversation

@lqkhanh295

Copy link
Copy Markdown

Summary

Fixes #6724 where calling NpgsqlConnectionStringBuilder.Remove("Encoding") or Remove("Kerberos Service Name") did not remove the keys from the connection string, and calling Clear() left Encoding=UTF8 in the connection string.

Root Cause

In src/Npgsql.SourceGenerators/NpgsqlConnectionStringBuilder.snbtxt, the generated GeneratedAction.Remove branch contained:

{{~ if p.type_name != "String" ~}}
base.Remove(canonicalName);
{{~ else ~}}
// String property setters call SetValue, which itself calls base.Remove().
{{~ end ~}}

The template assumed that string property setters call SetValue, which in turn removes the key from the underlying DbConnectionStringBuilder dictionary. However, SetValue only calls base.Remove() when value == null. For string properties with non-null default values (Encoding defaulting to "UTF8" and KerberosServiceName defaulting to "postgres"), setting the property sets base[key] = value, keeping the default key in the dictionary.

Changes

  • Updated NpgsqlConnectionStringBuilder.snbtxt to unconditionally call base.Remove(canonicalName); during GeneratedAction.Remove.
  • Added unit tests in ConnectionStringBuilderTests.cs verifying Remove(), Clear(), and indexer builder[key] = null for string properties with defaults.

Testing

  • Rebuilt Npgsql.SourceGenerators and Npgsql.
  • Executed ConnectionStringBuilderTests (14/14 passed, 0 failures).

Copilot AI balanced review requested due to automatic review settings November 16, 2026 13:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

This branch has not been deployed

No deployments
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.

NpgsqlConnectionStringBuilder.Remove and Clear leave Encoding and Kerberos Service Name in the connection string

3 participants