microsoft / microsoft/aspire

ReferenceExpressionBuilder.Append throws FormatException when processing raw strings with single braces (JSON)

Open
#18,951 1 comment 0 reactions 0 assignees View on GitHub
area-app-model triage:bot-seen
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

PR #8994 that fixed #8974 introduced escaping logic for unescaped literal braces inside ReferenceExpression.Create (via ExpressionInterpolatedStringHandler). However, this fix was only applied to the interpolated string handler and did not cover ReferenceExpressionBuilder. When building a reference expression incrementally using ReferenceExpressionBuilder.Append(...), passing a raw text snippet containing valid JSON structures (unpaired/single curly braces) causes the internal parser to evaluate them as composite formatting tokens (expecting an ASCII digit index), leading to a crash.

### Expected Behavior

ReferenceExpressionBuilder.Append should implicitly handle or escape literal opening/closing curly braces within input strings, mirroring the robust behavior implemented for ReferenceExpression.Create.

### Steps To Reproduce

```csharp
using Aspire.Hosting.ApplicationModel;

// 1. Works fine (Fixed via PR #8994)
var pureExpr = ReferenceExpression.Create($$"""{"myjson":"myvalue"}""");
Console.WriteLine(pureExpr.Format);
await pureExpr.GetValueAsync(default);

// 2. Throws FormatException
var rb = new ReferenceExpressionBuilder();
rb.Append($$"""{"myjson":"myvalue"}""");
var rbExpr = rb.Build();
Console.WriteLine(rbExpr.Format);
await rbExpr.GetValueAsync(default);
```

Output is

```
{{"myjson":"myvalue"}}
{"myjson":"myvalue"}
```

### Exceptions (if any)

```
System.FormatException: Input string was not in a correct format. Failure to parse near offset 1. Expected an ASCII digit.
```

### Aspire doctor output

_No response_

### Anything else?

Workaround:

```
rb.Append($$$"""{{"myjson": "{{{variable}}}"}}""");
```

Contributor guide

Open the contributing guide

Research direction

Start with ReferenceExpressionBuilder.Append and Build, then compare their handling with ReferenceExpression.Create and ExpressionInterpolatedStringHandler, as described in PR #8994. Reproduce the JSON example from the issue and inspect the existing tests around these entry points. Done means raw JSON with single braces no longer causes FormatException and the resulting expression preserves the input.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.