Skip to content

Commit e3c8704

Browse files
Fix double spacing in ClientUriBuilder generated code (#8971)
`BinaryOperatorExpression.Write()` adds spaces around operators during code generation. `ClientUriBuilderDefinition` was passing operators with embedded spaces (`" - "`, `" + "`, `" ??= "`), causing double spacing in generated output. **Changes:** - Remove spaces from operator strings in `ClientUriBuilderDefinition.cs` to match codebase convention **Before:** ```csharp private UriBuilder UriBuilder => _uriBuilder ??= new UriBuilder(); if (_pathLength > 0 && PathAndQuery[_pathLength - 1] == '/') ``` **After:** ```csharp private UriBuilder UriBuilder => _uriBuilder ??= new UriBuilder(); if (_pathLength > 0 && PathAndQuery[_pathLength - 1] == '/') ``` <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Inconsistent spacing in ClientUriBuilder</issue_title> > <issue_description>https://github.com/Azure/azure-sdk-for-net/pull/53776#discussion_r2505733548</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> - Fixes #8959 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: JoshLove-msft <[email protected]>
1 parent b637347 commit e3c8704

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientUriBuilderDefinition.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal sealed class ClientUriBuilderDefinition : TypeProvider
3333
modifiers: MethodSignatureModifiers.Private,
3434
name: "UriBuilder",
3535
type: typeof(UriBuilder),
36-
body: new ExpressionPropertyBody(new BinaryOperatorExpression(" ??= ", _uriBuilderField, New.Instance(typeof(UriBuilder)))),
36+
body: new ExpressionPropertyBody(new BinaryOperatorExpression("??=", _uriBuilderField, New.Instance(typeof(UriBuilder)))),
3737
description: null,
3838
enclosingType: this);
3939

@@ -51,7 +51,7 @@ internal sealed class ClientUriBuilderDefinition : TypeProvider
5151
modifiers: MethodSignatureModifiers.Private,
5252
name: "PathAndQuery",
5353
type: typeof(StringBuilder),
54-
body: new ExpressionPropertyBody(new BinaryOperatorExpression(" ??= ", _pathAndQueryField, New.Instance(typeof(StringBuilder)))),
54+
body: new ExpressionPropertyBody(new BinaryOperatorExpression("??=", _pathAndQueryField, New.Instance(typeof(StringBuilder)))),
5555
description: null,
5656
enclosingType: this);
5757

@@ -153,14 +153,14 @@ private MethodProvider[] BuildAppendPathMethods()
153153
},
154154
MethodBodyStatement.Empty,
155155
// Check for double slashes: if path ends with '/' and value starts with '/'
156-
new IfStatement(pathLength.GreaterThan(Int(0)).And(stringBuilder.Index(new BinaryOperatorExpression(" - ", pathLength, Int(1))).Equal(Literal('/'))).And(valueParameter.As<string>().Index(Int(0)).Equal(Literal('/'))))
156+
new IfStatement(pathLength.GreaterThan(Int(0)).And(stringBuilder.Index(new BinaryOperatorExpression("-", pathLength, Int(1))).Equal(Literal('/'))).And(valueParameter.As<string>().Index(Int(0)).Equal(Literal('/'))))
157157
{
158-
stringBuilder.Remove(new BinaryOperatorExpression(" - ", pathLength, Int(1)), Int(1)).Terminate(),
159-
_pathLengthField.Assign(new BinaryOperatorExpression(" - ", pathLength, Int(1))).Terminate()
158+
stringBuilder.Remove(new BinaryOperatorExpression("-", pathLength, Int(1)), Int(1)).Terminate(),
159+
_pathLengthField.Assign(new BinaryOperatorExpression("-", pathLength, Int(1))).Terminate()
160160
},
161161
MethodBodyStatement.Empty,
162162
stringBuilder.Invoke("Insert", [pathLength, valueParameter]).Terminate(),
163-
_pathLengthField.Assign(new BinaryOperatorExpression(" + ", pathLength, valueParameter.As<string>().Length())).Terminate()
163+
_pathLengthField.Assign(new BinaryOperatorExpression("+", pathLength, valueParameter.As<string>().Length())).Terminate()
164164
};
165165

