Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ private void WriteEnumContent(CodeWriter writer)
for (int i = 0; i < _provider.Fields.Count; i++)
{
writer.WriteXmlDocsNoScope(_provider.Fields[i].XmlDocs);
foreach (var attr in _provider.Fields[i].Attributes)
{
attr.Write(writer);
}
writer.Append($"{_provider.Fields[i].Name}");
if (_provider.Fields[i].InitializationValue != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// <auto-generated/>

#nullable disable

using System;

namespace Sample.Models
{
public enum TestEnum
{
[global::System.ObsoleteAttribute]
Value1 = 1,
Value2 = 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TypeSpec.Generator.Expressions;
using Microsoft.TypeSpec.Generator.Input;
using Microsoft.TypeSpec.Generator.Primitives;
using Microsoft.TypeSpec.Generator.Providers;
using Microsoft.TypeSpec.Generator.Statements;
using Microsoft.TypeSpec.Generator.Tests.Common;
using NUnit.Framework;

Expand Down Expand Up @@ -73,5 +75,47 @@ public void TypeProviderWriter_WriteModelAsStruct()
internal static readonly InputModelProperty RequiredStringProperty = InputFactory.Property("requiredString", InputPrimitiveType.String, isRequired: true);

internal static readonly InputModelProperty RequiredIntProperty = InputFactory.Property("requiredInt", InputPrimitiveType.Int32, isRequired: true);

[Test]
public void TypeProviderWriter_WriteEnumWithFieldAttributes()
{
var enumProvider = new TestEnumWithAttributesProvider();
var codeFile = new TypeProviderWriter(enumProvider).Write();
var result = codeFile.Content;

var expected = Helpers.GetExpectedFromFile();

Assert.AreEqual(expected, result);
}

private class TestEnumWithAttributesProvider : TypeProvider
{
protected override string BuildRelativeFilePath() => "TestEnum.cs";
protected override string BuildName() => "TestEnum";
protected override string BuildNamespace() => "Sample.Models";
protected override TypeSignatureModifiers BuildDeclarationModifiers() => TypeSignatureModifiers.Public | TypeSignatureModifiers.Enum;

protected internal override FieldProvider[] BuildFields()
{
return
[
new FieldProvider(
FieldModifiers.Public | FieldModifiers.Static,
typeof(int),
"Value1",
this,
$"First value",
initializationValue: new LiteralExpression(1),
attributes: [new AttributeStatement(typeof(ObsoleteAttribute))]),
new FieldProvider(
FieldModifiers.Public | FieldModifiers.Static,
typeof(int),
"Value2",
this,
$"Second value",
initializationValue: new LiteralExpression(2))
];
}
}
}
}
Loading