Skip to content

Commit

Permalink
Merge pull request #4217 from lroos/fix/typescript-writer-enum-default
Browse files Browse the repository at this point in the history
TypeScript writer enum default
  • Loading branch information
baywet authored Feb 23, 2024
2 parents cad8396 + 02c17d2 commit 4c938dd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 📢📢📢 Java generation is now stable! 🚀🚀🚀 special thanks to @andreaTP (Red Hat) for all the help.
- Fixed bug where stream responses would generate incorrect partial paging code. [#4207](https://github.com/microsoft/kiota/issues/4207)
- Sanitize Accept and Content-Type headers during generation. [#4211](https://github.com/microsoft/kiota/issues/4211)
- Fixed a bug in enum default value generator for TypeScript. [#4216](https://github.com/microsoft/kiota/issues/4216)

## [1.11.1] - 2024-02-05

Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ private void WriteDeserializerFunction(CodeFunction codeFunction, LanguageWriter
private static string GetDefaultValueLiteralForProperty(CodeProperty codeProperty)
{
if (string.IsNullOrEmpty(codeProperty.DefaultValue)) return string.Empty;
if (codeProperty.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition)
return $"{enumDefinition.Name.ToFirstCharacterUpperCase()}.{codeProperty.DefaultValue.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}";
if (codeProperty.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition && enumDefinition.CodeEnumObject is not null)
return $"{enumDefinition.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{codeProperty.DefaultValue.Trim('"').CleanupSymbolName().ToFirstCharacterUpperCase()}";
return codeProperty.DefaultValue;
}
private static void WriteDefensiveStatements(CodeMethod codeElement, LanguageWriter writer)
Expand Down
2 changes: 2 additions & 0 deletions tests/Kiota.Builder.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public static void AddSerializationPropertiesToModelClass(CodeClass modelClass)
{
Name = "EnumType"
};
var enumOption = new CodeEnumOption() { Name = "SomeOption" };
propertyEnum.AddOption(enumOption);
parentNamespace.AddEnum(propertyEnum);
modelClass.AddProperty(new CodeProperty
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,26 @@ public async Task WritesDeSerializerBodyWithDefaultValue()
Name = "string",
},
});
var propertyEnum = new CodeEnum
{
Name = "EnumTypeWithOption",
Parent = parentClass,
};
var enumOption = new CodeEnumOption() { Name = "SomeOption" };
propertyEnum.AddOption(enumOption);
var codeNamespace = parentClass.Parent as CodeNamespace;
codeNamespace.AddEnum(propertyEnum);
parentClass.AddProperty(new CodeProperty
{
Name = "propWithDefaultEnum",
DefaultValue = enumOption.Name,
Type = new CodeType
{
Name = "EnumTypeWithOption",
TypeDefinition = propertyEnum,
}
});

await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root);
var deserializerFunction = root.FindChildByName<CodeFunction>($"deserializeInto{parentClass.Name.ToFirstCharacterUpperCase()}");
Assert.NotNull(deserializerFunction);
Expand All @@ -342,6 +362,7 @@ public async Task WritesDeSerializerBodyWithDefaultValue()
writer.Write(deserializerFunction);
var result = tw.ToString();
Assert.Contains("?? \"Test Value\"", result);
Assert.Contains("?? EnumTypeWithOptionObject.SomeOption", result);
}
[Fact]
public async Task WritesInheritedSerializerBody()
Expand Down Expand Up @@ -1032,6 +1053,6 @@ public async Task WritesConstructorWithEnumValue()
var serializeFunction = root.FindChildByName<CodeFunction>($"Serialize{parentClass.Name.ToFirstCharacterUpperCase()}");
writer.Write(serializeFunction);
var result = tw.ToString();
Assert.Contains($" ?? {codeEnum.Name.ToFirstCharacterUpperCase()}.{defaultValue.CleanupSymbolName()}", result);//ensure symbol is cleaned up
Assert.Contains($" ?? {codeEnum.CodeEnumObject.Name.ToFirstCharacterUpperCase()}.{defaultValue.CleanupSymbolName()}", result);//ensure symbol is cleaned up
}
}

0 comments on commit 4c938dd

Please sign in to comment.