diff --git a/src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs b/src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs index a1a9142b5b..e4ad74acae 100644 --- a/src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs +++ b/src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs @@ -310,7 +310,7 @@ private static void WriteProperties(CodeClass requestBodyClass, LanguageWriter w } else { - writer.Write(GetDefaultValueForProperty(prop), includeIndent: false); + writer.Write(HttpConventionService.GetDefaultValueForProperty(prop), includeIndent: false); } // Add a trailing comma if there are more properties to be written @@ -331,25 +331,6 @@ private static void WriteProperties(CodeClass requestBodyClass, LanguageWriter w } } - - /// - /// Gets the default value for the given property. - /// - /// The property to get the default value for. - /// The default value as a string. - private static string GetDefaultValueForProperty(CodeProperty codeProperty) - { - return codeProperty.Type.Name switch - { - "int" or "integer" => "0", - "string" => "\"string\"", - "bool" or "boolean" => "false", - _ when codeProperty.Type is CodeType enumType && enumType.TypeDefinition is CodeEnum enumDefinition => - enumDefinition.Options.FirstOrDefault()?.Name is string enumName ? $"\"{enumName}\"" : "null", - _ => "null" - }; - } - private static string BuildUrlStringFromTemplate(string urlTemplateString, List queryParameters, List pathParameters, string? baseUrl) { // Use the provided baseUrl or default to "http://localhost/" diff --git a/src/Kiota.Builder/Writers/HTTP/HttpConventionService.cs b/src/Kiota.Builder/Writers/HTTP/HttpConventionService.cs index f51ca9f5d6..72e1c43fbe 100644 --- a/src/Kiota.Builder/Writers/HTTP/HttpConventionService.cs +++ b/src/Kiota.Builder/Writers/HTTP/HttpConventionService.cs @@ -1,5 +1,5 @@ using System; - +using System.Linq; using Kiota.Builder.CodeDOM; using Kiota.Builder.Extensions; @@ -83,4 +83,22 @@ public override string GetParameterSignature(CodeParameter parameter, CodeElemen }; return $"{parameter.Name.ToFirstCharacterLowerCase()} : {parameterType}{defaultValue}"; } + + /// + /// Gets the default value for the given property. + /// + /// The property to get the default value for. + /// The default value as a string. + public static string GetDefaultValueForProperty(CodeProperty codeProperty) + { + return codeProperty?.Type.Name switch + { + "int" or "integer" => "0", + "string" => "\"string\"", + "bool" or "boolean" => "false", + _ when codeProperty?.Type is CodeType enumType && enumType.TypeDefinition is CodeEnum enumDefinition => + enumDefinition.Options.FirstOrDefault()?.Name is string enumName ? $"\"{enumName}\"" : "null", + _ => "null" + }; + } } diff --git a/tests/Kiota.Builder.Tests/Writers/HTTP/HttpConventionServiceTests.cs b/tests/Kiota.Builder.Tests/Writers/HTTP/HttpConventionServiceTests.cs new file mode 100644 index 0000000000..f91371712f --- /dev/null +++ b/tests/Kiota.Builder.Tests/Writers/HTTP/HttpConventionServiceTests.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +using Kiota.Builder.CodeDOM; +using Kiota.Builder.Writers; +using Kiota.Builder.Writers.Http; +using Xunit; + +namespace Kiota.Builder.Tests.Writers.Http; +public sealed class HttpConventionServiceTest +{ + + [Fact] + public void TestGetDefaultValueForProperty_Int() + { + // Arrange + var codeProperty = new CodeProperty + { + Type = new CodeType { Name = "int" } + }; + + // Act + var result = HttpConventionService.GetDefaultValueForProperty(codeProperty); + + // Assert + Assert.Equal("0", result); + } + + [Fact] + public void TestGetDefaultValueForProperty_String() + { + // Arrange + var codeProperty = new CodeProperty + { + Type = new CodeType { Name = "string" } + }; + + // Act + var result = HttpConventionService.GetDefaultValueForProperty(codeProperty); + + // Assert + Assert.Equal("\"string\"", result); + } + + [Fact] + public void TestGetDefaultValueForProperty_Bool() + { + // Arrange + var codeProperty = new CodeProperty + { + Type = new CodeType { Name = "bool" } + }; + + // Act + var result = HttpConventionService.GetDefaultValueForProperty(codeProperty); + + // Assert + Assert.Equal("false", result); + } + + [Fact] + public void TestGetDefaultValueForProperty_Null() + { + // Arrange + var codeProperty = new CodeProperty + { + Type = new CodeType { Name = "unknown" } + }; + + // Act + var result = HttpConventionService.GetDefaultValueForProperty(codeProperty); + + // Assert + Assert.Equal("null", result); + } +}