我的控制器上的 swagger 缺少文档
Missing documentation from swagger on my Controller
我正在为招摇而苦苦挣扎。我对除 PUT 请求之外的所有方法生成的文档感到满意。
url 中的 userId 有详细记录,但没有其他对象 [FromBody]。 “示例值”中没有任何内容。
我不明白为什么它适用于 POST 和 GET 而不是 PUT。是 [FromBody] 导致问题吗?
此致
public class UpdateUserRequest
{
public Guid UpdatedBy { get; }
public Guid ProfilePictureId { get; }
public int? Size { get; }
public Guid CountryId { get; }
public int? CityId { get; }
public string UnitWeight { get; }
public string UnitSize { get; }
}
用户控制器:
[HttpPut("{userId}")]
public async Task<IActionResult> UpdateUser(Guid userId, [FromBody] UpdateUserRequest request)
{
await _userAccessModule.ExecuteCommandAsync(new UpdateUserCommand(
userId,
request.UpdatedBy,
request.ProfilePictureId,
request.Size,
request.CountryId,
request.CityId,
request.UnitWeight,
request.UnitSize));
return Ok();
}
Swagger 配置:
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "BXWeb API",
Version = "v1",
Description = "BXWeb API for modular monolith .NET application."
});
options.EnableAnnotations();
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var commentsFileName = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var commentsFile = Path.Combine(baseDirectory, commentsFileName);
options.IncludeXmlComments(commentsFile);
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri($"https://{auth0Configuration.Domain}/authorize?audience={auth0Configuration.Audience}", UriKind.Absolute),
TokenUrl = new Uri($"https://{auth0Configuration.Domain}/oauth/token?audience={auth0Configuration.Audience}", UriKind.Absolute),
Scopes = new Dictionary<string, string>
{
{"fullaccess:sport", "Global API for sport"}
}
}
},
In = ParameterLocation.Header,
Name = "Authorization",
Type = SecuritySchemeType.OAuth2
});
options.OperationFilter<SecurityRequirementsOperationFilter>();
});
我会测试你的场景和这个由只读属性引起的问题。
public class PocoModel
{
public Guid UpdatedBy { get; set; }
public Guid ProfilePictureId { get; set; }
public int? Size { get; set; }
public Guid CountryId { get; set; }
public int? CityId { get; }
public string UnitWeight { get; }
public string UnitSize { get; }
}
Output on swagger
我的解决方案;
public class PocoModel
{
public Guid UpdatedBy { get; set; }
public Guid ProfilePictureId { get; set; }
public int? Size { get; set; }
public Guid CountryId { get; set; }
private int? _cityId;
public int? CityId
{
get => _cityId;
set { }
}
public string UnitWeight { get; }
public string UnitSize { get; }
}
Output after solution on swagger
但我认为这是一个不合逻辑的解决方案。不合逻辑的事情是记录一个不能从客户端分配的变量。
希望对您有所帮助。
我正在为招摇而苦苦挣扎。我对除 PUT 请求之外的所有方法生成的文档感到满意。 url 中的 userId 有详细记录,但没有其他对象 [FromBody]。 “示例值”中没有任何内容。
我不明白为什么它适用于 POST 和 GET 而不是 PUT。是 [FromBody] 导致问题吗?
此致
public class UpdateUserRequest
{
public Guid UpdatedBy { get; }
public Guid ProfilePictureId { get; }
public int? Size { get; }
public Guid CountryId { get; }
public int? CityId { get; }
public string UnitWeight { get; }
public string UnitSize { get; }
}
用户控制器:
[HttpPut("{userId}")]
public async Task<IActionResult> UpdateUser(Guid userId, [FromBody] UpdateUserRequest request)
{
await _userAccessModule.ExecuteCommandAsync(new UpdateUserCommand(
userId,
request.UpdatedBy,
request.ProfilePictureId,
request.Size,
request.CountryId,
request.CityId,
request.UnitWeight,
request.UnitSize));
return Ok();
}
Swagger 配置:
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "BXWeb API",
Version = "v1",
Description = "BXWeb API for modular monolith .NET application."
});
options.EnableAnnotations();
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var commentsFileName = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var commentsFile = Path.Combine(baseDirectory, commentsFileName);
options.IncludeXmlComments(commentsFile);
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri($"https://{auth0Configuration.Domain}/authorize?audience={auth0Configuration.Audience}", UriKind.Absolute),
TokenUrl = new Uri($"https://{auth0Configuration.Domain}/oauth/token?audience={auth0Configuration.Audience}", UriKind.Absolute),
Scopes = new Dictionary<string, string>
{
{"fullaccess:sport", "Global API for sport"}
}
}
},
In = ParameterLocation.Header,
Name = "Authorization",
Type = SecuritySchemeType.OAuth2
});
options.OperationFilter<SecurityRequirementsOperationFilter>();
});
我会测试你的场景和这个由只读属性引起的问题。
public class PocoModel
{
public Guid UpdatedBy { get; set; }
public Guid ProfilePictureId { get; set; }
public int? Size { get; set; }
public Guid CountryId { get; set; }
public int? CityId { get; }
public string UnitWeight { get; }
public string UnitSize { get; }
}
Output on swagger
我的解决方案;
public class PocoModel
{
public Guid UpdatedBy { get; set; }
public Guid ProfilePictureId { get; set; }
public int? Size { get; set; }
public Guid CountryId { get; set; }
private int? _cityId;
public int? CityId
{
get => _cityId;
set { }
}
public string UnitWeight { get; }
public string UnitSize { get; }
}
Output after solution on swagger
但我认为这是一个不合逻辑的解决方案。不合逻辑的事情是记录一个不能从客户端分配的变量。
希望对您有所帮助。