From 2d5dcf7f06c7c8f412edb933f4f3098055978966 Mon Sep 17 00:00:00 2001
From: rkodev <43806892+rkodev@users.noreply.github.com>
Date: Wed, 27 Sep 2023 16:20:28 +0300
Subject: [PATCH] Exclude duplicate importS

---
 .../Writers/TypeScript/CodeFileDeclarationWriter.cs  | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeFileDeclarationWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeFileDeclarationWriter.cs
index 9debd754f4..4ed056a595 100644
--- a/src/Kiota.Builder/Writers/TypeScript/CodeFileDeclarationWriter.cs
+++ b/src/Kiota.Builder/Writers/TypeScript/CodeFileDeclarationWriter.cs
@@ -31,7 +31,17 @@ public override void WriteCodeElement(CodeFileDeclaration codeElement, LanguageW
                 }
             );
             conventions.WriteAutoGeneratedStart(writer);
-            _codeUsingWriter.WriteCodeElement(usings, ns, writer);
+
+            // remove duplicate using, keep a single using for each internal type in the same namespace
+            var enumeratedUsing = usings.ToArray();
+            var filteredUsing = enumeratedUsing.Where(x => x.IsExternal)
+                .Union(enumeratedUsing.ToArray()
+                    .Where(x => x is { IsExternal: false, Declaration.TypeDefinition: not null })
+                    .GroupBy(x =>
+                        $"{x.Declaration!.TypeDefinition!.GetImmediateParentOfType<CodeNamespace>().Name}.{x.Declaration?.Name.ToLowerInvariant()}")
+                    .Select(x => x.First()));
+
+            _codeUsingWriter.WriteCodeElement(filteredUsing, ns, writer);
         }
     }