From 70bc8f9436dfbfb0718c8c8b203d5a3efd5b92da Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 23 Aug 2023 09:17:36 -0400 Subject: [PATCH] - fixes backward compatible indexer method for Go --- src/Kiota.Builder/CodeDOM/CodeClass.cs | 2 +- .../Refiners/CommonLanguageRefiner.cs | 26 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Kiota.Builder/CodeDOM/CodeClass.cs b/src/Kiota.Builder/CodeDOM/CodeClass.cs index 2b4fcdbe7b..7a01892305 100644 --- a/src/Kiota.Builder/CodeDOM/CodeClass.cs +++ b/src/Kiota.Builder/CodeDOM/CodeClass.cs @@ -45,7 +45,7 @@ public CodeComposedTypeBase? OriginalComposedType { get; set; } - public CodeIndexer? Indexer => InnerChildElements.Values.OfType().FirstOrDefault(static x => !x.Deprecation?.IsDeprecated ?? true); + public CodeIndexer? Indexer => InnerChildElements.Values.OfType().FirstOrDefault(static x => !x.IsLegacyIndexer); public void AddIndexer(params CodeIndexer[] indexers) { if (indexers == null || Array.Exists(indexers, static x => x == null)) diff --git a/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs b/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs index debe688d59..7e708a073c 100644 --- a/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs +++ b/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs @@ -603,21 +603,27 @@ protected static void ReplaceIndexersByMethodsWithParameter(CodeElement currentE { if (indexerParentClass.ContainsMember(currentElement.Name)) // TODO remove condition for v2 necessary because of the second case of Go block indexerParentClass.RemoveChildElement(currentElement); - //TODO remove who block except for last else if body for v2 - if (currentIndexer.IsLegacyIndexer && language == GenerationLanguage.Go) + //TODO remove whole block except for last else if body for v2 + if (language == GenerationLanguage.Go) { - if (indexerParentClass.Methods.FirstOrDefault(x => x.IsOfKind(CodeMethodKind.IndexerBackwardCompatibility) && x.OriginalIndexer is not null && !x.OriginalIndexer.IsLegacyIndexer) is CodeMethod typeSpecificCompatibleMethod && - typeSpecificCompatibleMethod.OriginalIndexer is not null) + if (currentIndexer.IsLegacyIndexer) { - indexerParentClass.RenameChildElement(typeSpecificCompatibleMethod.Name, typeSpecificCompatibleMethod.Name + typeSpecificCompatibleMethod.OriginalIndexer.IndexParameter.Type.Name.ToFirstCharacterUpperCase()); + if (indexerParentClass.Indexer is CodeIndexer specificIndexer && specificIndexer != currentIndexer && !specificIndexer.IsLegacyIndexer) + { + indexerParentClass.RemoveChildElement(specificIndexer); + indexerParentClass.AddMethod(CodeMethod.FromIndexer(specificIndexer, methodNameCallback, parameterNameCallback, parameterNullable, true)); + } + indexerParentClass.AddMethod(CodeMethod.FromIndexer(currentIndexer, methodNameCallback, parameterNameCallback, parameterNullable)); } - else if (indexerParentClass.Indexer != null && indexerParentClass.Indexer != currentIndexer && !indexerParentClass.Indexer.IsLegacyIndexer) + else { - var specificIndexer = indexerParentClass.Indexer; - indexerParentClass.RemoveChildElement(specificIndexer); - indexerParentClass.AddMethod(CodeMethod.FromIndexer(specificIndexer, methodNameCallback, parameterNameCallback, parameterNullable, true)); + if (indexerParentClass.GetChildElements(true).OfType().FirstOrDefault(static x => x.IsLegacyIndexer) is CodeIndexer legacyIndexer) + { + indexerParentClass.RemoveChildElement(legacyIndexer); + indexerParentClass.AddMethod(CodeMethod.FromIndexer(legacyIndexer, methodNameCallback, parameterNameCallback, parameterNullable)); + } + indexerParentClass.AddMethod(CodeMethod.FromIndexer(currentIndexer, methodNameCallback, parameterNameCallback, parameterNullable, true)); } - indexerParentClass.AddMethod(CodeMethod.FromIndexer(currentIndexer, methodNameCallback, parameterNameCallback, parameterNullable)); } else if (!currentIndexer.IsLegacyIndexer) indexerParentClass.AddMethod(CodeMethod.FromIndexer(currentIndexer, methodNameCallback, parameterNameCallback, parameterNullable));