Skip to content

Add configurable VariableNameTransformation to EnvironmentVariables config#127503

Merged
svick merged 6 commits into
dotnet:mainfrom
svick:env-var-name-transformation
May 20, 2026
Merged

Add configurable VariableNameTransformation to EnvironmentVariables config#127503
svick merged 6 commits into
dotnet:mainfrom
svick:env-var-name-transformation

Conversation

@svick
Copy link
Copy Markdown
Member

@svick svick commented Apr 28, 2026

Implements #125137.

API additions

  • IConfigurationBuilder.AddEnvironmentVariables(string? prefix, Func<string, string>? variableNameTransformation) overload.
  • EnvironmentVariablesConfigurationSource.VariableNameTransformation -- a settable Func<string, string>? that, when non-null, completely replaces the default transformation.
  • EnvironmentVariablesConfigurationSource.DefaultTransformation -- the existing default that replaces __ with the configuration key delimiter (:).
  • EnvironmentVariablesConfigurationSource.ColonAndDotTransformation -- replaces ___ with . and __ with :, so configuration keys containing dots can be set from environment variables.

API shape matches @bartonjs's approval comment on the issue.

Implementation notes

  • ColonAndDotTransformation uses ValueStringBuilder + stackalloc char[256] on .NET, StringBuilder on .NET Framework, kept under a single small #if NET region. No new package references.
  • Prefix is itself passed through the active transformation before comparison, so it should be specified in pre-transformation form (e.g. "Logging__" rather than "Logging:"). Documented in <remarks> on the Prefix property and the matching overloads.

Tests

13 new tests in EnvironmentVariablesTest cover:

  • The two built-in transformations behaviorally ([Theory] tables for double/triple/4/5/6 underscores, single-underscore preservation, no-underscore early return).
  • VariableNameTransformation replacing the default, identity transformation disabling replacement, transformation applied to prefix, source/builder wiring, and a behavioral check that ColonAndDotTransformation is not the default.

Note

This PR description and the implementation were drafted with assistance from GitHub Copilot.

…onfig (dotnet#125137)

Adds:

- AddEnvironmentVariables(prefix, variableNameTransformation) overload on IConfigurationBuilder.
- EnvironmentVariablesConfigurationSource.VariableNameTransformation: a settable property that, when non-null, replaces the default transformation behavior.
- EnvironmentVariablesConfigurationSource.DefaultTransformation: the existing default that replaces __ with the configuration key delimiter (:).
- EnvironmentVariablesConfigurationSource.ColonAndDotTransformation: replaces ___ with . and __ with :, allowing dots in configuration keys without the override-by-environment ergonomics being limited to colons.

ColonAndDotTransformation is implemented with ValueStringBuilder on .NET and StringBuilder on .NET Framework.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

LGTM.

@svick svick marked this pull request as ready for review May 15, 2026 16:57
Copy link
Copy Markdown
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

Added some minor comments. Still the changes LGTM 😄

Copy link
Copy Markdown
Member

@rosebyte rosebyte left a comment

Choose a reason for hiding this comment

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

LGTM, I left couple suggestions, none of them blocking.

@svick svick enabled auto-merge (squash) May 20, 2026 16:04
@svick svick merged commit 2a65f10 into dotnet:main May 20, 2026
92 of 94 checks passed
@svick svick deleted the env-var-name-transformation branch May 21, 2026 11:44
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview6 May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Replacement of forbidden characters in variable names in EnvironmentVariablesConfigurationProvider

5 participants