From dd35897345d544c4bc77f602e560a810675d1e08 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 10:12:30 +0300 Subject: [PATCH 01/13] Fix function writer --- src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs index 1710944648..af0331846e 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs @@ -117,7 +117,7 @@ private void WriteComposedTypeSerializer(CodeFunction codeElement, LanguageWrite private void WriteSerializationFunctionForCodeIntersectionType(CodeComposedTypeBase composedType, CodeParameter composedParam, CodeFunction method, LanguageWriter writer) { - foreach (var mappedType in composedType.Types.Where(x => !IsPrimitiveType(x, composedType))) + foreach (var mappedType in composedType.Types.Where(x => !IsPrimitiveType(x, composedType) && x.TypeDefinition != null)) { var functionName = GetSerializerFunctionName(method, mappedType); var variableName = composedParam.Name.ToFirstCharacterLowerCase(); From 259a67ef5367619d70a5704ce00ca7f2437fb179 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:31:19 +0300 Subject: [PATCH 02/13] enable tests --- it/config.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/it/config.json b/it/config.json index dab857bc52..777f0a36fd 100644 --- a/it/config.json +++ b/it/config.json @@ -29,10 +29,6 @@ "apisguru::github.com:api.github.com": { "MockServerITFolder": "gh", "Suppressions": [ - { - "Language": "typescript", - "Rationale": "https://github.com/microsoft/kiota/issues/5634" - }, { "Language": "ruby", "Rationale": "https://github.com/microsoft/kiota/issues/1816" From 5af8923f5dc59f06cbda1797f44294e14954a3a0 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 12:09:43 +0300 Subject: [PATCH 03/13] fix documentation --- .../TypeScript/TypeScriptConventionService.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs index 90a6ac83e3..033869820f 100644 --- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs +++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs @@ -252,7 +252,20 @@ TYPE_LOWERCASE_BOOLEAN or private static bool IsPrimitiveTypeOrPrimitiveCollection(CodeType codeType, CodeComposedTypeBase codeComposedTypeBase) => IsPrimitiveType(codeType, codeComposedTypeBase, false); - internal static string RemoveInvalidDescriptionCharacters(string originalDescription) => originalDescription?.Replace("\\", "/", StringComparison.OrdinalIgnoreCase) ?? string.Empty; + private static Dictionary InvalidCharactersReplacements = new() { + { "\\", "/"}, + { "/*", "//*"} + }; + + internal static string RemoveInvalidDescriptionCharacters(string originalDescription) + { + if (string.IsNullOrEmpty(originalDescription)) return string.Empty; + foreach (var replacement in InvalidCharactersReplacements) + { + originalDescription = originalDescription.Replace(replacement.Key, replacement.Value, StringComparison.OrdinalIgnoreCase); + } + return originalDescription; + } public override bool WriteShortDescription(IDocumentedElement element, LanguageWriter writer, string prefix = "", string suffix = "") { ArgumentNullException.ThrowIfNull(writer); From 694c61061c4a795b2f4aa46903d7a2c22a07502b Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 12:12:38 +0300 Subject: [PATCH 04/13] ling re-qrite --- .../Refiners/TypeScriptRefiner.cs | 27 ++++++++++++------- .../TypeScript/TypeScriptConventionService.cs | 6 ++--- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs index 4b634c7d08..6e9288a894 100644 --- a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs +++ b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs @@ -1495,19 +1495,26 @@ private static void AddDeserializerUsingToDiscriminatorFactory(CodeElement codeE { if (mappedType.Value is CodeType type && type.TypeDefinition is CodeClass mappedClass) { - var deserializer = GetSerializationFunctionsForNamespace(mappedClass).Item2; - - if (deserializer.Parent is not null) + try { - parsableFactoryFunction.AddUsing(new CodeUsing + var deserializer = GetSerializationFunctionsForNamespace(mappedClass).Item2; + + if (deserializer.Parent is not null) { - Name = deserializer.Parent.Name, - Declaration = new CodeType + parsableFactoryFunction.AddUsing(new CodeUsing { - Name = deserializer.Name, - TypeDefinition = deserializer - }, - }); + Name = deserializer.Parent.Name, + Declaration = new CodeType + { + Name = deserializer.Name, + TypeDefinition = deserializer + }, + }); + } + } + catch (InvalidOperationException) + { + // The deserializer function for the mapped class does not exist. } } } diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs index 033869820f..6b4c8c0d02 100644 --- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs +++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs @@ -260,10 +260,8 @@ TYPE_LOWERCASE_BOOLEAN or internal static string RemoveInvalidDescriptionCharacters(string originalDescription) { if (string.IsNullOrEmpty(originalDescription)) return string.Empty; - foreach (var replacement in InvalidCharactersReplacements) - { - originalDescription = originalDescription.Replace(replacement.Key, replacement.Value, StringComparison.OrdinalIgnoreCase); - } + originalDescription = InvalidCharactersReplacements + .Aggregate(originalDescription, (current, replacement) => current.Replace(replacement.Key, replacement.Value, StringComparison.OrdinalIgnoreCase)); return originalDescription; } public override bool WriteShortDescription(IDocumentedElement element, LanguageWriter writer, string prefix = "", string suffix = "") From 329fc79ff0ef8319c80eaa83b451780d05f4449c Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:49:27 +0300 Subject: [PATCH 05/13] Fix default value for escaped enum options --- .../Writers/TypeScript/CodeFunctionWriter.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs index af0331846e..94f72affb6 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs @@ -611,9 +611,17 @@ private static string GetDefaultValueSuffix(CodeProperty otherProp) private static string GetDefaultValueLiteralForProperty(CodeProperty codeProperty) { if (string.IsNullOrEmpty(codeProperty.DefaultValue)) return string.Empty; - if (codeProperty.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition && enumDefinition.CodeEnumObject is not null) - return $"{enumDefinition.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{codeProperty.DefaultValue.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}"; - return codeProperty.DefaultValue; + if (codeProperty.Type is not CodeType propertyType || + propertyType.TypeDefinition is not CodeEnum enumDefinition || + enumDefinition.CodeEnumObject is null) + { + return codeProperty.DefaultValue; + } + + var codeEnumOption = enumDefinition.Options.First(x => + x.SymbolName.Equals(codeProperty.DefaultValue.Trim('"').CleanupSymbolName(), + StringComparison.OrdinalIgnoreCase)); + return $"{enumDefinition.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{codeEnumOption.Name.CleanupSymbolName().ToFirstCharacterUpperCase()}"; } private void WriteDefensiveStatements(CodeMethod codeElement, LanguageWriter writer) { From 3f4fb541ac9cc4ea651450b8199861794343f674 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 16:14:40 +0300 Subject: [PATCH 06/13] Fix enum default value --- src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs index 94f72affb6..e3feba7634 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs @@ -615,13 +615,13 @@ private static string GetDefaultValueLiteralForProperty(CodeProperty codePropert propertyType.TypeDefinition is not CodeEnum enumDefinition || enumDefinition.CodeEnumObject is null) { - return codeProperty.DefaultValue; + return codeProperty.DefaultValue.Trim('"').CleanupSymbolName(); } var codeEnumOption = enumDefinition.Options.First(x => x.SymbolName.Equals(codeProperty.DefaultValue.Trim('"').CleanupSymbolName(), StringComparison.OrdinalIgnoreCase)); - return $"{enumDefinition.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{codeEnumOption.Name.CleanupSymbolName().ToFirstCharacterUpperCase()}"; + return $"{enumDefinition.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{codeEnumOption.Name.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}"; } private void WriteDefensiveStatements(CodeMethod codeElement, LanguageWriter writer) { From 7aabcf59ad442d81812255e1e9c1546dab3b9057 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 17:39:49 +0300 Subject: [PATCH 07/13] fix --- .../Writers/TypeScript/CodeFunctionWriter.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs index e3feba7634..c21197ae14 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs @@ -611,17 +611,17 @@ private static string GetDefaultValueSuffix(CodeProperty otherProp) private static string GetDefaultValueLiteralForProperty(CodeProperty codeProperty) { if (string.IsNullOrEmpty(codeProperty.DefaultValue)) return string.Empty; - if (codeProperty.Type is not CodeType propertyType || - propertyType.TypeDefinition is not CodeEnum enumDefinition || - enumDefinition.CodeEnumObject is null) + if (codeProperty.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition && + enumDefinition.CodeEnumObject is not null) { - return codeProperty.DefaultValue.Trim('"').CleanupSymbolName(); + var codeEnumOption = enumDefinition.Options.First(x => + x.SymbolName.Equals(codeProperty.DefaultValue.Trim('"').CleanupSymbolName(), + StringComparison.OrdinalIgnoreCase)); + return $"{enumDefinition.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{codeEnumOption.Name.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}"; } - var codeEnumOption = enumDefinition.Options.First(x => - x.SymbolName.Equals(codeProperty.DefaultValue.Trim('"').CleanupSymbolName(), - StringComparison.OrdinalIgnoreCase)); - return $"{enumDefinition.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{codeEnumOption.Name.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}"; + // only string primitive should keep quotes + return codeProperty.Type.Name.Equals("string", StringComparison.Ordinal) ? codeProperty.DefaultValue : codeProperty.DefaultValue.Trim('"'); } private void WriteDefensiveStatements(CodeMethod codeElement, LanguageWriter writer) { From df2eb3e938c22cc99cce1d0ac6b2953cd930408f Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 18:00:01 +0300 Subject: [PATCH 08/13] fix signature for composed types of collections --- .../Writers/TypeScript/TypeScriptConventionService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs index 6b4c8c0d02..0a6d3d9836 100644 --- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs +++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs @@ -99,7 +99,7 @@ public override string GetParameterSignature(CodeParameter parameter, CodeElemen // add a 'Parsable' type to the parameter if it is composed of non-Parsable types var parsableTypes = ( - composedType != null && !composedType.IsComposedOfObjects(IsPrimitiveType), + composedType != null && (!composedType.IsComposedOfObjects(IsPrimitiveType) || composedType.Types.Any(x => x.IsCollection)), parameter.Parent is CodeMethod method && (method.IsOfKind(CodeMethodKind.Deserializer, CodeMethodKind.Serializer)) ) switch { From 1fae65bebeeddd1b8a48de2206189803a62a5a2b Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 18:22:20 +0300 Subject: [PATCH 09/13] fix missing data types --- .../Writers/TypeScript/TypeScriptConventionService.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs index 0a6d3d9836..d309c1325a 100644 --- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs +++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs @@ -232,7 +232,10 @@ TYPE_NUMBER or TYPE_LOWERCASE_STRING or TYPE_BYTE_ARRAY or TYPE_LOWERCASE_BOOLEAN or - TYPE_LOWERCASE_VOID => true, + TYPE_LOWERCASE_VOID or + TYPE_DATE_ONLY or + TYPE_TIME_ONLY or + TYPE_DURATION => true, _ => false, }; } From 87f97efe9a4b64fc77b12de714ee6e88f6958945 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 18:38:50 +0300 Subject: [PATCH 10/13] fix test --- .../Writers/TypeScript/CodeConstantWriter.cs | 2 +- .../TypeScript/TypeScriptConventionService.cs | 11 ++++++++++- .../TypeScript/CodeFunctionWriterTests.cs | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs index b75590008a..0abb0fda27 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs @@ -102,7 +102,7 @@ private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWri var isStream = conventions.StreamTypeName.Equals(returnType, StringComparison.OrdinalIgnoreCase); var isEnum = executorMethod.ReturnType is CodeType codeType && codeType.TypeDefinition is CodeEnum; var returnTypeWithoutCollectionSymbol = GetReturnTypeWithoutCollectionSymbol(executorMethod, returnType); - var isPrimitive = IsPrimitiveType(returnTypeWithoutCollectionSymbol); + var isPrimitive = IsPrimitiveType(returnTypeWithoutCollectionSymbol) || IsKiotaPrimitive(returnTypeWithoutCollectionSymbol); var isPrimitiveAlias = GetPrimitiveAlias(returnTypeWithoutCollectionSymbol) is not null; writer.StartBlock($"{executorMethod.Name.ToFirstCharacterLowerCase()}: {{"); var urlTemplateValue = executorMethod.HasUrlTemplateOverride ? $"\"{executorMethod.UrlTemplateOverride}\"" : uriTemplateConstant.Name.ToFirstCharacterUpperCase(); diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs index d309c1325a..3e5ad787e7 100644 --- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs +++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs @@ -232,7 +232,16 @@ TYPE_NUMBER or TYPE_LOWERCASE_STRING or TYPE_BYTE_ARRAY or TYPE_LOWERCASE_BOOLEAN or - TYPE_LOWERCASE_VOID or + TYPE_LOWERCASE_VOID => true, + _ => false, + }; + } + + // Types that are imported from kiota-abstractions and considered as primitive types + public static bool IsKiotaPrimitive(string typeName) + { + return typeName switch + { TYPE_DATE_ONLY or TYPE_TIME_ONLY or TYPE_DURATION => true, diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs index 6eee533f97..79025ef0fa 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs @@ -1116,6 +1116,23 @@ public async Task WritesConstructorWithEnumValueAsync() { Name = "pictureSize" }).First(); + + codeEnum.AddOption( + new CodeEnumOption + { + Name = "256x256", + SerializationName = "256x256" + }, + new CodeEnumOption + { + Name = "512x512", + SerializationName = "512x512" + }, + new CodeEnumOption + { + Name = "1024x1024", + SerializationName = "1024x1024" + }); parentClass.AddProperty(new CodeProperty { Name = propName, From a961a6bfb906b90c81ea8e1a87a41bd180fd20a4 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 19:15:20 +0300 Subject: [PATCH 11/13] Fix --- .../Refiners/TypeScriptRefiner.cs | 30 ++++++++----------- .../Writers/TypeScript/CodeConstantWriter.cs | 2 +- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs index 6e9288a894..85f22e79e0 100644 --- a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs +++ b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs @@ -1493,28 +1493,22 @@ private static void AddDeserializerUsingToDiscriminatorFactory(CodeElement codeE foreach (var mappedType in parsableFactoryFunction.OriginalMethodParentClass.DiscriminatorInformation.DiscriminatorMappings) { - if (mappedType.Value is CodeType type && type.TypeDefinition is CodeClass mappedClass) + // check if the model was trimmed + if (mappedType.Value is CodeType type && type.TypeDefinition is CodeClass mappedClass && mappedClass.Parent is CodeNamespace codeNamespace && codeNamespace.FindChildByName(mappedClass.Name) != null) { - try - { - var deserializer = GetSerializationFunctionsForNamespace(mappedClass).Item2; + var deserializer = GetSerializationFunctionsForNamespace(mappedClass).Item2; - if (deserializer.Parent is not null) + if (deserializer.Parent is not null) + { + parsableFactoryFunction.AddUsing(new CodeUsing { - parsableFactoryFunction.AddUsing(new CodeUsing + Name = deserializer.Parent.Name, + Declaration = new CodeType { - Name = deserializer.Parent.Name, - Declaration = new CodeType - { - Name = deserializer.Name, - TypeDefinition = deserializer - }, - }); - } - } - catch (InvalidOperationException) - { - // The deserializer function for the mapped class does not exist. + Name = deserializer.Name, + TypeDefinition = deserializer + }, + }); } } } diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs index 0abb0fda27..f9f41d41a8 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs @@ -169,7 +169,7 @@ private string GetTypeFactory(bool isVoid, bool isStream, CodeMethod codeElement { if (isVoid) return string.Empty; var typeName = conventions.TranslateType(codeElement.ReturnType); - if (isStream || IsPrimitiveType(typeName)) return $" \"{typeName}\""; + if (isStream || IsPrimitiveType(typeName) || IsKiotaPrimitive(typeName)) return $" \"{typeName}\""; if (GetPrimitiveAlias(typeName) is { } alias && !string.IsNullOrEmpty(alias)) return $" \"{alias}\""; return $" {GetFactoryMethodName(codeElement.ReturnType, codeElement, writer)}"; From 3f6b7af0b945d93ece86a6c7f430a27e210d67f8 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 19:46:52 +0300 Subject: [PATCH 12/13] Adds readme --- CHANGELOG.md | 1 + .../Refiners/TypeScriptRefiner.cs | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7649b49a9c..8524fead23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed Python error when a class inherits from a base class and implements an interface. [5637](https://github.com/microsoft/kiota/issues/5637) - Fix anyOf/oneOf generation in TypeScript. [5353](https://github.com/microsoft/kiota/issues/5353) - Fixed invalid code in Php caused by "*/*/" in property description. [5635](https://github.com/microsoft/kiota/issues/5635) +- Fixed TypeScript generation error when generating usings from shaken serializers. [#5634](https://github.com/microsoft/kiota/issues/5634) ## [1.20.0] - 2024-11-07 diff --git a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs index 85f22e79e0..3ca1fbd068 100644 --- a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs +++ b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs @@ -1493,23 +1493,26 @@ private static void AddDeserializerUsingToDiscriminatorFactory(CodeElement codeE foreach (var mappedType in parsableFactoryFunction.OriginalMethodParentClass.DiscriminatorInformation.DiscriminatorMappings) { - // check if the model was trimmed - if (mappedType.Value is CodeType type && type.TypeDefinition is CodeClass mappedClass && mappedClass.Parent is CodeNamespace codeNamespace && codeNamespace.FindChildByName(mappedClass.Name) != null) + if (mappedType.Value is not + { TypeDefinition: CodeClass { Parent: CodeNamespace codeNamespace } mappedClass } + || codeNamespace.FindChildByName( + $"{ModelDeserializerPrefix}{mappedClass.Name.ToFirstCharacterUpperCase()}") is not + { } deserializer) { - var deserializer = GetSerializationFunctionsForNamespace(mappedClass).Item2; + continue; + } - if (deserializer.Parent is not null) + if (deserializer.Parent is not null) + { + parsableFactoryFunction.AddUsing(new CodeUsing { - parsableFactoryFunction.AddUsing(new CodeUsing + Name = deserializer.Parent.Name, + Declaration = new CodeType { - Name = deserializer.Parent.Name, - Declaration = new CodeType - { - Name = deserializer.Name, - TypeDefinition = deserializer - }, - }); - } + Name = deserializer.Name, + TypeDefinition = deserializer + }, + }); } } } From 542be57afc65e1c9daf807c7af1602316b055cb8 Mon Sep 17 00:00:00 2001 From: rkodev <43806892+rkodev@users.noreply.github.com> Date: Thu, 21 Nov 2024 19:47:43 +0300 Subject: [PATCH 13/13] Format code --- src/Kiota.Builder/Refiners/TypeScriptRefiner.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs index 3ca1fbd068..ca06132982 100644 --- a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs +++ b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs @@ -1494,10 +1494,10 @@ private static void AddDeserializerUsingToDiscriminatorFactory(CodeElement codeE foreach (var mappedType in parsableFactoryFunction.OriginalMethodParentClass.DiscriminatorInformation.DiscriminatorMappings) { if (mappedType.Value is not - { TypeDefinition: CodeClass { Parent: CodeNamespace codeNamespace } mappedClass } + { TypeDefinition: CodeClass { Parent: CodeNamespace codeNamespace } mappedClass } || codeNamespace.FindChildByName( $"{ModelDeserializerPrefix}{mappedClass.Name.ToFirstCharacterUpperCase()}") is not - { } deserializer) + { } deserializer) { continue; }