Skip to content

Commit 7b5c29e

Browse files
authored
Move encoding to ReferenceExpression (#12082)
Fixes #12070
1 parent dcae024 commit 7b5c29e

File tree

11 files changed

+174
-186
lines changed

11 files changed

+174
-186
lines changed

src/Aspire.Hosting.Azure.AppContainers/Aspire.Hosting.Azure.AppContainers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<Compile Include="$(SharedDir)ResourceNameComparer.cs" LinkBase="Shared\ResourceNameComparer.cs" />
13+
<Compile Include="$(SharedDir)BicepFormattingHelpers.cs" LinkBase="Shared\BicepFormattingHelpers.cs" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Azure.Provisioning.AppContainers;
99
using Azure.Provisioning.Expressions;
1010
using Azure.Provisioning.Resources;
11+
using Aspire.Hosting.Azure.Utils;
1112

1213
namespace Aspire.Hosting.Azure;
1314

@@ -253,21 +254,6 @@ BicepValue<string> GetHostValue(string? prefix = null, string? suffix = null)
253254
return (AllocateParameter(output, secretType: secretType), secretType);
254255
}
255256

256-
if (value is IUrlEncoderProvider encoder && encoder.ValueProvider is { } valueProvider)
257-
{
258-
// Evaluate the inner value to get the bicep expression
259-
var (innerValue, secret) = ProcessValue(valueProvider, secretType: secretType, parent: parent);
260-
261-
var innerExpression = innerValue switch
262-
{
263-
ProvisioningParameter p => p.Value.Compile(),
264-
IBicepValue b => b.Compile(),
265-
_ => throw new ArgumentException($"Invalid expression type for url-encoding: {innerValue.GetType()}")
266-
};
267-
268-
return (new FunctionCallExpression(new IdentifierExpression("uriComponent"), innerExpression), secret);
269-
}
270-
271257
#pragma warning disable CS0618 // Type or member is obsolete
272258
if (value is BicepSecretOutputReference)
273259
{
@@ -303,7 +289,14 @@ BicepValue<string> GetHostValue(string? prefix = null, string? suffix = null)
303289
// Special case simple expressions
304290
if (expr.Format == "{0}" && expr.ValueProviders.Count == 1)
305291
{
306-
return ProcessValue(expr.ValueProviders[0], secretType, parent: parent);
292+
var val = ProcessValue(expr.ValueProviders[0], secretType, parent: parent);
293+
294+
if (expr.StringFormats[0] is string format)
295+
{
296+
val = (BicepFormattingHelpers.FormatBicepExpression(val, format), secretType);
297+
}
298+
299+
return val;
307300
}
308301

309302
var args = new object[expr.ValueProviders.Count];
@@ -319,11 +312,15 @@ BicepValue<string> GetHostValue(string? prefix = null, string? suffix = null)
319312
finalSecretType = SecretType.Normal;
320313
}
321314

315+
if (expr.StringFormats[index] is string format)
316+
{
317+
val = BicepFormattingHelpers.FormatBicepExpression(val, format);
318+
}
319+
322320
args[index++] = val;
323321
}
324322

325323
return (FormattableStringFactory.Create(expr.Format, args), finalSecretType);
326-
327324
}
328325

329326
if (value is IManifestExpressionProvider manifestExpressionProvider)

src/Aspire.Hosting.Azure.AppService/Aspire.Hosting.Azure.AppService.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<ItemGroup>
1313
<Compile Include="$(SharedDir)ResourceNameComparer.cs" LinkBase="Shared\ResourceNameComparer.cs" />
14+
<Compile Include="$(SharedDir)BicepFormattingHelpers.cs" LinkBase="Shared\BicepFormattingHelpers.cs" />
1415
</ItemGroup>
1516

1617
<ItemGroup>

src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Globalization;
66
using System.Runtime.CompilerServices;
77
using Aspire.Hosting.ApplicationModel;
8+
using Aspire.Hosting.Azure.Utils;
89
using Azure.Provisioning;
910
using Azure.Provisioning.AppService;
1011
using Azure.Provisioning.Authorization;
@@ -141,21 +142,6 @@ private void ProcessEndpoints()
141142
return (AllocateParameter(output, secretType: secretType), secretType);
142143
}
143144

144-
if (value is IUrlEncoderProvider encoder && encoder.ValueProvider is { } valueProvider)
145-
{
146-
// Evaluate the inner value to get the bicep expression
147-
var (innerValue, secret) = ProcessValue(valueProvider, secretType: secretType, parent: parent);
148-
149-
var innerExpression = innerValue switch
150-
{
151-
ProvisioningParameter p => p.Value.Compile(),
152-
IBicepValue b => b.Compile(),
153-
_ => throw new ArgumentException($"Invalid expression type for url-encoding: {innerValue.GetType()}")
154-
};
155-
156-
return (new FunctionCallExpression(new IdentifierExpression("uriComponent"), innerExpression), secret);
157-
}
158-
159145
if (value is IAzureKeyVaultSecretReference vaultSecretReference)
160146
{
161147
if (parent is null)
@@ -178,7 +164,14 @@ private void ProcessEndpoints()
178164
{
179165
if (expr.Format == "{0}" && expr.ValueProviders.Count == 1)
180166
{
181-
return ProcessValue(expr.ValueProviders[0], secretType, parent);
167+
var val = ProcessValue(expr.ValueProviders[0], secretType, parent: parent);
168+
169+
if (expr.StringFormats[0] is string format)
170+
{
171+
val = (BicepFormattingHelpers.FormatBicepExpression(val, format), secretType);
172+
}
173+
174+
return val;
182175
}
183176

184177
var args = new object[expr.ValueProviders.Count];
@@ -192,6 +185,12 @@ private void ProcessEndpoints()
192185
{
193186
finalSecretType = SecretType.Normal;
194187
}
188+
189+
if (expr.StringFormats[index] is string format)
190+
{
191+
val = BicepFormattingHelpers.FormatBicepExpression(val, format);
192+
}
193+
195194
args[index++] = val;
196195
}
197196

src/Aspire.Hosting/ApplicationModel/ExpressionResolver.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Globalization;
5+
using Aspire.Hosting.Utils;
56

67
namespace Aspire.Hosting.ApplicationModel;
78

@@ -43,6 +44,13 @@ async Task<ResolvedValue> EvalExpressionAsync(ReferenceExpression expr)
4344
{
4445
var result = await ResolveInternalAsync(expr.ValueProviders[i]).ConfigureAwait(false);
4546
args[i] = result?.Value;
47+
48+
// Apply string format if needed
49+
if (expr.StringFormats[i] is { } stringFormat && args[i] is string s)
50+
{
51+
args[i] = FormattingHelpers.FormatValue(s, stringFormat);
52+
}
53+
4654
if (result?.IsSensitive is true)
4755
{
4856
isSensitive = true;

src/Aspire.Hosting/ApplicationModel/IUrlEncoderProvider.cs

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)