[WIP] Add Configuration references#129734
Draft
rosebyte wants to merge 4 commits into
Draft
Conversation
added 2 commits
June 23, 2026 11:04
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a new “configuration references” feature for Microsoft.Extensions.Configuration, enabling configuration values to be expanded via reference syntax (e.g., ref(Target:Key)) and composed values (e.g., format(template, key1, key2, ...)). It does so by adding a contextual configuration source/provider that materializes expanded values on top of upstream providers, plus a fairly comprehensive new test suite.
Changes:
- Adds a new reference rule model + pattern matcher (
ReferenceRule,KeyPattern) and a contextual configuration provider (ReferenceConfigurationProvider) to materialize expanded values and mirror subtrees. - Extends
ConfigurationBuilderandConfigurationManagerto support building “contextual” configuration sources that require an upstream provider snapshot. - Introduces new public API surface (
AllowReferences,ConfigurationReferenceBuilder,ConfigurationExpansion) and adds end-to-end tests.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.Configuration/tests/ConfigurationReferenceTests.cs | New test coverage for reference resolution, templates, composition, reload behavior, and parsing. |
| src/libraries/Microsoft.Extensions.Configuration/src/Resources/Strings.resx | Adds new user-facing error strings for reference expansion and validation. |
| src/libraries/Microsoft.Extensions.Configuration/src/ReferenceRule.cs | Defines rule semantics (subject + allowed/denied targets) and validates parser output. |
| src/libraries/Microsoft.Extensions.Configuration/src/ReferenceConfigurationSource.cs | Contextual source that requires upstream provider snapshot to build its provider. |
| src/libraries/Microsoft.Extensions.Configuration/src/ReferenceConfigurationProvider.cs | Core implementation: resolves references, mirrors subtrees, composes format expansions, handles reload. |
| src/libraries/Microsoft.Extensions.Configuration/src/ReferenceConfigurationBuilderExtensions.cs | Public builder extension method AllowReferences(...). |
| src/libraries/Microsoft.Extensions.Configuration/src/KeyPattern.cs | Wildcard/doublestar key-pattern matching for subjects/targets. |
| src/libraries/Microsoft.Extensions.Configuration/src/IContextualConfigurationSource.cs | Internal contract to allow sources to build with upstream provider list. |
| src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationReferenceBuilder.cs | Public builder object for declaring rules and customizing the parser. |
| src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationManager.cs | Adds contextual-source dispatch when adding/reloading sources. |
| src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationExpansion.cs | Public struct representing expansion kinds (reference/literal/format) returned by parsers. |
| src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationBuilder.cs | Adds contextual-source dispatch during build. |
| src/libraries/Microsoft.Extensions.Configuration/ref/Microsoft.Extensions.Configuration.cs | Updates public API contract to include the new APIs/types. |
Comment on lines
+52
to
+62
| public static ConfigurationExpansion Format(string template, params string[] referencedKeys) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(template); | ||
| ArgumentNullException.ThrowIfNull(referencedKeys); | ||
| return new ConfigurationExpansion(template, referencedKeys.Length switch | ||
| { | ||
| 0 => StringValues.Empty, | ||
| 1 => new StringValues(referencedKeys[0]), | ||
| _ => new StringValues(referencedKeys), | ||
| }); | ||
| } |
Comment on lines
+180
to
+184
| [Fact] | ||
| public void Configurable_ValidSelection_ResolvesThrough() | ||
| { | ||
| IConfigurationRoot root = BuilderWith(Dict( | ||
| ("Shared:Prod:Credential", "prod"), |
Comment on lines
+9
to
+13
| public sealed partial class ConfigurationReferenceBuilder | ||
| { | ||
| internal ConfigurationReferenceBuilder() { } | ||
| public Microsoft.Extensions.Configuration.ConfigurationReferenceBuilder Allow(string subject, string target, params string[] additionalTargets) { throw null; } | ||
| public Microsoft.Extensions.Configuration.ConfigurationReferenceBuilder Deny(string subject, string target, params string[] additionalTargets) { throw null; } |
Comment on lines
+43
to
+49
| ReferenceRule rule = FindOrCreateRule(NormaliseSubject(subject)); | ||
| rule.AddTarget(NormaliseTarget(target)); | ||
| foreach (string additionalTarget in additionalTargets) | ||
| { | ||
| rule.AddTarget(NormaliseTarget(additionalTarget)); | ||
| } | ||
| return this; |
Comment on lines
+67
to
+73
| ReferenceRule rule = FindOrCreateRule(NormaliseSubject(subject)); | ||
| rule.AddDisallowedTarget(NormaliseTarget(target)); | ||
| foreach (string additionalTarget in additionalTargets) | ||
| { | ||
| rule.AddDisallowedTarget(NormaliseTarget(additionalTarget)); | ||
| } | ||
| return this; |
Comment on lines
+389
to
+405
| // Reads the value already materialised for <paramref name="key"/> if any; otherwise | ||
| // falls back to the first non-empty upstream literal at the same key. | ||
| private string? OverlayAwareRead(string key, Dictionary<string, string?> values) | ||
| { | ||
| if (values.TryGetValue(key, out string? ov)) | ||
| { | ||
| return ov; | ||
| } | ||
| for (int i = _upstream.Count - 1; i >= 0; i--) | ||
| { | ||
| if (_upstream[i].TryGet(key, out string? pv) && !string.IsNullOrEmpty(pv)) | ||
| { | ||
| return pv; | ||
| } | ||
| } | ||
| return null; | ||
| } |
Comment on lines
+325
to
+335
| // A non-empty upstream literal at the overlay key should win on read; | ||
| // skip the overlay entry so it does. | ||
| string? sourceAtOverlay = ReadUpstream(overlayKey); | ||
| if (string.IsNullOrEmpty(sourceAtOverlay)) | ||
| { | ||
| string? targetValue = OverlayAwareRead(childKey, values); | ||
| if (targetValue is not null) | ||
| { | ||
| values[overlayKey] = targetValue; | ||
| } | ||
| } |
Comment on lines
+141
to
+146
| <data name="Error_ReferenceTargetsRequired" xml:space="preserve"> | ||
| <value>At least one non-empty reference target must be provided.</value> | ||
| </data> | ||
| <data name="Error_ReferenceTargetInvalid" xml:space="preserve"> | ||
| <value>'{0}' is not a valid reference target pattern. Each ':'-separated segment must be non-empty; '*' is permitted only as a whole segment.</value> | ||
| </data> |
added 2 commits
June 28, 2026 08:44
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.