Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
koros committed Dec 3, 2024
1 parent d9da636 commit ef519d1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 21 deletions.
21 changes: 1 addition & 20 deletions src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -331,25 +331,6 @@ private static void WriteProperties(CodeClass requestBodyClass, LanguageWriter w
}
}


/// <summary>
/// Gets the default value for the given property.
/// </summary>
/// <param name="codeProperty">The property to get the default value for.</param>
/// <returns>The default value as a string.</returns>
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<CodeProperty> queryParameters, List<CodeProperty> pathParameters, string? baseUrl)
{
// Use the provided baseUrl or default to "http://localhost/"
Expand Down
20 changes: 19 additions & 1 deletion src/Kiota.Builder/Writers/HTTP/HttpConventionService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;

using System.Linq;
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Extensions;

Expand Down Expand Up @@ -83,4 +83,22 @@ public override string GetParameterSignature(CodeParameter parameter, CodeElemen
};
return $"{parameter.Name.ToFirstCharacterLowerCase()} : {parameterType}{defaultValue}";
}

/// <summary>
/// Gets the default value for the given property.
/// </summary>
/// <param name="codeProperty">The property to get the default value for.</param>
/// <returns>The default value as a string.</returns>
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"
};
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit ef519d1

Please sign in to comment.