Skip to content

Commit 37eff3c

Browse files
Copilotdavidfowl
andcommitted
Refine parameter normalization to only apply to default configuration keys
Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
1 parent 8c9c3b0 commit 37eff3c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Aspire.Hosting/ParameterResourceBuilderExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,15 @@ public static IResourceBuilder<ParameterResource> WithCustomInput(this IResource
203203

204204
private static string GetParameterValue(ConfigurationManager configuration, string name, ParameterDefault? parameterDefault, string? configurationKey = null)
205205
{
206+
var useDefaultKey = configurationKey is null;
206207
configurationKey ??= $"Parameters:{name}";
207208

208209
// First try to get the value with the exact configuration key
209210
var value = configuration[configurationKey];
210211

211-
// If not found and the name contains dashes, try with underscores as a fallback
212+
// If not found, the default key was used, and the name contains dashes, try with underscores as a fallback
212213
// This supports command-line arguments and environment variables where dashes are replaced with underscores
213-
if (value is null && name.Contains('-', StringComparison.Ordinal))
214+
if (value is null && useDefaultKey && name.Contains('-', StringComparison.Ordinal))
214215
{
215216
var normalizedKey = $"Parameters:{name.Replace("-", "_", StringComparison.Ordinal)}";
216217
value = configuration[normalizedKey];

tests/Aspire.Hosting.Tests/AddParameterTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,29 @@ public void ParameterWithoutDash_DoesNotFallbackToUnderscore()
579579
#pragma warning restore CS0618 // Type or member is obsolete
580580
});
581581
}
582+
583+
[Fact]
584+
public void ParameterWithCustomConfigurationKey_DoesNotUseFallback()
585+
{
586+
// Arrange
587+
var appBuilder = DistributedApplication.CreateBuilder();
588+
589+
// Set configuration with custom key that has underscore
590+
appBuilder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
591+
{
592+
["CustomSection:my_key"] = "custom-value"
593+
});
594+
595+
// Act - use custom configuration key
596+
appBuilder.AddParameterFromConfiguration("my-param", "CustomSection:my-key");
597+
598+
using var app = appBuilder.Build();
599+
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
600+
var parameterResource = Assert.Single(appModel.Resources.OfType<ParameterResource>());
601+
602+
// Assert - should find the value using the custom key without any fallback logic
603+
#pragma warning disable CS0618 // Type or member is obsolete
604+
Assert.Equal("custom-value", parameterResource.Value);
605+
#pragma warning restore CS0618 // Type or member is obsolete
606+
}
582607
}

0 commit comments

Comments
 (0)