Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- fixes a bug where TypeScript generation would consider boolean argument as empty when false #4365

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading