public async Task GetResponseMetadata_ReturnsValuesFromApiConventionMethodAttribute()
        {
            // Arrange
            var compilation = await GetResponseMetadataCompilation();

            var controller  = compilation.GetTypeByMetadataName($"{Namespace}.{nameof(GetResponseMetadata_ControllerActionWithAttributes)}");
            var method      = (IMethodSymbol)controller.GetMembers(nameof(GetResponseMetadata_ControllerActionWithAttributes.GetResponseMetadata_ReturnsValuesFromApiConventionMethodAttribute)).First();
            var symbolCache = new ApiControllerSymbolCache(compilation);

            // Act
            var result = SymbolApiResponseMetadataProvider.GetDeclaredResponseMetadata(symbolCache, method, Array.Empty <AttributeData>());

            // Assert
            Assert.Collection(
                result,
                metadata =>
            {
                Assert.Equal(200, metadata.StatusCode);
                Assert.NotNull(metadata.Attribute);
            },
                metadata =>
            {
                Assert.Equal(404, metadata.StatusCode);
                Assert.NotNull(metadata.Attribute);
            });
        }
        private async Task GetStatusCodeTest(string actionName, int expected)
        {
            var compilation = await GetResponseMetadataCompilation();

            var controller = compilation.GetTypeByMetadataName($"{Namespace}.{nameof(GetResponseMetadata_ControllerActionWithAttributes)}");
            var method     = (IMethodSymbol)controller.GetMembers(actionName).First();
            var attribute  = method.GetAttributes().First();

            var statusCode = SymbolApiResponseMetadataProvider.GetStatusCode(attribute);

            Assert.Equal(expected, statusCode);
        }
        public async Task GetDefaultStatusCode_ReturnsValueDefinedUsingHttpStatusCast()
        {
            // Arrange
            var compilation = await GetCompilation("GetDefaultStatusCodeTest");

            var attribute = compilation.GetTypeByMetadataName(typeof(TestActionResultUsingHttpStatusCodeCast).FullName).GetAttributes()[0];

            // Act
            var actual = SymbolApiResponseMetadataProvider.GetDefaultStatusCode(attribute);

            // Assert
            Assert.Equal(302, actual);
        }
