控制器中的多个HttpPost方法可防止swagger .json生成

时间:2018-06-07 06:12:26

标签: c# asp.net-core swagger swashbuckle

我在示例C#ASP.NET Core 2.0 Api中有一个Authors控制器,我使用Swashbuckle生成Swagger .json。

当我在AuthorsController中包含以下两个方法时,.json不会生成

    [HttpPost(Name = "CreateAuthor")]
     public IActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
    {
      return null //for simplicity repeating the problem
    }

[HttpPost(Name = "CreateAuthorWithDateOfDeath")]
public IActionResult CreateAuthorWithDateOfDeath(
        [FromBody] AuthorForCreationWithDateOfDeathDto author)
    {
        return null 
    }

然后,当我尝试访问Swagger UI时,我得到了

无法加载API定义。    undefined ./v1/swagger.json

enter image description here

但是如果我注释掉任何一种方法,.json就会生成。

在Startup ConfigureServices中我有

services.AddSwaggerGen(c => {
    c.OperationFilter<AuthorizationHeaderParameterOperationFilter>();

    c.SwaggerDoc("v1", new Info
    {
        Version = "v1",
        Title = "track3 API",
        Description = "ASP.NET Core Web API",
        TermsOfService = "None",
        Contact = new Contact
        {
            Name = "my name",
            Email = "myemail@mydomain.com"
        }
    });

});

,其中

public class AuthorizationHeaderParameterOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        var filterPipeline = context.ApiDescription.ActionDescriptor.FilterDescriptors;
        var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is AuthorizeFilter);
        var allowAnonymous = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is IAllowAnonymousFilter);

        if (isAuthorized && !allowAnonymous)
        {
            if (operation.Parameters == null)
                operation.Parameters = new List<IParameter>();

            operation.Parameters.Add(new NonBodyParameter
            {
                Name = "Authorization",
                In = "header",
                Description = "access token",
                Required = true,
                Type = "string"
            });
        }
    }
}

在配置中我有

        app.UseSwaggerUI(c =>
        {
            c.RoutePrefix = "api-docs";
            c.SwaggerEndpoint("./v1/swagger.json", "Api v1");
        });

为什么会这样?

[更新]

控制器中有第二种类似的方法。 如果我注释掉第二种方法并取消注释第一种方法,则会生成.json。 两种方法都不会出现在Swagger

这是Dto的代码

public class AuthorForCreationDto
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTimeOffset DateOfBirth { get; set; }
    public string Genre { get; set; }

    public ICollection<BookForCreationDto> Books { get; set; }
    = new List<BookForCreationDto>();
}

public class AuthorForCreationWithDateOfDeathDto
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTimeOffset DateOfBirth { get; set; }
    public DateTimeOffset? DateOfDeath { get; set; }
    public string Genre { get; set; }
}

public class BookForCreationDto : BookForManipulationDto
{
}

public abstract class BookForManipulationDto
{
    [Required(ErrorMessage = "You should fill out a title.")]
    [MaxLength(100, ErrorMessage = "The title shouldn't have more than 100 characters.")]
    public string Title { get; set; }

    [MaxLength(500, ErrorMessage = "The description shouldn't have more than 500 characters.")]
    public virtual string Description { get; set; }
}

0 个答案:

没有答案