Coverlet: Coverlet changes order of attributes

Created on 3 Jan 2019  路  8Comments  路  Source: coverlet-coverage/coverlet

Versions:
dotnet core 2.1
asp.net core 2.1.5
coverlet 2.5.0

Some of our tests that rely on the order of asp.net ValidationAttributes for method parameters fail when run with coverlet. Coverlet instrumentation seems to change the order of attributes.

e.g.

C# method.GetParameters().ForEach(p => { var validationAttributes = p.CustomAttributes.Where(a => typeof(ValidationAttribute).IsAssignableFrom(a.AttributeType)) .Select(a => a.AttributeType).ToList(); if (validationAttributes.All(a => a != typeof(ShouldBeFirstAttribute))) return; if (validationAttributes.First() != typeof(ShouldBeFirstAttribute)) { Assert.Fail("Action: {0}.{1}(...) should have {2} declared before other validation attributes", type.FullName, method.Name, typeof(ShouldBeFirstAttribute).Name); } });

This passes when run with \p:CodeCoverage=false

All 8 comments

I didn't think the .NET APIs makes any guarantees on the order in which attributes are returned:

The order in which attributes are specified in such a list, and the order in which sections attached to the same program entity are arranged, is not significant. For instance, the attribute specifications [A][B], [B][A], [A,B], and [B,A] are equivalent. (Source)

@carusology , yes, the C# language specfication treats them unordered.
And that's why we have tests to ensure/check the attributes are picked up in order and they seem to work on all dotnet core versions (tried 2.0 and above). Instrumentation should not change the order in which they are picked up.

Prior to release 0.10.3, Mono.Cecil was using an unstable sort when rewriting metadata (raised as https://github.com/jbevain/cecil/issues/558 and resolved).

Thanks @SteveGilham, will update to the latest Mono.Cecil version then

Did anyone find a workaround to keep the attributes in the same order?

@dorgold I opened a PR to upgrade Cecil lib https://github.com/tonerdo/coverlet/pull/508 to avoid reordering...you can use nightly untill next release https://github.com/tonerdo/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md

Thanks @MarcoRossignoli!
Just found a workaround till then, i'll use the Order property of ActionFilterAttribute:
https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-2.2#overriding-the-default-order

Great also better!

Was this page helpful?
0 / 5 - 0 ratings