ContractModelsAttributeCheck 1.0.1

This package has a SemVer 2.0.0 package version: 1.0.1+build.6.
There is a newer version of this package available.
See the version list below for details.
dotnet add package ContractModelsAttributeCheck --version 1.0.1
NuGet\Install-Package ContractModelsAttributeCheck -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ContractModelsAttributeCheck" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ContractModelsAttributeCheck --version 1.0.1
#r "nuget: ContractModelsAttributeCheck, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ContractModelsAttributeCheck as a Cake Addin
#addin nuget:?package=ContractModelsAttributeCheck&version=1.0.1

// Install ContractModelsAttributeCheck as a Cake Tool
#tool nuget:?package=ContractModelsAttributeCheck&version=1.0.1

Contract Models Attribute Checks

GitHub Actions Status

GitHub Actions Build History

What does this Package do?

This package enables you to find all used types in an OpenApi contract and check if every DTO has properties that are decorated with attributes like the JsonPropertyNameAttribute.

ValidationResults

Each property get's a AttributeCheckResult that looks like this:

    public class AttributeCheckResult
    {
        /// <summary>
        /// Fullname or type.ToString
        /// </summary>
        public string Fullname { get; } 
        public string PropertyName { get; } 
        /// <summary>
        /// validation message
        /// </summary>
        public string Message { get;} 
        /// <summary>
        /// true if at least one attribute is used
        /// </summary>
        public bool HasRequiredAttribute { get; }
        /// <summary>
        /// Type 
        /// </summary>
        public Type Type { get; } 

Use Case: Validate the types you want

The 'AttributeChecker' first visits types and their property types recursivly and checks afterwards which properties are violating your expected attribute list.

    public class AttributeCheckerTests
    {
        private readonly AttributeChecker _attributeChecker = new AttributeChecker();
        private readonly Type[] _attributes = new[] { typeof(JsonPropertyNameAttribute), typeof(JsonIgnoreAttribute) };

        [Fact]
        public void All_Properties_Should_Have_Attributes()
        {
            var results = _attributeChecker.CheckPropertiesForAttributes(typeof(TestClassWithAttributes), _attributes);
            var typesWithMissingAttributes = results.Where(w => !w.HasRequiredAttribute);
            typesWithMissingAttributes.Should().BeEmpty();
        }

Use Case: Validate OpenApi contract

ASP.NET Core offers an easy way to create integration tests. See also Link

With the 'ApiContractModelsAttributeChecker' class you can search for all used response and request types and check if they use the specified attributes on every property.

The code for this is as simply as this:

 public class ValidateContractModelsAttributesTest: IClassFixture<WebApplicationFactory<SampleWebApp.Startup>>
    {
        private readonly WebApplicationFactory<SampleWebApp.Startup> _factory;
        private readonly Type[] _attributes = new[] { typeof(JsonPropertyNameAttribute), typeof(JsonIgnoreAttribute) };

        public ValidateContractModelsAttributesTest(WebApplicationFactory<SampleWebApp.Startup> factory)
        {
            _factory = factory;
        }

        [Fact]
        public void V2Models_Have_Attributes()
        {
            // Arrange
            var apiProvider = _factory.Services.GetService<IApiDescriptionGroupCollectionProvider>();
            var apiInfoForVersion = apiProvider.ApiDescriptionGroups.Items.FirstOrDefault(w => w.GroupName == "v2");
            var modelFinder = new ApiContractModelsAttributeChecker();
            // Act
            var validationResults = modelFinder.CheckAttributesOfApiContractTypes(apiInfoForVersion, _attributes, "application/json");

            // Assert
            var typesWithMissingAttributes = validationResults.Where(w => !w.HasRequiredAttribute);
            typesWithMissingAttributes.Should().BeEmpty();
        }

By combining it with FluentAssertions you will get useful messages on failing tests.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 5,334 1/12/2022
1.0.4 341 4/11/2021
1.0.3 169 4/11/2021
1.0.1 122 4/5/2021
1.0.0 124 4/5/2021