microsoft / kiota-typescript

TypeScript libraries for Kiota-generated API clients.
https://aka.ms/kiota/docs
MIT License
32 stars 25 forks source link

Duration format bug - Class tried to be generated #1128

Closed gadcam closed 3 months ago

gadcam commented 4 months ago

Duration being a class (plus probably some other factor I do not understand) https://github.com/microsoft/kiota-typescript/blob/dfbafb0aaf1afdb49e920c2c0c77c15766fce135/packages/abstractions/src/duration.ts#L8

It triggers this exception https://github.com/microsoft/kiota/blob/c78914ba8b21d33aecba9539b9584edbf4b4fec8/src/Kiota.Builder/Writers/TypeScript/CodePropertyWriter.cs#L26

The following patch to Kiota enables to finish the generation but the resulting code is not satisfactory (I had to edit it to make it build). It was more a confirmation of the code path that led to the issue.

diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs
--- a/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs
+++ b/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs
@@ -22,14 +24,13 @@ public class CodeMethodWriter : BaseElementWriter<CodeMethod, TypeScriptConventi
         var isVoid = "void".EqualsIgnoreCase(returnType);
         WriteMethodDocumentation(codeElement, writer, isVoid);
         WriteMethodPrototype(codeElement, writer, returnType, isVoid);
-        if (codeElement.Parent is CodeClass)
-            throw new InvalidOperationException("No method implementations are generated in typescript: either functions or constants.");
     }

     private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer, bool isVoid)
     {
         WriteMethodDocumentationInternal(code, writer, isVoid, conventions);
     }
diff --git a/src/Kiota.Builder/Writers/TypeScript/CodePropertyWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodePropertyWriter.cs
--- a/src/Kiota.Builder/Writers/TypeScript/CodePropertyWriter.cs
+++ b/src/Kiota.Builder/Writers/TypeScript/CodePropertyWriter.cs
@@ -17,22 +21,17 @@ public class CodePropertyWriter : BaseElementWriter<CodeProperty, TypeScriptConv
                          && !codeElement.Type.IsCollection;//collection of flagged enums are not supported/don't make sense

         conventions.WriteLongDescription(codeElement, writer);
-        switch (codeElement.Parent)
-        {
-            case CodeInterface:
-                WriteCodePropertyForInterface(codeElement, writer, returnType, isFlagEnum);
-                break;
-            case CodeClass:
-                throw new InvalidOperationException($"All properties are defined on interfaces in TypeScript.");
-        }
+        WriteCodeProperty(codeElement, writer, returnType, isFlagEnum);
     }
-    private static void WriteCodePropertyForInterface(CodeProperty codeElement, LanguageWriter writer, string returnType, bool isFlagEnum)
+
+    private static void WriteCodeProperty(CodeProperty codeElement, LanguageWriter writer, string returnType, bool isFlagEnum)
     {
koros commented 3 months ago

Hi @gadcam,

Thanks for reporting the issue, do you have a sample openapi yml snippet you were using so that I can use it to debug the issue. Thanks.

microsoft-github-policy-service[bot] commented 3 months ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.