Skip to content

Commit

Permalink
- fixes a bug where TypeScript generation would consider boolean argu…
Browse files Browse the repository at this point in the history
…ment as empty when false
  • Loading branch information
baywet committed Mar 20, 2024
1 parent 4aec462 commit 99f4a42
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 99f4a42

Please sign in to comment.