0

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; }
    }
MK998392
  • 15
  • 3
  • You might need to make these properties read-only. In OpenAPI 2.0/3.0, [`readOnly`](https://stackoverflow.com/a/40843802/113116) means this property appears in _responses_ (e.g. GET responses) but not included in _requests_ (e.g. POST/PUT). – Helen Mar 12 '21 at 07:45

2 Answers2

0

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.

Duy Anh
  • 11
  • 3
0

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.