166166
return
@@ -222,7 +222,7 @@ private MethodProvider[] BuildAppendQueryMethods()
222222
{
223223
stringBuilder.Append(Literal('?')).Terminate()
224224
},
225-
new IfStatement(stringBuilder.Length().GreaterThan(pathLength).And(stringBuilder.Index(new BinaryOperatorExpression(" - ", stringBuilder.Length(), Int(1))).NotEqual(Literal('?'))))
225+
new IfStatement(stringBuilder.Length().GreaterThan(pathLength).And(stringBuilder.Index(new BinaryOperatorExpression("-", stringBuilder.Length(), Int(1))).NotEqual(Literal('?'))))
226226
{
227227
stringBuilder.Append(Literal('&')).Terminate()
228228
},
@@ -353,7 +353,7 @@ private MethodProvider BuildToUriMethod()
353353
// Set the query portion if it exists
354354
new IfStatement(stringBuilder.Length().GreaterThan(pathLength))
355355
{
356-
UriBuilderQuery.Assign(stringBuilder.Invoke("ToString", [new BinaryOperatorExpression(" + ", pathLength, Int(1)), new BinaryOperatorExpression(" - ", new BinaryOperatorExpression(" - ", stringBuilder.Length(), pathLength), Int(1))])).Terminate()
356+
UriBuilderQuery.Assign(stringBuilder.Invoke("ToString", [new BinaryOperatorExpression("+", pathLength, Int(1)), new BinaryOperatorExpression("-", new BinaryOperatorExpression("-", stringBuilder.Length(), pathLength), Int(1))])).Terminate()
357357
},
358358
new IfStatement(stringBuilder.Length().Equal(pathLength))
359359
{

packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Internal/ClientUriBuilder.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public ClientUriBuilder()
2222
{
2323
}
2424

25-
private UriBuilder UriBuilder => _uriBuilder ??= new UriBuilder();
25+
private UriBuilder UriBuilder => _uriBuilder ??= new UriBuilder();
2626

27-
private StringBuilder PathAndQuery => _pathAndQuery ??= new StringBuilder();
27+
private StringBuilder PathAndQuery => _pathAndQuery ??= new StringBuilder();
2828

2929
public void Reset(Uri uri)
3030
{
@@ -40,13 +40,13 @@ public void AppendPath(string value, bool escape)
4040
{
4141
value = Uri.EscapeDataString(value);
4242
}
43-
if (_pathLength > 0 && PathAndQuery[_pathLength - 1] == '/' && value[0] == '/')
43+
if (_pathLength > 0 && PathAndQuery[_pathLength - 1] == '/' && value[0] == '/')
4444
{
45-
PathAndQuery.Remove(_pathLength - 1, 1);
46-
_pathLength = _pathLength - 1;
45+
PathAndQuery.Remove(_pathLength - 1, 1);
46+
_pathLength = _pathLength - 1;
4747
}
4848
PathAndQuery.Insert(_pathLength, value);
49-
_pathLength = _pathLength + value.Length;
49+
_pathLength = _pathLength + value.Length;
5050
}
5151

5252
public void AppendPath(bool value, bool escape = false) => AppendPath(TypeFormatters.ConvertToString(value), escape);
@@ -80,7 +80,7 @@ public void AppendQuery(string name, string value, bool escape)
8080
{
8181
PathAndQuery.Append('?');
8282
}
83-
if (PathAndQuery.Length > _pathLength && PathAndQuery[PathAndQuery.Length - 1] != '?')
83+
if (PathAndQuery.Length > _pathLength && PathAndQuery[PathAndQuery.Length - 1] != '?')
8484
{
8585
PathAndQuery.Append('&');
8686
}
@@ -127,7 +127,7 @@ public Uri ToUri()
127127
UriBuilder.Path = PathAndQuery.ToString(0, _pathLength);
128128
if (PathAndQuery.Length > _pathLength)
129129
{
130-
UriBuilder.Query = PathAndQuery.ToString(_pathLength + 1, PathAndQuery.Length - _pathLength - 1);
130+
UriBuilder.Query = PathAndQuery.ToString(_pathLength + 1, PathAndQuery.Length - _pathLength - 1);
131131
}
132132
if (PathAndQuery.Length == _pathLength)
133133
{

0 commit comments

Comments
 (0)