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

Fix enum name evaluation in go contructors #3836

Merged
merged 2 commits into from
Nov 30, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug where property names that matched reserved type would be replaced in dotnet.
- Fixed pass by value for `contentType` param in Go `requestInfo.SetStreamContentAndContentType`[#3830](https://github.com/microsoft/kiota/issues/3830)
- Fixed parsing of `DateOnly` values generated in request executors [#3679](https://github.com/microsoft/kiota/issues/3679)
- Fixes generation of default values names for go contructor functions [#3436](https://github.com/microsoft/kiota/issues/3436)


## [1.8.2] - 2023-11-08

Expand Down
5 changes: 4 additions & 1 deletion src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,10 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho
var defaultValue = propWithDefault.DefaultValue;
if (propWithDefault.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition)
{
defaultValue = $"{defaultValue.Trim('"').ToUpperInvariant()}_{enumDefinition.Name.ToUpperInvariant()}";
defaultValue = defaultValue.Trim('"');
defaultValue =
enumDefinition.Options.FirstOrDefault(x => x.SerializationName.Equals(defaultValue, StringComparison.OrdinalIgnoreCase))?.Name ?? defaultValue;
defaultValue = $"{defaultValue.ToUpperInvariant()}_{enumDefinition.Name.ToUpperInvariant()}";
}
writer.WriteLine($"{defaultValueReference} := {defaultValue}");
defaultValueReference = $"&{defaultValueReference}";
Expand Down
26 changes: 26 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,32 @@ public void WritesConstructor()
Assert.Contains("NewBaseRequestBuilder", result);
}
[Fact]
public void WritesConstructorWithEnumValue()
{
setup();
var modelsNamespace = root.AddNamespace("models");
method.Kind = CodeMethodKind.Constructor;
var serializationName = "1024x1024";
var defaultValue = "TENTWENTYFOUR_BY_TENTWENTYFOUR";
var propName = "size";
var codeEnum = modelsNamespace.AddEnum(new CodeEnum
{
Name = "pictureSize"
}).First();
parentClass.AddProperty(new CodeProperty
{
Name = propName,
DefaultValue = defaultValue,
SerializationName = serializationName,
Kind = CodePropertyKind.Custom,
Type = new CodeType { TypeDefinition = codeEnum }
});
writer.Write(method);
var result = tw.ToString();
Assert.Contains(parentClass.Name.ToFirstCharacterUpperCase(), result);
Assert.Contains($"Set{propName.ToFirstCharacterUpperCase()}({defaultValue})", result);//ensure symbol is cleaned up
}
[Fact]
public void WritesWithUrl()
{
setup();
Expand Down
Loading