Skip to content
This repository was archived by the owner on Jun 16, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright(c) Microsoft Corporation.All rights reserved.
// Licensed under the MIT License.

using System;
using AutoRest.CSharp.Generation.Writers;

namespace AutoRest.CSharp.Common.Output.Expressions.ValueExpressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using AutoRest.CSharp.Common.Output.Expressions.ValueExpressions;
using AutoRest.CSharp.Common.Output.Models;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Output.Models.Serialization;
using AutoRest.CSharp.Output.Models.Shared;
using static AutoRest.CSharp.Common.Output.Models.Snippets;

Expand All @@ -32,7 +31,10 @@ private ClientUriBuilderProvider() : base(Configuration.HelperNamespace, null)
private readonly FieldDeclaration _uriBuilderField = new(FieldModifiers.Private, typeof(UriBuilder), "_uriBuilder");
private readonly FieldDeclaration _pathBuilderField = new(FieldModifiers.Private, typeof(StringBuilder), "_pathBuilder");
private readonly FieldDeclaration _queryBuilderField = new(FieldModifiers.Private, typeof(StringBuilder), "_queryBuilder");
private readonly FieldDeclaration _pathSeparatorField = new(FieldModifiers.Private | FieldModifiers.Const, typeof(char), "PathSeparator", Literal('/'), SerializationFormat.Default);
private readonly FieldDeclaration _pathSeparatorField = new(FieldModifiers.Private | FieldModifiers.Const, typeof(char), "PathSeparator")
{
InitializationValue = Literal('/')
};

protected override IEnumerable<FieldDeclaration> BuildFields()
{
Expand Down Expand Up @@ -147,6 +149,7 @@ private IEnumerable<Method> BuildAppendPathMethods()
var value = new StringExpression(valueParameter);
var escape = new BoolExpression(escapeParameter);
var pathBuilder = new StringBuilderExpression(PathBuilderProperty);
var pathSeparator = new CharExpression(_pathSeparatorField);
MethodBodyStatement body = new MethodBodyStatement[]
{
Argument.AssertNotNullOrWhiteSpace(value),
Expand All @@ -156,7 +159,7 @@ private IEnumerable<Method> BuildAppendPathMethods()
Assign(value, new InvokeStaticMethodExpression(typeof(Uri), nameof(Uri.EscapeDataString), new[]{ value }))
},
EmptyLine,
new IfStatement(Equal(new IndexerExpression(value, Literal(0)), _pathSeparatorField))
new IfStatement(And(Equal(new IndexerExpression(value, Literal(0)), _pathSeparatorField), And(GreaterThan(pathBuilder.Length, Int(0)), Equal(new IndexerExpression(pathBuilder, new BinaryOperatorExpression("-", pathBuilder.Length, Int(1))), _pathSeparatorField))))
{
Assign(value, value.Substring(Literal(1)))
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void AppendPath(string value, bool escape)
value = Uri.EscapeDataString(value);
}

if (value[0] == PathSeparator)
if (value[0] == PathSeparator && PathBuilder.Length > 0 && PathBuilder[PathBuilder.Length - 1] == PathSeparator)
{
value = value.Substring(1);
}
Expand Down
66 changes: 66 additions & 0 deletions test/UnbrandedProjects.Tests/ClientUriBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using NUnit.Framework;
using UnbrandedTypeSpec;

namespace UnbrandedProjects.Tests
{
public class ClientUriBuilderTests
{
[TestCaseSource(nameof(AppendPathSource))]
public void ValidateAppendPath_ToUri(Uri expected, Uri endpoint, params string[] paths)
Comment thread
ArcturusZhang marked this conversation as resolved.
Outdated
{
var uriBuilder = new ClientUriBuilder();
uriBuilder.Reset(endpoint);
foreach (var path in paths)
{
uriBuilder.AppendPath(path, false);
}

var uri = uriBuilder.ToUri();
Assert.AreEqual(expected, uri);
}

private static readonly object[] AppendPathSource =
{
new object[]
{
new Uri("https://api.openai.com/v1"), new Uri("https://api.openai.com/v1"), Array.Empty<string>()
},
new object[]
{
new Uri("https://api.openai.com/v1/"), new Uri("https://api.openai.com/v1/"), Array.Empty<string>()
},
new object[]
{
new Uri("https://api.openai.com/v1/doSomething"), new Uri("https://api.openai.com/v1"), new[] { "/doSomething" }
},
new object[]
{
new Uri("https://api.openai.com/v1/doSomething"), new Uri("https://api.openai.com/v1/"), new[] { "/doSomething" }
},
new object[]
{
new Uri("https://api.openai.com/v1/doSomething/"), new Uri("https://api.openai.com/v1"), new[] { "/doSomething/" }
},
new object[]
{
new Uri("https://api.openai.com/v1/doSomething/"), new Uri("https://api.openai.com/v1/"), new[] { "/doSomething/" }
},
new object[]
{
new Uri("https://api.openai.com/v1/doSomething/else"), new Uri("https://api.openai.com/v1"), new[] { "/doSomething", "/else" }
},
new object[]
{
new Uri("https://api.openai.com/v1/doSomething/else"), new Uri("https://api.openai.com/v1/"), new[] { "/doSomething", "/else" }
},
new object[]
{
new Uri("https://api.openai.com/v1/doSomething/else/"), new Uri("https://api.openai.com/v1/"), new[] { "/doSomething/", "/else/" }
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void AppendPath(string value, bool escape)
value = Uri.EscapeDataString(value);
}

if (value[0] == PathSeparator)
if (value[0] == PathSeparator && PathBuilder.Length > 0 && PathBuilder[PathBuilder.Length - 1] == PathSeparator)
{
value = value.Substring(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void AppendPath(string value, bool escape)
value = Uri.EscapeDataString(value);
}

if (value[0] == PathSeparator)
if (value[0] == PathSeparator && PathBuilder.Length > 0 && PathBuilder[PathBuilder.Length - 1] == PathSeparator)
Comment thread
ArcturusZhang marked this conversation as resolved.
Outdated
{
value = value.Substring(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void AppendPath(string value, bool escape)
value = Uri.EscapeDataString(value);
}

if (value[0] == PathSeparator)
if (value[0] == PathSeparator && PathBuilder.Length > 0 && PathBuilder[PathBuilder.Length - 1] == PathSeparator)
{
value = value.Substring(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void AppendPath(string value, bool escape)
value = Uri.EscapeDataString(value);
}

if (value[0] == PathSeparator)
if (value[0] == PathSeparator && PathBuilder.Length > 0 && PathBuilder[PathBuilder.Length - 1] == PathSeparator)
{
value = value.Substring(1);
}
Expand Down