예제 #1
0
        private ClassDeclarationSyntax AddSecuritySchemes(ClassDeclarationSyntax target, ILocatedOpenApiElement <OpenApiOperation> operation)
        {
            var className = IdentifierName(target.Identifier);

            var attributes = new List <AttributeSyntax>();

            foreach (var securityRequirement in operation.GetSecurityRequirements())
            {
                ILocatedOpenApiElement <OpenApiSecurityScheme>[] securitySchemes = securityRequirement.GetSecuritySchemes()
                                                                                   .Select(p => p.Key)
                                                                                   .ToArray();

                attributes.Add(Attribute(_authenticationNamespace.SecuritySchemeSetAttribute)
                               .AddArgumentListArguments(
                                   securitySchemes.Select(securityScheme =>
                                                          AttributeArgument(TypeOfExpression(_context.TypeNameProvider.GetName(securityScheme))))
                                   .ToArray()));

                if (securitySchemes.Length == 1)
                {
                    TypeSyntax schemeTypeName = _context.TypeNameProvider.GetName(securitySchemes[0]);

                    target = target.AddMembers(MethodDeclaration(className, "WithAuthenticator")
                                               .AddModifiers(Token(SyntaxKind.PublicKeyword))
                                               .AddParameterListParameters(
                                                   Parameter(Identifier("authenticator"))
                                                   .WithType(schemeTypeName))
                                               .WithBody(Block(
                                                             ExpressionStatement(AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
                                                                                                      IdentifierName("Authenticator"),
                                                                                                      IdentifierName("authenticator"))),
                                                             ReturnStatement(ThisExpression()))));
                }
                else if (securitySchemes.Length > 1)
                {
                    target = target.AddMembers(MethodDeclaration(className, "WithAuthenticator")
                                               .AddModifiers(Token(SyntaxKind.PublicKeyword))
                                               .AddParameterListParameters(
                                                   securitySchemes
                                                   .Select((p, index) =>
                                                           Parameter(Identifier($"authenticator{index}"))
                                                           .WithType(_context.TypeNameProvider.GetName(p)))
                                                   .ToArray())
                                               .WithBody(Block(
                                                             ExpressionStatement(AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
                                                                                                      IdentifierName("Authenticator"),
                                                                                                      ObjectCreationExpression(_authenticationNamespace.MultiAuthenticator)
                                                                                                      .AddArgumentListArguments(
                                                                                                          securitySchemes
                                                                                                          .Select((_, index) => Argument(IdentifierName($"authenticator{index}")))
                                                                                                          .ToArray()))),
                                                             ReturnStatement(ThisExpression()))));
                }
            }

            if (attributes.Count > 0)
            {
                target = target.AddAttributeLists(AttributeList(null, SeparatedList(attributes)));
            }

            return(target);
        }