From b73493d533b100963aa2fe13d7552ab94fc15aa7 Mon Sep 17 00:00:00 2001 From: Llewellyn Roos Date: Thu, 22 Feb 2024 15:00:14 +0200 Subject: [PATCH 1/4] fix enum default value literal #4216 --- 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 1fb6d24e66..c5b809c950 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs @@ -255,8 +255,8 @@ private void WriteDeserializerFunction(CodeFunction codeFunction, LanguageWriter 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) - return $"{enumDefinition.Name.ToFirstCharacterUpperCase()}.{codeProperty.DefaultValue.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}"; + 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; } private static void WriteDefensiveStatements(CodeMethod codeElement, LanguageWriter writer) From 7ffd6abe44761eb8bc480e5470e6f78d79a57ca3 Mon Sep 17 00:00:00 2001 From: Llewellyn Roos Date: Thu, 22 Feb 2024 22:30:07 +0200 Subject: [PATCH 2/4] Add test for TypeScript enum object reference --- tests/Kiota.Builder.Tests/TestHelper.cs | 2 ++ .../TypeScript/CodeFunctionWriterTests.cs | 23 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/Kiota.Builder.Tests/TestHelper.cs b/tests/Kiota.Builder.Tests/TestHelper.cs index bd9725336f..aa81170632 100644 --- a/tests/Kiota.Builder.Tests/TestHelper.cs +++ b/tests/Kiota.Builder.Tests/TestHelper.cs @@ -115,6 +115,8 @@ public static void AddSerializationPropertiesToModelClass(CodeClass modelClass) { Name = "EnumType" }; + var enumOption = new CodeEnumOption() { Name = "SomeOption" }; + propertyEnum.AddOption(enumOption); parentNamespace.AddEnum(propertyEnum); modelClass.AddProperty(new CodeProperty { diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs index 18358913bb..059e5a1c2a 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs @@ -333,6 +333,26 @@ public async Task WritesDeSerializerBodyWithDefaultValue() Name = "string", }, }); + var propertyEnum = new CodeEnum + { + Name = "EnumTypeWithOption", + Parent = parentClass, + }; + var enumOption = new CodeEnumOption() { Name = "SomeOption" }; + propertyEnum.AddOption(enumOption); + var codeNamespace = parentClass.Parent as CodeNamespace; + codeNamespace.AddEnum(propertyEnum); + parentClass.AddProperty(new CodeProperty + { + Name = "propWithDefaultEnum", + DefaultValue = enumOption.Name, + Type = new CodeType + { + Name = "EnumTypeWithOption", + TypeDefinition = propertyEnum, + } + }); + await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root); var deserializerFunction = root.FindChildByName($"deserializeInto{parentClass.Name.ToFirstCharacterUpperCase()}"); Assert.NotNull(deserializerFunction); @@ -342,6 +362,7 @@ public async Task WritesDeSerializerBodyWithDefaultValue() writer.Write(deserializerFunction); var result = tw.ToString(); Assert.Contains("?? \"Test Value\"", result); + Assert.Contains("?? EnumTypeWithOptionObject.SomeOption", result); } [Fact] public async Task WritesInheritedSerializerBody() @@ -1032,6 +1053,6 @@ public async Task WritesConstructorWithEnumValue() var serializeFunction = root.FindChildByName($"Serialize{parentClass.Name.ToFirstCharacterUpperCase()}"); writer.Write(serializeFunction); var result = tw.ToString(); - Assert.Contains($" ?? {codeEnum.Name.ToFirstCharacterUpperCase()}.{defaultValue.CleanupSymbolName()}", result);//ensure symbol is cleaned up + Assert.Contains($" ?? {codeEnum.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{defaultValue.CleanupSymbolName()}", result);//ensure symbol is cleaned up } } From 09d031096d3b225c441cd8cdcb1f038d1bea3b91 Mon Sep 17 00:00:00 2001 From: Llewellyn Roos Date: Fri, 23 Feb 2024 08:36:43 +0200 Subject: [PATCH 3/4] Update change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be77daa67d..cdc291662b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Deduplicates 4XX and 5XX error mappings when they map to the same type to reduce emitted code. [#4025](https://github.com/microsoft/kiota/issues/4025) - 📢📢📢 Java generation is now stable! 🚀🚀🚀 special thanks to @andreaTP (Red Hat) for all the help. - Fixed bug where stream responses would generate incorrect partial paging code. [#4207](https://github.com/microsoft/kiota/issues/4207) +- Fixed a bug in enum default value generator for TypeScript. [#4216](https://github.com/microsoft/kiota/issues/4216) ## [1.11.1] - 2024-02-05 From 02c17d2eb8efb3800100668e05c5be9abae82268 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 23 Feb 2024 07:41:59 -0500 Subject: [PATCH 4/4] - fixes formatting --- .../Writers/TypeScript/CodeFunctionWriterTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs index 059e5a1c2a..8855644e50 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs @@ -339,17 +339,17 @@ public async Task WritesDeSerializerBodyWithDefaultValue() Parent = parentClass, }; var enumOption = new CodeEnumOption() { Name = "SomeOption" }; - propertyEnum.AddOption(enumOption); + propertyEnum.AddOption(enumOption); var codeNamespace = parentClass.Parent as CodeNamespace; codeNamespace.AddEnum(propertyEnum); parentClass.AddProperty(new CodeProperty { - Name = "propWithDefaultEnum", + Name = "propWithDefaultEnum", DefaultValue = enumOption.Name, Type = new CodeType { Name = "EnumTypeWithOption", - TypeDefinition = propertyEnum, + TypeDefinition = propertyEnum, } });