Reputation: 483
I'm struggling with swagger. I'm happy with the generated documentation for all my method except for the PUT requests. The userId in the url is well documented but no the other object [FromBody]. There is nothing in "Example value".
I don't see why it would works for the POST and GET but not the PUT. Is it the [FromBody] which is causing problem ?
Regards
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; }
}
UserController:
[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 config:
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>();
});
Upvotes: 0
Views: 899
Reputation: 604
I will test your scenario and this problem caused by readonly properties.
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; }
}
My solution;
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
But I think this is an illogical solution. The illogical thing is to document a variable that cannot be assigned from the client side.
I hope its helps.
Upvotes: 1