示例#4
0
        private void InitializeWorker(CompilationStartAnalysisContext compilationStartAnalysisContext, ApiControllerSymbolCache symbolCache)
        {
            compilationStartAnalysisContext.RegisterSyntaxNodeAction(syntaxNodeContext =>
            {
                var methodSyntax  = (MethodDeclarationSyntax)syntaxNodeContext.Node;
                var semanticModel = syntaxNodeContext.SemanticModel;
                var method        = semanticModel.GetDeclaredSymbol(methodSyntax, syntaxNodeContext.CancellationToken);

                if (!ShouldEvaluateMethod(symbolCache, method))
                {
                    return;
                }

                var conventionAttributes     = GetConventionTypeAttributes(symbolCache, method);
                var expectedResponseMetadata = SymbolApiResponseMetadataProvider.GetResponseMetadata(symbolCache, method, conventionAttributes);
                var actualResponseMetadata   = new HashSet <int>();

                var context = new ApiConventionContext(
                    symbolCache,
                    syntaxNodeContext,
                    expectedResponseMetadata,
                    actualResponseMetadata);

                var hasUndocumentedStatusCodes = false;
                foreach (var returnStatementSyntax in methodSyntax.DescendantNodes(_shouldDescendIntoChildren).OfType <ReturnStatementSyntax>())
                {
                    hasUndocumentedStatusCodes |= VisitReturnStatementSyntax(context, returnStatementSyntax);
                }

                if (hasUndocumentedStatusCodes)
                {
                    // If we produced analyzer warnings about undocumented status codes, don't attempt to determine
                    // if there are documented status codes that are missing from the method body.
                    return;
                }

                for (var i = 0; i < expectedResponseMetadata.Count; i++)
                {
                    var expectedStatusCode = expectedResponseMetadata[i].StatusCode;
                    if (!actualResponseMetadata.Contains(expectedStatusCode))
                    {
                        context.SyntaxNodeContext.ReportDiagnostic(Diagnostic.Create(
                                                                       DiagnosticDescriptors.MVC1006_ActionDoesNotReturnDocumentedStatusCode,
                                                                       methodSyntax.Identifier.GetLocation(),
                                                                       expectedStatusCode));
                    }
                }
            }, SyntaxKind.MethodDeclaration);
        }
        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 symbolCache = new ApiControllerSymbolCache(compilation);

            // Act
            var result = SymbolApiResponseMetadataProvider.GetDeclaredResponseMetadata(symbolCache, method, Array.Empty <AttributeData>());

            // 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 symbolCache = new ApiControllerSymbolCache(compilation);

            // Act
            var result = SymbolApiResponseMetadataProvider.GetDeclaredResponseMetadata(symbolCache, method, Array.Empty <AttributeData>());

            // Assert
            Assert.Empty(result);
        }
        public async Task GetResponseMetadata_IgnoresCustomResponseTypeMetadataProvider()
        {
            // Arrange
            var compilation = await GetResponseMetadataCompilation();

            var controller = compilation.GetTypeByMetadataName($"{Namespace}.{nameof(GetResponseMetadata_ControllerActionWithAttributes)}");
            var method     = (IMethodSymbol)controller.GetMembers(nameof(GetResponseMetadata_ControllerActionWithAttributes.ActionWithCustomApiResponseMetadataProvider)).First();
            var typeCache  = new ApiControllerTypeCache(compilation);

            // Act
            var result = SymbolApiResponseMetadataProvider.GetResponseMetadata(typeCache, method);

            // Assert
            Assert.Empty(result);
        }
        private async Task <ActualApiResponseMetadata?> RunInspectReturnStatementSyntax(string source, string test)
        {
            var project     = DiagnosticProject.Create(GetType().Assembly, new[] { source });
            var compilation = await project.GetCompilationAsync();

            var symbolCache = new ApiControllerSymbolCache(compilation);

            var returnType = compilation.GetTypeByMetadataName($"{Namespace}.{test}");
            var syntaxTree = returnType.DeclaringSyntaxReferences[0].SyntaxTree;

            var method          = (IMethodSymbol)returnType.GetMembers().First();
            var methodSyntax    = syntaxTree.GetRoot().FindNode(method.Locations[0].SourceSpan);
            var returnStatement = methodSyntax.DescendantNodes().OfType <ReturnStatementSyntax>().First();

            return(SymbolApiResponseMetadataProvider.InspectReturnStatementSyntax(
                       symbolCache,
                       compilation.GetSemanticModel(syntaxTree),
                       returnStatement,
                       CancellationToken.None));
        }
        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 symbolCache = new ApiControllerSymbolCache(compilation);

            // Act
            var result = SymbolApiResponseMetadataProvider.GetDeclaredResponseMetadata(symbolCache, method, Array.Empty <AttributeData>());

            // 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 symbolCache = new ApiControllerSymbolCache(compilation);

            // Act
            var result = SymbolApiResponseMetadataProvider.GetDeclaredResponseMetadata(symbolCache, method, Array.Empty <AttributeData>());

            // Assert
            Assert.Collection(
                result,
                metadata =>
            {
                Assert.Equal(200, metadata.StatusCode);
                Assert.NotNull(metadata.Attribute);
                Assert.Null(metadata.Convention);
            });
        }
        public async Task GetResponseMetadata_ReturnsValueFromCustomProducesResponseType()
        {
            // Arrange
            var compilation = await GetResponseMetadataCompilation();

            var controller = compilation.GetTypeByMetadataName($"{Namespace}.{nameof(GetResponseMetadata_ControllerActionWithAttributes)}");
            var method     = (IMethodSymbol)controller.GetMembers(nameof(GetResponseMetadata_ControllerActionWithAttributes.ActionWithCustomProducesResponseTypeAttributeWithArguments)).First();
            var typeCache  = new ApiControllerTypeCache(compilation);

            // Act
            var result = SymbolApiResponseMetadataProvider.GetResponseMetadata(typeCache, method);

            // Assert
            Assert.Collection(
                result,
                metadata =>
            {
                Assert.Equal(201, metadata.StatusCode);
                Assert.NotNull(metadata.Attribute);
                Assert.Null(metadata.Convention);
            });
        }
