Skip to content

Commit cd795c0

Browse files
Add [Obsolete] to deprecated members. (#472)
* Add [Obsolete] to deprecated members. * Add test * Fix test break
1 parent 53db630 commit cd795c0

File tree

8 files changed

+28
-2
lines changed

8 files changed

+28
-2
lines changed

src/FlatSharp.Compiler/SchemaModel/BaseReferenceTypeSchemaModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private void EmitCopyConstructor(CodeWriter writer, CompileContext context)
134134
return;
135135
}
136136

137-
foreach (var property in this.properties)
137+
foreach (var property in this.properties.Where(x => !x.Value.Field.Deprecated))
138138
{
139139
writer.AppendLine($"this.{property.Value.FieldName} = {context.FullyQualifiedCloneMethodName}(source.{property.Value.FieldName});");
140140
}
@@ -162,7 +162,7 @@ private void EmitDefaultConstrutor(CodeWriter writer, CompileContext context)
162162
writer.AppendLine($"public {this.Name}()");
163163
using (writer.WithBlock())
164164
{
165-
foreach (var item in this.properties.OrderBy(x => x.Key))
165+
foreach (var item in this.properties.OrderBy(x => x.Key).Where(x => !x.Value.Field.Deprecated))
166166
{
167167
this.EmitDefaultConstructorFieldInitialization(item.Value, writer, context);
168168
}

src/FlatSharp.Compiler/SchemaModel/PropertyFieldModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ public void WriteCode(CompileContext context, CodeWriter writer)
142142
writer.AppendSummaryComment(this.Field.Documentation);
143143
writer.AppendLine(this.GetAttribute());
144144

145+
if (this.Field.Deprecated)
146+
{
147+
writer.AppendLine("[Obsolete]");
148+
}
149+
145150
this.Attributes.EmitAsMetadata(writer);
146151

147152
SetterKind setterKind = this.Attributes.SetterKind ?? SetterKind.Public;

src/FlatSharp/Serialization/RoslynSerializerGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ Func<string> cSharpFactory
337337
var failures = result.Diagnostics
338338
.Where(d => d.Id != "CS8019") // unnecessary using directive.
339339
.Where(d => d.Id != "CS1701") // DLL version mismatch
340+
.Where(d => d.Id != "CS9042") // obsolete warning
340341
.ToArray();
341342

342343
if (failures.Length > 0)

src/Tests/FlatSharpEndToEndTests/MetadataAttributes/MetadataAttributeTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public void Table_Attributes()
111111
Assert.AreEqual("0", attrs["required"]);
112112
Assert.AreEqual("0", attrs["fs_writeThrough"]);
113113
Assert.AreEqual("value_struct", attrs["test"]);
114+
115+
attrs = GetAttributes(typeof(Message).GetProperty("DeprecatedInt"));
116+
Assert.AreEqual(1, attrs.Count);
117+
Assert.AreEqual("0", attrs["deprecated"]);
118+
Assert.IsNotNull(typeof(Message).GetProperty("DeprecatedInt").GetCustomAttribute<ObsoleteAttribute>());
114119
}
115120

116121
private static Dictionary<string, string?> GetAttributes<T>() => GetAttributes(typeof(T));

src/Tests/FlatSharpEndToEndTests/MetadataAttributes/MetadataAttributes.fbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ table Message (fs_serializer:"Lazy", test:"Message") {
3737
single_int : int (test:"single_int");
3838
vector_int : [ int ] (fs_vectorKind:"IList", test:"vector_int");
3939
value_struct : ValueStruct (required, fs_writeThrough, test:"value_struct");
40+
deprecated_int : int (deprecated);
4041
}
4142

4243
rpc_service EchoService (fs_rpcInterface, test:"EchoService") {

src/Tests/FlatSharpEndToEndTests/RawData/RawDataStringTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public void EmptyString()
4747
Assert.IsTrue(expectedResult.AsSpan().SequenceEqual(data));
4848
}
4949

50+
#pragma warning disable CS0612 // Type or member is obsolete
51+
5052
[TestMethod]
5153
public void DeprecatedString()
5254
{
@@ -68,6 +70,8 @@ public void DeprecatedString()
6870
Assert.IsTrue(expectedResult.AsSpan().SequenceEqual(data));
6971
}
7072

73+
#pragma warning restore CS0612 // Type or member is obsolete
74+
7175
[TestMethod]
7276
public void SimpleString()
7377
{
@@ -97,11 +101,13 @@ public void SimpleString()
97101
[TestMethod]
98102
public void StringVector()
99103
{
104+
#pragma warning disable CS0612 // Type or member is obsolete
100105
var root = new StringTable
101106
{
102107
ItemDeprecated = "notnull",
103108
ItemVectorImplicit = new[] { "abc_", "def", "ghi" }
104109
};
110+
#pragma warning restore CS0612 // Type or member is obsolete
105111

106112
byte[] data = root.AllocateAndSerialize();
107113

src/Tests/FlatSharpEndToEndTests/RawData/RawDataTableTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ public void DeprecatedValue_IgnoreOnRead(FlatBufferDeserializationOption option)
249249

250250
var parsed = DeprecatedItemTable.Serializer.Parse(data, option);
251251

252+
#pragma warning disable CS0612 // Type or member is obsolete
252253
Assert.AreEqual(0, parsed.Value);
254+
#pragma warning restore CS0612 // Type or member is obsolete
253255
Assert.AreEqual(255L, parsed.Other);
254256

255257
var nonDeprecatedParsed = NonDeprecatedItemTable.Serializer.Parse(data, option);
@@ -262,11 +264,13 @@ public void DeprecatedValue_IgnoreOnRead(FlatBufferDeserializationOption option)
262264
[DynamicData(nameof(DynamicDataHelper.DeserializationModes), typeof(DynamicDataHelper))]
263265
public void DeprecatedValue_IgnoreOnWrite(FlatBufferDeserializationOption option)
264266
{
267+
#pragma warning disable CS0612 // Type or member is obsolete
265268
var deprecatedTable = new DeprecatedItemTable
266269
{
267270
Value = 123,
268271
Other = 255,
269272
};
273+
#pragma warning restore CS0612 // Type or member is obsolete
270274

271275
byte[] data =
272276
{

src/Tests/FlatSharpEndToEndTests/TableMembers/TableMembersTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public void Byte(FlatBufferDeserializationOption option)
8484
[DynamicData(nameof(DynamicDataHelper.DeserializationModes), typeof(DynamicDataHelper))]
8585
public void Double(FlatBufferDeserializationOption option) => this.RunTest<double, DoubleTable>(3.14, option);
8686

87+
#pragma warning disable CS0612 // Type or member is obsolete
88+
8789
[TestMethod]
8890
[DynamicData(nameof(DynamicDataHelper.DeserializationModes), typeof(DynamicDataHelper))]
8991
public void String(FlatBufferDeserializationOption option)
@@ -125,6 +127,8 @@ public void String(FlatBufferDeserializationOption option)
125127
}
126128
}
127129

130+
#pragma warning restore CS0612 // Type or member is obsolete
131+
128132
private void RunTest<T, TTable>(T expectedDefault, FlatBufferDeserializationOption option)
129133
where T : struct
130134
where TTable : class, ITypedTable<T>, IFlatBufferSerializable<TTable>, new()

0 commit comments

Comments
 (0)