'Remove Model properties in WebAPI POST but keep for GET in Swagger
I have a few properties that capture timestamps when data is inserted or updated in the database. I don't want these properties visible in Swagger in POST requests, how can I show these in GET requests but not in the POST endpoints?
public class BaseEntity
{
/// <summary>
/// Gets or Sets Date Created
/// </summary>
[DataMember(Name = "object_created", IsRequired = false)]
public DateTime DateCreated { get; set; }
/// <summary>
/// Gets or Sets Date Updated
/// </summary>
[DataMember(Name = "object_updated", IsRequired = false)]
public DateTime DateUpdated { get; set; }
}
Solution 1:[1]
See in topic "hide property from displaying": https://github.com/domaindrivendev/Swashbuckle.WebApi/issues/1230
Another way: you can create two model for that. I thing that way is the best.
Solution 2:[2]
You have to make the setter private, e.g.
public class BaseEntity
{
public string Name { get; set; }
public DateTime DateCreated { get; private set; }
public DateTime DateUpdated { get; private set; }
}
Then those properties will be hidden from POST and PUT operations.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Duy Anh |
Solution 2 | Matt Cassinelli |