Skip to content

Commit

Permalink
Merged features/dart into namin
Browse files Browse the repository at this point in the history
  • Loading branch information
Kees-Schotanus committed Nov 4, 2024
2 parents 4c4a30b + 3764ec0 commit 8b5f44f
Show file tree
Hide file tree
Showing 7 changed files with 1,795 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/Kiota.Builder/Refiners/DartRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ private void EscapeStringValues(CodeElement currentElement)
property.DefaultValue = property.DefaultValue.Replace("$", "\\$", StringComparison.Ordinal);
}
}
else if (currentElement is CodeMethod method && method.HasUrlTemplateOverride)
{
method.UrlTemplateOverride = method.UrlTemplateOverride.Replace("$", "\\$", StringComparison.Ordinal);
}
CrawlTree(currentElement, EscapeStringValues);
}

Expand Down
23 changes: 11 additions & 12 deletions src/Kiota.Builder/Writers/Dart/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,10 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho
if (propWithDefault.Type is CodeType propertyType2 && propertyType2.Name.Equals("String", StringComparison.Ordinal))
{
defaultValue = defaultValue.Trim('"');
defaultValue = $"'{defaultValue}'";
}
else if (propWithDefault.Type is CodeType propertyType3 && propertyType3.Name.Equals("Boolean", StringComparison.Ordinal))
{
defaultValue = defaultValue.Trim('"');
defaultValue = $"{defaultValue}";
if (propertyType2.Name.Equals("String", StringComparison.Ordinal))
{
defaultValue = $"'{defaultValue}'";
}
}
writer.WriteLine($"{propWithDefault.Name.ToFirstCharacterLowerCase()} = {defaultValue}{separator}");
}
Expand Down Expand Up @@ -510,7 +508,8 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req
if (currentClass.GetPropertyOfKind(CodePropertyKind.UrlTemplate) is not CodeProperty urlTemplateProperty) throw new InvalidOperationException("url template property cannot be null");

var operationName = codeElement.HttpMethod.ToString();
writer.WriteLine($"var {RequestInfoVarName} = RequestInformation(httpMethod : HttpMethod.{operationName?.ToLowerInvariant()}, {currentClass.GetPropertyOfKind(CodePropertyKind.UrlTemplate)?.Name} : {GetPropertyCall(urlTemplateProperty, "string.Empty")}, {currentClass.GetPropertyOfKind(CodePropertyKind.PathParameters)?.Name} : {GetPropertyCall(urlTemplateParamsProperty, "string.Empty")});");
var urlTemplateValue = codeElement.HasUrlTemplateOverride ? $"'{codeElement.UrlTemplateOverride}'" : GetPropertyCall(urlTemplateProperty, "''");
writer.WriteLine($"var {RequestInfoVarName} = RequestInformation(httpMethod : HttpMethod.{operationName?.ToLowerInvariant()}, {urlTemplateProperty.Name} : {urlTemplateValue}, {urlTemplateParamsProperty.Name} : {GetPropertyCall(urlTemplateParamsProperty, "string.Empty")});");

