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

#2488 - Add using statement for EnumMember attribute #2494

Merged
merged 1 commit into from
Mar 31, 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 src/Kiota.Builder/Refiners/CSharpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ protected static void MakeEnumPropertiesNullable(CodeElement currentElement)
"Microsoft.Kiota.Abstractions.Serialization", "ParseNodeHelper"),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.Headers),
"Microsoft.Kiota.Abstractions", "RequestHeaders"),
new (static x => x is CodeEnum prop && prop.Options.Any(x => x.IsNameEscaped),
"System.Runtime.Serialization", "EnumMemberAttribute")
};
protected static void CapitalizeNamespacesFirstLetters(CodeElement current)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@ public class CSharpLanguageRefinerTests
private readonly CodeNamespace root = CodeNamespace.InitRootNamespace();
#region CommonLanguageRefinerTests
[Fact]
public async Task EnumHasEscapedOption_UsesEnumMemberAttribute()
{
var model = root.AddEnum(new CodeEnum
{
Name = "someenum"
}).First();
var option = new CodeEnumOption { Name = "kiotaCsharpName", SerializationName = "Kiota:CSharp:Enum" };
model.AddOption(option);
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.CSharp }, root);

var declaration = model.StartBlock;

Assert.Contains("EnumMemberAttribute", declaration.Usings.Select(x => x.Name));
}
[Fact]
public async Task EnumDoesntHaveEscapedOption_DoesntUseEnumMemberAttribute()
{
var model = root.AddEnum(new CodeEnum
{
Name = "someenum"
}).First();
var option = new CodeEnumOption { Name = "item1" };
model.AddOption(option);
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.CSharp }, root);

var declaration = model.StartBlock;

Assert.DoesNotContain("EnumMemberAttribute", declaration.Usings.Select(x => x.Name));
}
[Fact]
public async Task AddsExceptionInheritanceOnErrorClasses()
{
var model = root.AddClass(new CodeClass
Expand Down