Skip to content

Commit

Permalink
- additional attempt at fixing names escaping logic without leading t…
Browse files Browse the repository at this point in the history
…o collisions

Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Nov 30, 2023
1 parent d826629 commit cfa1cd7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public static partial class OpenApiUrlTreeNodeExtensions
private static readonly Func<string, string> replaceSingleParameterSegmentByItem =
static x => x.IsPathSegmentWithSingleSimpleParameter() ? "item" : x;
private static readonly char[] namespaceNameSplitCharacters = ['.', '-', '$']; //$ref from OData
private const string EscapedSuffix = "Escaped";
internal static string GetNamespaceFromPath(this string currentPath, string prefix) =>
prefix +
((currentPath?.Contains(PathNameSeparator, StringComparison.OrdinalIgnoreCase) ?? false) ?
(string.IsNullOrEmpty(prefix) ? string.Empty : ".")
+ currentPath
.Split(PathNameSeparator, StringSplitOptions.RemoveEmptyEntries)
.Select(replaceSingleParameterSegmentByItem)
.Select(static x => SegmentsToSkipForClassNames.Contains(x) ? $"{x}Escaped" : x)
.Select(static x => string.Join(string.Empty, x
.Split(namespaceNameSplitCharacters, StringSplitOptions.RemoveEmptyEntries)
.Except(SegmentsToSkipForClassNames, StringComparer.OrdinalIgnoreCase)
.Select(CleanupParametersFromPath)
.Select(static (y, idx) => idx == 0 ? y : y.ToFirstCharacterUpperCase())))
.Select(static x => SegmentsToSkipForClassNames.Contains(x) ? $"{x}{EscapedSuffix}" : x)
.Select(static x => x.CleanupSymbolName())
.Select(static x => GenerationConfiguration.ModelsNamespaceSegmentName.Equals(x, StringComparison.OrdinalIgnoreCase) ? $"{x}Requests" : x) //avoids projecting requests builders to models namespace
.Aggregate(string.Empty,
Expand Down Expand Up @@ -113,7 +113,7 @@ private static string GetSegmentName(this OpenApiUrlTreeNode currentNode, Struct
CleanupParametersFromPath(currentNode.Segment)?.ReplaceValueIdentifier()));
if (!string.IsNullOrEmpty(rawClassName) && string.IsNullOrEmpty(referenceName))
{
if (stripExtensionForIndexersRegex().IsMatch(rawClassName))
if (stripExtensionForIndexersTestRegex().IsMatch(rawClassName)) // {id}.json is considered as indexer
rawClassName = stripExtensionForIndexersRegex().Replace(rawClassName, string.Empty);
if ((currentNode?.DoesNodeBelongToItemSubnamespace() ?? false) && idClassNameCleanup().Replace(rawClassName, string.Empty) is string cleanedUpClassName && !cleanedUpClassName.Equals(rawClassName, StringComparison.Ordinal))
{
Expand Down Expand Up @@ -177,6 +177,8 @@ private static bool IsPathSegmentWithNumberOfParameters(this string currentSegme
}
[GeneratedRegex(@"\.(?:json|yaml|yml|csv|txt)$", RegexOptions.Singleline, 500)]
private static partial Regex stripExtensionForIndexersRegex(); // so {param-name}.json is considered as indexer
[GeneratedRegex(@"\{\w+\}\.(?:json|yaml|yml|csv|txt)$", RegexOptions.Singleline, 500)]
private static partial Regex stripExtensionForIndexersTestRegex(); // so {param-name}.json is considered as indexer
public static bool IsComplexPathMultipleParameters(this OpenApiUrlTreeNode currentNode) =>
(currentNode?.Segment?.IsPathSegmentWithNumberOfParameters(static x => x.Any()) ?? false) && !currentNode.IsPathSegmentWithSingleSimpleParameter();
public static string GetUrlTemplate(this OpenApiUrlTreeNode currentNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public void GetUrlTemplateCleansInvalidParameters()
Assert.Equal("{+baseurl}/{param%2Dwith%2Ddashes}/existing-segment{?%24select,api%2Dversion,api%7Etopic,api%2Eencoding}", node.Children.First().Value.GetUrlTemplate());
// the query parameters will be decoded by a middleware at runtime before the request is executed
}
[InlineData("\\reviews\\search.json", "reviews.search")]
[InlineData("\\reviews\\search.json", "reviews.searchJson")]
[InlineData("\\members\\microsoft.graph.$ref", "members.microsoftGraphRef")]
[InlineData("\\feeds\\video-comments.{format}", "feeds.videoCommentsWithFormat")]
[Theory]
Expand Down

0 comments on commit cfa1cd7

Please sign in to comment.