示例#12
0
        private void InitializeWorker(CompilationStartAnalysisContext compilationStartAnalysisContext, ApiControllerSymbolCache symbolCache)
        {
            compilationStartAnalysisContext.RegisterSyntaxNodeAction(syntaxNodeContext =>
            {
                var cancellationToken = syntaxNodeContext.CancellationToken;
                var methodSyntax      = (MethodDeclarationSyntax)syntaxNodeContext.Node;
                var semanticModel     = syntaxNodeContext.SemanticModel;
                var method            = semanticModel.GetDeclaredSymbol(methodSyntax, syntaxNodeContext.CancellationToken);

                if (!ApiControllerFacts.IsApiControllerAction(symbolCache, method))
                {
                    return;
                }

                var conventionAttributes     = GetConventionTypeAttributes(symbolCache, method);
                var declaredResponseMetadata = SymbolApiResponseMetadataProvider.GetDeclaredResponseMetadata(symbolCache, method, conventionAttributes);

                var hasUnreadableStatusCodes   = SymbolApiResponseMetadataProvider.TryGetActualResponseMetadata(symbolCache, semanticModel, methodSyntax, cancellationToken, out var actualResponseMetadata);
                var hasUndocumentedStatusCodes = false;
                foreach (var item in actualResponseMetadata)
                {
                    var location = item.ReturnStatement.GetLocation();

                    if (item.IsDefaultResponse)
                    {
                        if (!(HasStatusCode(declaredResponseMetadata, 200) || HasStatusCode(declaredResponseMetadata, 201)))
                        {
                            hasUndocumentedStatusCodes = true;
                            syntaxNodeContext.ReportDiagnostic(Diagnostic.Create(
                                                                   DiagnosticDescriptors.MVC1005_ActionReturnsUndocumentedSuccessResult,
                                                                   location));
                        }
                    }
                    else if (!HasStatusCode(declaredResponseMetadata, item.StatusCode))
                    {
                        hasUndocumentedStatusCodes = true;
                        syntaxNodeContext.ReportDiagnostic(Diagnostic.Create(
                                                               DiagnosticDescriptors.MVC1004_ActionReturnsUndocumentedStatusCode,
                                                               location,
                                                               item.StatusCode));
                    }
                }

                if (hasUndocumentedStatusCodes || hasUnreadableStatusCodes)
                {
                    // If we produced analyzer warnings about undocumented status codes, don't attempt to determine
                    // if there are documented status codes that are missing from the method body.
                    return;
                }

                for (var i = 0; i < declaredResponseMetadata.Count; i++)
                {
                    var expectedStatusCode = declaredResponseMetadata[i].StatusCode;
                    if (!HasStatusCode(actualResponseMetadata, expectedStatusCode))
                    {
                        syntaxNodeContext.ReportDiagnostic(Diagnostic.Create(
                                                               DiagnosticDescriptors.MVC1006_ActionDoesNotReturnDocumentedStatusCode,
                                                               methodSyntax.Identifier.GetLocation(),
                                                               expectedStatusCode));
                    }
                }
            }, SyntaxKind.MethodDeclaration);
        }
        private void InitializeWorker(CompilationStartAnalysisContext context, ApiControllerSymbolCache symbolCache)
        {
            context.RegisterOperationAction(operationAnalysisContext =>
            {
                var ifOperation = (IConditionalOperation)operationAnalysisContext.Operation;
                if (!(ifOperation.Syntax is IfStatementSyntax ifStatement))
                {
                    return;
                }

                if (ifOperation.WhenTrue == null || ifOperation.WhenFalse != null)
                {
                    // We only support expressions of the format
                    // if (!ModelState.IsValid)
                    // or
                    // if (ModelState.IsValid == false)
                    // If the conditional is misisng a true condition or has an else expression, skip this operation.
                    return;
                }

                var parent = ifOperation.Parent;
                if (parent?.Kind == OperationKind.Block)
                {
                    parent = parent?.Parent;
                }

                if (parent?.Kind != OperationKind.MethodBodyOperation)
                {
                    // Only support top-level ModelState IsValid checks.
                    return;
                }

                var trueStatement = UnwrapSingleStatementBlock(ifOperation.WhenTrue);
                if (trueStatement.Kind != OperationKind.Return)
                {
                    // We need to verify that the if statement does a ModelState.IsValid check and that the block inside contains
                    // a single return statement returning a 400. We'l get to it in just a bit
                    return;
                }

                if (!(parent.Syntax is MethodDeclarationSyntax methodSyntax))
                {
                    return;
                }

                var semanticModel = operationAnalysisContext.Compilation.GetSemanticModel(methodSyntax.SyntaxTree);
                var methodSymbol  = semanticModel.GetDeclaredSymbol(methodSyntax, operationAnalysisContext.CancellationToken);

                if (!ApiControllerFacts.IsApiControllerAction(symbolCache, methodSymbol))
                {
                    // Not a ApiController. Nothing to do here.
                    return;
                }

                if (!IsModelStateIsValidCheck(symbolCache, ifOperation.Condition))
                {
                    return;
                }

                var returnOperation = (IReturnOperation)trueStatement;

                var returnValue = returnOperation.ReturnedValue;
                if (returnValue == null ||
                    !symbolCache.IActionResult.IsAssignableFrom(returnValue.Type))
                {
                    return;
                }

                var returnStatementSyntax = (ReturnStatementSyntax)returnOperation.Syntax;
                var actualMetadata        = SymbolApiResponseMetadataProvider.InspectReturnStatementSyntax(
                    symbolCache,
                    semanticModel,
                    returnStatementSyntax,
                    operationAnalysisContext.CancellationToken);

                if (actualMetadata == null || actualMetadata.Value.StatusCode != 400)
                {
                    return;
                }

                var additionalLocations = new[]
                {
                    ifStatement.GetLocation(),
                    returnStatementSyntax.GetLocation(),
                };

                operationAnalysisContext.ReportDiagnostic(
                    Diagnostic.Create(
                        DiagnosticDescriptors.MVC1007_ApiActionsDoNotRequireExplicitModelValidationCheck,
                        ifStatement.GetLocation(),
                        additionalLocations: additionalLocations));
            }, OperationKind.Conditional);
        }