Ejemplo n.º 1
0
        public void Apply(Operation operation, OperationFilterContext context)
        {
            var controllerActionDescriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;

            if (controllerActionDescriptor == null)
            {
                return;
            }

            var actionName = controllerActionDescriptor.ActionName;

            var resourceName = controllerActionDescriptor.ControllerName.TrimEnd('s');

            switch (actionName)
            {
            case "Create":
                operation.Summary = operation.Summary ?? $"Creates a {resourceName}";

                SwaggerUtils.CheckSet(operation, "200", new Response {
                    Description = $"A {resourceName} was created"
                });
                SwaggerUtils.CheckSet(operation, "400", new Response {
                    Description = $"Failed creating a {resourceName}"
                });

                break;

            case "GetAll":

                operation.Summary = operation.Summary ?? $"Returns all {resourceName}s";
                break;

            case "GetById":
                operation.Summary = operation.Summary ?? $"Retrieves a {resourceName} by unique id";
                SwaggerUtils.CheckSet(operation, "404", new Response {
                    Description = $"Could not find the requested {resourceName}"
                });
                break;

            case "Update":
                operation.Summary = operation.Summary ?? $"Updates a {resourceName} by unique id";
                operation.Parameters[0].Description = $"a unique id for the {resourceName}";
                if (operation.Parameters.Count >= 2)
                {
                    operation.Parameters[1].Description = $"a {resourceName} representation";
                }

                SwaggerUtils.CheckSet(operation, "404", new Response {
                    Description = $"Could not find the requested {resourceName}"
                });

                break;

            case "Delete":
                operation.Summary = operation.Summary ?? $"Deletes a {resourceName} by unique id";
                operation.Parameters[0].Description = $"a unique id for the {resourceName}";
                break;
            }
        }
 public void Apply(Operation operation, OperationFilterContext context)
 {
     SwaggerUtils.CheckSet(operation, Constants.StatusCodes.ModelErrorStatusCode.ToString(), new Response
     {
         Description = Constants.Messages.InvalidModelMsg
     });
     SwaggerUtils.CheckSet(operation, Constants.StatusCodes.ErrorUnexpcetedStatusCode.ToString(), new Response
     {
         Description = Constants.Messages.UnexpectedErrorMsg
     });
 }
        public void Apply(Operation operation, OperationFilterContext context)
        {
            var controllerActionDescriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;

            if (controllerActionDescriptor == null)
            {
                return;
            }


            var actionName = controllerActionDescriptor.ActionName;

            var resourceName = controllerActionDescriptor.ControllerName.TrimEnd('s');

            // Policy names map to scopes
            var controllerScopes = context.ApiDescription.ControllerAttributes()
                                   .OfType <AuthorizeAttribute>()
                                   .Select(attr => attr.Policy);

            var actionScopes = context.ApiDescription.ActionAttributes()
                               .OfType <AuthorizeAttribute>()
                               .Select(attr => attr.Policy);

            var allowedAnon = context.ApiDescription.ActionAttributes().OfType <AllowAnonymousAttribute>().Any();

            var requireScopes = controllerScopes.Union(actionScopes).Distinct();

            if (!allowedAnon)
            {
                if (operation.Parameters == null)
                {
                    operation.Parameters = new List <IParameter>();
                }
                SwaggerUtils.CheckSet(operation, Constants.StatusCodes.AuthorizationErrorStatusCode.ToString(), new Response
                {
                    Description = Constants.Messages.UnauthorizedMsg
                });
            }
        }