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
3 changes: 2 additions & 1 deletion src/Mvc/Mvc.Core/src/ApiConventionTypeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private static bool IsAllowedAttribute(object attribute)
{
return attribute is ProducesResponseTypeAttribute ||
attribute is ProducesDefaultResponseTypeAttribute ||
attribute is ApiConventionNameMatchAttribute;
attribute is ApiConventionNameMatchAttribute ||
attribute.GetType().FullName == "System.Runtime.CompilerServices.NullableContextAttribute";
Copy link
Member

@captainsafia captainsafia Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attribute.GetType().FullName == "System.Runtime.CompilerServices.NullableContextAttribute";
attribute.GetType().FullName == "System.Runtime.CompilerServices.NullableContextAttribute";

Is there a reason we can't do attribute is NullableContextAttribute here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NullableContextAttribute is a compiler code spit into the users code, example from SharpLab:

https://sharplab.io/#v2:EYLgtghglgdgNAExAagD4AEAsACAYge3wAoAhCAJwH5tgBKAWACgBvJgXyafQCZszymrRh0ZA===

}
}
37 changes: 36 additions & 1 deletion src/Mvc/Mvc.Core/test/ApiConventionMethodAttributeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Mvc.ApiExplorer;
Expand Down Expand Up @@ -46,6 +46,41 @@ public void Constructor_ThrowsIfTypeIsNotStatic()
expected);
}

public static class ConventionWithNullableContextAttribute
{
#nullable enable
public static void Get(Foo? foo) { }
#nullable restore
}

public class Foo { }

[Fact]
public void Convention_With_NullableContextAttribute_Has_NullableContextAttribute_On_Get_Method()
{
// Arrange
var type = typeof(ConventionWithNullableContextAttribute);
var method = type.GetMethod(nameof(ConventionWithNullableContextAttribute.Get));
var attributes = method.GetCustomAttributes(false);

// Act & Assert
Assert.Contains(attributes, (a) =>
{
var attributeType = a.GetType();
return attributeType.FullName == "System.Runtime.CompilerServices.NullableContextAttribute";
});
}

[Fact]
public void Convention_With_NullableContextAttribute_With_NullableContextAttribute_On_Get_Method_Does_Not_Throw()
{
// Arrange
var methodName = typeof(ConventionWithNullableContextAttribute).FullName + '.' + nameof(ConventionWithNullableContextAttribute.Get);
var attribute = typeof(ProducesAttribute);

new ApiConventionMethodAttribute(typeof(ConventionWithNullableContextAttribute), nameof(ConventionWithNullableContextAttribute.Get));
}

[Fact]
public void Constructor_ThrowsIfMethodCannotBeFound()
{
Expand Down