if (requestParams.requestConfiguration != null && requestParams.requestConfiguration.Type is CodeType paramType)
{
Expand Down Expand Up @@ -620,10 +619,10 @@ private void WriteSerializerBodyForIntersectionModel(CodeMethod method, CodeClas
writer.IncreaseIndent();
}
var firstPropertyName = complexProperties.First().Name;
var propertiesNames = complexProperties.Skip(0)
var propertiesNames = complexProperties.Skip(1).Any() ? complexProperties.Skip(1)
.Select(static x => x.Name)
.OrderBy(static x => x)
.Aggregate(static (x, y) => $"{x}, {y}");
.Aggregate(static (x, y) => $"{x}, {y}") : string.Empty;
var propertiesList = string.IsNullOrEmpty(propertiesNames) ? "" : $", [{propertiesNames}]";
writer.WriteLine($"writer.{GetSerializationMethodName(complexProperties.First().Type, method)}(null, {firstPropertyName}{propertiesList});");
if (includeElse)
Expand Down Expand Up @@ -736,19 +735,19 @@ private void WriteMethodPrototype(CodeMethod code, CodeClass parentClass, Langua
$"[{GetParameterSignatureWithNullableRefType(p, code)}]" :
conventions.GetParameterSignature(p, code))
.ToList());
writer.WriteLine($"{conventions.GetAccessModifier(code.Access)} {staticModifier}{completeReturnTypeWithNullable}{methodName}({nullableParameters}){baseSuffix}{async} {{");
writer.WriteLine($"{staticModifier}{completeReturnTypeWithNullable}{conventions.GetAccessModifier(code.Access)}{methodName}({nullableParameters}){baseSuffix}{async} {{");
}
else if (parentClass.IsOfKind(CodeClassKind.Model) && code.IsOfKind(CodeMethodKind.Custom) && code.Name.EqualsIgnoreCase("copyWith"))
{
var parentParameters = "int? statusCode, String? message, Map<String, List<String>>? responseHeaders, Iterable<Object?>? innerExceptions, ";
var ownParameters = string.Join(", ", parentClass.GetPropertiesOfKind(CodePropertyKind.Custom, CodePropertyKind.AdditionalData)
.Where(p => !conventions.ErrorClassPropertyExistsInSuperClass(p))
.Select(p => $"{GetPrefix(p)}{conventions.TranslateType(p.Type)}{getSuffix(p)}? {p.Name}"));
writer.WriteLine($"{conventions.GetAccessModifier(code.Access)} {staticModifier}{completeReturnType}{methodName}({openingBracket}{parentParameters}{ownParameters} }}){{");
writer.WriteLine($"{staticModifier}{completeReturnType}{conventions.GetAccessModifier(code.Access)}{methodName}({openingBracket}{parentParameters}{ownParameters} }}){{");
}
else
{
writer.WriteLine($"{conventions.GetAccessModifier(code.Access)} {staticModifier}{completeReturnType}{methodName}({parameters}{closingparenthesis}{baseSuffix}{async} {openingBracket}");
writer.WriteLine($"{staticModifier}{completeReturnType}{conventions.GetAccessModifier(code.Access)}{methodName}({parameters}{closingparenthesis}{baseSuffix}{async} {openingBracket}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/Dart/CodePropertyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private void WritePropertyInternal(CodeProperty codeElement, LanguageWriter writ
case CodePropertyKind.AdditionalData when backingStoreProperty != null:
case CodePropertyKind.Custom when backingStoreProperty != null:
var backingStoreKey = codeElement.WireName;
var defaultIfNotNullable = propertyType.EndsWith('?') ? string.Empty : $" ?? {codeElement.DefaultValue}";
var defaultIfNotNullable = propertyType.EndsWith('?') ? string.Empty : codeElement.IsOfKind(CodePropertyKind.AdditionalData) ? " ?? {}" : $" ?? {codeElement.DefaultValue}";
writer.WriteLine($"{propertyType} get {conventions.GetAccessModifierPrefix(codeElement.Access)}{propertyName} {{");
writer.IncreaseIndent();
writer.WriteLine($"return {backingStoreProperty.Name}.get<{propertyType}>('{backingStoreKey}'){defaultIfNotNullable};");
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/Dart/DartConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void WriteLongDescription(CodeElement element, LanguageWriter writer, IEn
writer.WriteLine($"{DocCommentPrefix}{additionalComment}");

if (documentation.ExternalDocumentationAvailable)
writer.WriteLine($"{DocCommentPrefix}@see <a href=\"{documentation.DocumentationLink}\">{documentation.DocumentationLabel}</a>");
writer.WriteLine($"{DocCommentPrefix} [{documentation.DocumentationLabel}]({documentation.DocumentationLink})");
}
}

Expand All @@ -87,7 +87,7 @@ private string[] GetDeprecationInformationForDocumentationComment(IDeprecableEle
public override string GetAccessModifier(AccessModifier access)
{
// Dart does not support access modifiers
return "";
return access == AccessModifier.Private ? "_" : string.Empty;
}

#pragma warning disable CA1822 // Method should be static
Expand Down
88 changes: 88 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Dart/CodeEnumWriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.IO;
using System.Linq;

using Kiota.Builder.CodeDOM;
using Kiota.Builder.Extensions;
using Kiota.Builder.Writers;

using Xunit;

namespace Kiota.Builder.Tests.Writers.Dart;
public sealed class CodeEnumWriterTests : IDisposable
{
private const string DefaultPath = "./";
private const string DefaultName = "name";
private readonly StringWriter tw;
private readonly LanguageWriter writer;
private readonly CodeEnum currentEnum;
private const string EnumName = "SomeEnum";
public CodeEnumWriterTests()
{
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.Dart, DefaultPath, DefaultName);
tw = new StringWriter();
writer.SetTextWriter(tw);
var root = CodeNamespace.InitRootNamespace();
currentEnum = root.AddEnum(new CodeEnum
{
Name = EnumName,
}).First();
}
public void Dispose()
{
tw?.Dispose();
GC.SuppressFinalize(this);
}
[Fact]
public void WritesEnum()
{
const string optionName = "option1";
currentEnum.AddOption(new CodeEnumOption { Name = optionName, SerializationName = optionName });
writer.Write(currentEnum);
var result = tw.ToString();
Assert.Contains("enum", result);
Assert.Contains(EnumName, result);
Assert.Contains($"{optionName}(\"{optionName}\")", result);
Assert.Contains($"const {EnumName}(this.value);", result);
Assert.Contains("final String value;", result);
}
[Fact]
public void DoesntWriteAnythingOnNoOption()
{
writer.Write(currentEnum);
var result = tw.ToString();
Assert.Empty(result);
}
[Fact]
public void WritesEnumOptionDescription()
{
var option = new CodeEnumOption
{
Documentation = new()
{
DescriptionTemplate = "Some option description",
},
Name = "option1",
};
currentEnum.AddOption(option);
writer.Write(currentEnum);
var result = tw.ToString();
Console.WriteLine(result);
Assert.Contains($"/// {option.Documentation.DescriptionTemplate}", result);
}
[Fact]
public void WritesEnumSerializationValue()
{
var OptionName = "plus1";
var SerializationValue = "+1";
var option = new CodeEnumOption
{
Name = OptionName,
SerializationName = SerializationValue
};
currentEnum.AddOption(option);
writer.Write(currentEnum);
var result = tw.ToString();
Assert.Contains($"{OptionName}(\"{SerializationValue}\")", result);
}
}
Loading

0 comments on commit 8b5f44f

Please sign in to comment.