Skip to content

Commit

Permalink
Fixes some wrong method names, invalid enum values and correct deprec…
Browse files Browse the repository at this point in the history
…ation annotation
  • Loading branch information
joanne-ter-maat committed Oct 4, 2024
1 parent 8245968 commit 2e4a471
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/Kiota.Builder/Writers/Dart/CodeEnumWriter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;

using Kiota.Builder.CodeDOM;
using Kiota.Builder.Extensions;
using Kiota.Builder.Refiners;
using Microsoft.OpenApi.Any;

namespace Kiota.Builder.Writers.Dart;
public class CodeEnumWriter : BaseElementWriter<CodeEnum, DartConventionService>
Expand All @@ -23,20 +25,31 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write
writer.StartBlock($"enum {enumName} {{");
var lastOption = codeElement.Options.Last();

HashSet<String> usedNames = new HashSet<string>();
foreach (var option in codeElement.Options)
{
conventions.WriteShortDescription(option, writer);
var correctedName = getCorrectedName(option.Name);
if (reservedNamesProvider.ReservedNames.Contains(correctedName))
if (reservedNamesProvider.ReservedNames.Contains(correctedName) || IllegalEnumValue(correctedName))
{
correctedName += "Escaped";
}
if (!usedNames.Add(correctedName))
{
correctedName = option.Name;
usedNames.Add(correctedName);
}
writer.WriteLine($"{correctedName}(\"{option.Name}\"){(option == lastOption ? ";" : ",")}");
}
writer.WriteLine($"const {enumName}(this.value);");
writer.WriteLine("final String value;");
}

private bool IllegalEnumValue(string correctedName)
{
return correctedName.EqualsIgnoreCase("string") || correctedName.EqualsIgnoreCase("index");
}

private string getCorrectedName(string name)
{
if (name.Contains('_', StringComparison.Ordinal))
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/Dart/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ protected void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams re
};
writer.WriteLine($"{prefix}await requestAdapter.{GetSendRequestMethodName(isVoid, codeElement, codeElement.ReturnType)}(requestInfo{returnTypeFactory}, {errorMappingVarName});");
if (codeElement.ReturnType.IsCollection)
writer.WriteLine("return collectionResult?.ToList();");
writer.WriteLine("return collectionResult?.toList();");
}
private const string RequestInfoVarName = "requestInfo";
private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams requestParams, CodeClass currentClass, LanguageWriter writer)
Expand Down Expand Up @@ -479,7 +479,7 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req
}
else if (currentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) is CodeProperty requestAdapterProperty)
if (requestParams.requestBody.Type is CodeType bodyType && (bodyType.TypeDefinition is CodeClass || bodyType.Name.Equals("MultipartBody", StringComparison.OrdinalIgnoreCase)))
writer.WriteLine($"{RequestInfoVarName}.setContentFromParsable({requestAdapterProperty.Name.ToFirstCharacterLowerCase()}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name});");
writer.WriteLine($"{RequestInfoVarName}.setContentFromParsable{suffix}({requestAdapterProperty.Name.ToFirstCharacterLowerCase()}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name});");
else
writer.WriteLine($"{RequestInfoVarName}.setContentFromScalar{suffix}({requestAdapterProperty.Name.ToFirstCharacterLowerCase()}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name});");
}
Expand Down
6 changes: 3 additions & 3 deletions src/Kiota.Builder/Writers/Dart/DartConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DartConventionService : CommonLanguageConventionService
public override string StreamTypeName => "stream";
public override string VoidTypeName => "void";
public override string DocCommentPrefix => "/// ";
private static readonly HashSet<string> NullableTypes = new(StringComparer.OrdinalIgnoreCase) { "int", "bool", "double", "string", "datetime" };
private static readonly HashSet<string> NullableTypes = new(StringComparer.OrdinalIgnoreCase) { "int", "bool", "double", "string", "datetime", "dateonly", "timeonly" };
public const char NullableMarker = '?';
public static string NullableMarkerAsString => "?";
public override string ParseNodeInterfaceName => "ParseNode";
Expand Down Expand Up @@ -313,14 +313,14 @@ _ when nameof(String).Equals(parameterType, StringComparison.OrdinalIgnoreCase)
var close = !string.IsNullOrEmpty(defaultValue) ? "}" : "";
return $"{GetDeprecationInformation(parameter)}{open}{parameterType} {parameter.Name.ToFirstCharacterLowerCase()}{defaultValue}{close}";
}
private static string GetDeprecationInformation(IDeprecableElement element)
private string GetDeprecationInformation(IDeprecableElement element)
{
if (element.Deprecation is null || !element.Deprecation.IsDeprecated) return string.Empty;

var versionComment = string.IsNullOrEmpty(element.Deprecation.Version) ? string.Empty : $" as of {element.Deprecation.Version}";
var dateComment = element.Deprecation.Date is null ? string.Empty : $" on {element.Deprecation.Date.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
var removalComment = element.Deprecation.RemovalDate is null ? string.Empty : $" and will be removed {element.Deprecation.RemovalDate.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
return $"@obsolete(\"{element.Deprecation.GetDescription}{versionComment}{dateComment}{removalComment}\")";
return $"@Deprecated(\"{element.Deprecation.GetDescription(type => GetTypeString(type, (element as CodeElement)!))}{versionComment}{dateComment}{removalComment}\")";
}
internal void WriteDeprecationAttribute(IDeprecableElement element, LanguageWriter writer)
{
Expand Down

0 comments on commit 2e4a471

Please sign in to comment.