Skip to content

Commit 1736364

Browse files
committed
- fixes flaky behavior for indexer replacement
1 parent afcb4ad commit 1736364

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/Kiota.Builder/CodeDOM/CodeMethod.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using Kiota.Builder.Extensions;
56

67
namespace Kiota.Builder.CodeDOM;
78

@@ -78,7 +79,7 @@ public object Clone()
7879
public class CodeMethod : CodeTerminalWithKind<CodeMethodKind>, ICloneable, IDocumentedElement, IDeprecableElement
7980
{
8081
public static readonly CodeParameterKind ParameterKindForConvertedIndexers = CodeParameterKind.Custom;
81-
public static CodeMethod FromIndexer(CodeIndexer originalIndexer, Func<string, string> methodNameCallback, Func<string, string> parameterNameCallback, bool parameterNullable)
82+
public static CodeMethod FromIndexer(CodeIndexer originalIndexer, Func<string, string> methodNameCallback, Func<string, string> parameterNameCallback, bool parameterNullable, bool typeSpecificOverload = false)
8283
{
8384
ArgumentNullException.ThrowIfNull(originalIndexer);
8485
ArgumentNullException.ThrowIfNull(methodNameCallback);
@@ -89,7 +90,7 @@ public static CodeMethod FromIndexer(CodeIndexer originalIndexer, Func<string, s
8990
IsStatic = false,
9091
Access = AccessModifier.Public,
9192
Kind = CodeMethodKind.IndexerBackwardCompatibility,
92-
Name = methodNameCallback(originalIndexer.IndexParameterName),
93+
Name = methodNameCallback(originalIndexer.IndexParameterName) + (typeSpecificOverload ? originalIndexer.IndexType.Name.ToFirstCharacterUpperCase() : string.Empty),
9394
Documentation = new()
9495
{
9596
Description = originalIndexer.Documentation.Description,

src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,9 @@ protected static void ReplaceIndexersByMethodsWithParameter(CodeElement currentE
695695
if (currentElement is CodeIndexer currentIndexer &&
696696
currentElement.Parent is CodeClass indexerParentClass)
697697
{
698-
indexerParentClass.RemoveChildElement(currentElement);
699-
//TODO remove for v2
698+
if (indexerParentClass.ContainsMember(currentElement.Name)) // TODO remove condition for v2 necessary because of the second case of Go block
699+
indexerParentClass.RemoveChildElement(currentElement);
700+
//TODO remove who block except for last else if body for v2
700701
var isIndexerStringBackwardCompatible = "string".Equals(currentIndexer.IndexType.Name, StringComparison.OrdinalIgnoreCase) &&
701702
currentIndexer.Deprecation is not null && currentIndexer.Deprecation.IsDeprecated &&
702703
(indexerParentClass.Methods.Any(x => x.IsOfKind(CodeMethodKind.IndexerBackwardCompatibility) && x.OriginalIndexer is not null && IsIndexerTypeSpecificVersion(currentIndexer.IndexParameterName, x.OriginalIndexer)) ||
@@ -708,6 +709,12 @@ currentIndexer.Deprecation is not null && currentIndexer.Deprecation.IsDeprecate
708709
{
709710
indexerParentClass.RenameChildElement(typeSpecificCompatibleMethod.Name, typeSpecificCompatibleMethod.Name + typeSpecificCompatibleMethod.OriginalIndexer.IndexType.Name.ToFirstCharacterUpperCase());
710711
}
712+
else if (indexerParentClass.Indexer != null && indexerParentClass.Indexer != currentIndexer && IsIndexerTypeSpecificVersion(currentIndexer.IndexParameterName, indexerParentClass.Indexer))
713+
{
714+
var specificIndexer = indexerParentClass.Indexer;
715+
indexerParentClass.RemoveChildElement(specificIndexer);
716+
indexerParentClass.AddMethod(CodeMethod.FromIndexer(specificIndexer, methodNameCallback, parameterNameCallback, parameterNullable, true));
717+
}
711718
indexerParentClass.AddMethod(CodeMethod.FromIndexer(currentIndexer, methodNameCallback, parameterNameCallback, parameterNullable));
712719
}
713720
else if (!isIndexerStringBackwardCompatible)

0 commit comments

Comments
 (0)