Skip to content

Commit 372ab25

Browse files
Copilotdavidfowl
andcommitted
Simplify normalization logic to apply to all configuration keys
Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
1 parent 37eff3c commit 372ab25

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/Aspire.Hosting/ParameterResourceBuilderExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,16 @@ 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;
207206
configurationKey ??= $"Parameters:{name}";
208207

209208
// First try to get the value with the exact configuration key
210209
var value = configuration[configurationKey];
211210

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

tests/Aspire.Hosting.Tests/AddParameterTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public void ParameterWithoutDash_DoesNotFallbackToUnderscore()
581581
}
582582

583583
[Fact]
584-
public void ParameterWithCustomConfigurationKey_DoesNotUseFallback()
584+
public void ParameterWithCustomConfigurationKey_UsesFallback()
585585
{
586586
// Arrange
587587
var appBuilder = DistributedApplication.CreateBuilder();
@@ -592,14 +592,14 @@ public void ParameterWithCustomConfigurationKey_DoesNotUseFallback()
592592
["CustomSection:my_key"] = "custom-value"
593593
});
594594

595-
// Act - use custom configuration key
595+
// Act - use custom configuration key with dash
596596
appBuilder.AddParameterFromConfiguration("my-param", "CustomSection:my-key");
597597

598598
using var app = appBuilder.Build();
599599
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
600600
var parameterResource = Assert.Single(appModel.Resources.OfType<ParameterResource>());
601601

602-
// Assert - should find the value using the custom key without any fallback logic
602+
// Assert - should find the value using the normalized key (dash -> underscore)
603603
#pragma warning disable CS0618 // Type or member is obsolete
604604
Assert.Equal("custom-value", parameterResource.Value);
605605
#pragma warning restore CS0618 // Type or member is obsolete

0 commit comments

Comments
 (0)