From 99f4a42c19324055db4a5c443e575e11935d8854 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 20 Mar 2024 12:41:40 -0400 Subject: [PATCH] - fixes a bug where TypeScript generation would consider boolean argument as empty when false --- CHANGELOG.md | 1 + .../Writers/TypeScript/CodeFunctionWriter.cs | 5 +- .../TypeScript/CodeFunctionWriterTests.cs | 57 +++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eba65dc8d8..f6a71fd038 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - PREVIEW: Moved preview configuration files to the .kiota directory. [#4310](https://github.com/microsoft/kiota/issues/4310) - PREVIEW: Moved the copy descriptions to dedicated folders. [#4310](https://github.com/microsoft/kiota/issues/4310) - PREVIEW: Renamed the config to workspace file. [#4310](https://github.com/microsoft/kiota/issues/4310) +- Fixed a bug where TypeScript generation would consider boolean argument as empty when false. [#4103](https://github.com/microsoft/kiota/issues/4103) - Changed Csharp code generation to put braces on new lines (where it makes sense). [#4347](https://github.com/microsoft/kiota/issues/4347) ## [1.12.0] - 2024-03-06 diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs index c5b809c950..0e54393520 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs @@ -259,7 +259,7 @@ private static string GetDefaultValueLiteralForProperty(CodeProperty codePropert return $"{enumDefinition.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{codeProperty.DefaultValue.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}"; return codeProperty.DefaultValue; } - private static void WriteDefensiveStatements(CodeMethod codeElement, LanguageWriter writer) + private void WriteDefensiveStatements(CodeMethod codeElement, LanguageWriter writer) { if (codeElement.IsOfKind(CodeMethodKind.Setter)) return; @@ -271,7 +271,8 @@ private static void WriteDefensiveStatements(CodeMethod codeElement, LanguageWri .OrderBy(static x => x.Name, StringComparer.OrdinalIgnoreCase)) { var parameterName = parameter.Name.ToFirstCharacterLowerCase(); - writer.WriteLine($"if(!{parameterName}) throw new Error(\"{parameterName} cannot be undefined\");"); + if (!"boolean".Equals(conventions.TranslateType(parameter.Type), StringComparison.OrdinalIgnoreCase)) + writer.WriteLine($"if(!{parameterName}) throw new Error(\"{parameterName} cannot be undefined\");"); } } private string GetDeserializationMethodName(CodeTypeBase propType, CodeFunction codeFunction) diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs index 8855644e50..a22dec13c7 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeFunctionWriterTests.cs @@ -717,6 +717,63 @@ public void WritesDeprecationInformationFromBuilder() var result = tw.ToString(); Assert.Contains("This method is obsolete. Use NewAwesomeMethod instead.", result); } + [Fact] + public void WritesDefensiveStatements() + { + var parentClass = root.AddClass(new CodeClass + { + Name = "ODataError", + Kind = CodeClassKind.Model, + }).First(); + parentClass.DiscriminatorInformation.DiscriminatorPropertyName = "@odata.type"; + parentClass.DiscriminatorInformation.AddDiscriminatorMapping("string", new CodeType() { Name = "string" }); + var method = TestHelper.CreateMethod(parentClass, MethodName, ReturnTypeName); + method.AddParameter(new CodeParameter + { + Name = "param1", + Type = new CodeType() + { + Name = "string", + IsNullable = false, + }, + }); + method.Kind = CodeMethodKind.Factory; + method.IsStatic = true; + method.Name = "NewAwesomeMethod";// new method replacement + var function = new CodeFunction(method); + root.TryAddCodeFile("foo", function); + writer.Write(function); + var result = tw.ToString(); + Assert.Contains("cannot be undefined", result); + } + [Fact] + public void DoesNotWriteDefensiveStatementsForBooleanParameters() + { + var parentClass = root.AddClass(new CodeClass + { + Name = "ODataError", + Kind = CodeClassKind.Model, + }).First(); + var method = TestHelper.CreateMethod(parentClass, MethodName, ReturnTypeName); + method.AddParameter(new CodeParameter + { + Name = "param1", + Type = new CodeType() + { + Name = "boolean", + IsNullable = false, + }, + Optional = false, + }); + method.Kind = CodeMethodKind.Factory; + method.IsStatic = true; + method.Name = "NewAwesomeMethod";// new method replacement + var function = new CodeFunction(method); + root.TryAddCodeFile("foo", function); + writer.Write(function); + var result = tw.ToString(); + Assert.DoesNotContain("cannot be undefined", result); + } private const string MethodDescription = "some description"; private const string ParamDescription = "some parameter description"; private const string ParamName = "paramName";