public async Task GetResponseMetadata_IgnoresProducesAttribute() { // Arrange var compilation = await GetResponseMetadataCompilation(); var controller = compilation.GetTypeByMetadataName($"{Namespace}.{nameof(GetResponseMetadata_ControllerActionWithAttributes)}"); var method = (IMethodSymbol)controller.GetMembers(nameof(GetResponseMetadata_ControllerActionWithAttributes.ActionWithProducesAttribute)).First(); var typeCache = new ApiControllerTypeCache(compilation); // Act var result = SymbolApiResponseMetadataProvider.GetResponseMetadata(typeCache, method); // Assert Assert.Empty(result); }
public async Task GetResponseMetadata_ReturnsEmptySequence_IfNoAttributesArePresent_ForPostAction() { // Arrange var compilation = await GetResponseMetadataCompilation(); var controller = compilation.GetTypeByMetadataName($"{Namespace}.{nameof(GetResponseMetadata_ControllerWithoutConvention)}"); var method = (IMethodSymbol)controller.GetMembers(nameof(GetResponseMetadata_ControllerWithoutConvention.PostPerson)).First(); var typeCache = new ApiControllerTypeCache(compilation); // Act var result = SymbolApiResponseMetadataProvider.GetResponseMetadata(typeCache, method); // Assert Assert.Empty(result); }
internal static IList <ApiResponseMetadata> GetResponseMetadata(ApiControllerTypeCache typeCache, IMethodSymbol methodSymbol) { var responseMetadataAttributes = methodSymbol.GetAttributes(typeCache.ProducesResponseTypeAttribute, inherit: true); var metadataItems = new List <ApiResponseMetadata>(); foreach (var attribute in responseMetadataAttributes) { var statusCode = GetStatusCode(attribute); var metadata = new ApiResponseMetadata(statusCode, attribute, convention: null); metadataItems.Add(metadata); } return(metadataItems); }
public async Task GetResponseMetadata_ReturnsValueFromProducesResponseType_WhenStatusCodeIsSpecifiedInConstructorWithResponseType() { // Arrange var compilation = await GetResponseMetadataCompilation(); var controller = compilation.GetTypeByMetadataName($"{Namespace}.{nameof(GetResponseMetadata_ControllerActionWithAttributes)}"); var method = (IMethodSymbol)controller.GetMembers(nameof(GetResponseMetadata_ControllerActionWithAttributes.ActionWithProducesResponseType_StatusCodeAndTypeInConstructor)).First(); var typeCache = new ApiControllerTypeCache(compilation); // Act var result = SymbolApiResponseMetadataProvider.GetResponseMetadata(typeCache, method); // Assert Assert.Collection( result, metadata => { Assert.Equal(202, metadata.StatusCode); Assert.NotNull(metadata.Attribute); Assert.Null(metadata.Convention); }); }
private async Task GetResponseMetadata_IgnoresInvalidOrUnsupportedAttribues(string typeName, string methodName) { // Arrange var compilation = await GetResponseMetadataCompilation(); var controller = compilation.GetTypeByMetadataName($"{Namespace}.{typeName}"); var method = (IMethodSymbol)controller.GetMembers(methodName).First(); var typeCache = new ApiControllerTypeCache(compilation); // Act var result = SymbolApiResponseMetadataProvider.GetResponseMetadata(typeCache, method); // Assert Assert.Collection( result, metadata => { Assert.Equal(200, metadata.StatusCode); Assert.NotNull(metadata.Attribute); Assert.Null(metadata.Convention); }); }