13

I am new to ASP.NET Core and this question looks simple but I couldn't find a proper solution online. So here's the problem.
This is the structure of the class that I am using.

public class Alert
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public string AlertId { get; set; }
    public string Type { get; set; }

}

This is the description for the Post request API in swagger.

{
  "alertId": "string",
  "type": "string"
}

Since I am using [DatabaseGenerated(DatabaseGeneratedOption.Identity)] annotation alertId in the post request is optional. My aim is to hide alertId from the post request description only.
I am using ASP.NET Core 3.1, EF Core(3.1.1) and Swashbuckle.AspDotNetCore(5.1.0).
Please Help.
Thank you.

Ivan Biji
  • 148
  • 1
  • 1
  • 5
  • 2
    It is better not to return your 'Alert' object to the client. You can create a separate model (for example 'AlertDto') which would contain only the properties you want to return in a response. – Ivan Apr 14 '20 at 20:05
  • As Ivan has said, create a class that models the data you need from the user. After getting the data, you can map it to to the `Alert` entity. – bolkay Apr 14 '20 at 21:08
  • As Ivan has said, create a class that models the data you need from the user. After getting the data, you can map it to to the `Alert` entity. – bolkay Apr 14 '20 at 21:09
  • I have been trying to do this for a while now. I wanted to hide/exclude the ID property from HttpPost using a custom HttpPostIgnore-attribute. But when I think about it, it would be confusing if I have a Schema (model) in Swagger act different across methods from how it is documented in the Schema section. I dont want my API to be too out of standard, so I am discontinuing my attempt, and create separate DTOs for different methods instead. Just wanted to put my perspective out there :) – mathkid91 Jun 16 '21 at 12:33
  • hi @mathkid91 , is my answer what you want? – Hanabi Jul 17 '21 at 16:23

3 Answers3

18

You can use the Swashbuckle.AspNetCore.Annotations package, it allows you to mark that some properties are only displayed in the input parameters, and some are only displayed in the output.

In your case, you want to hide the AlertId in the input parameter of the post, you just need to do this by the [SwaggerSchema]:

public class Alert
{
    [SwaggerSchema(ReadOnly = true)]
    public string AlertId { get; set; }
    public string Type { get; set; }
}

See more about it in the Documentation

In the ConfigureServices method of Startup.cs, enable annotations within in the Swagger config block:

services.AddSwaggerGen(c =>
{
   ...

   c.EnableAnnotations();
});
Hanabi
  • 577
  • 4
  • 9
  • Hi, thanks for the tag! It sounds like what I want, but its not working as expected in any of my projects - at least not by just installing the NuGet and adding the tag. I will try follow the documentation step by step and check it out again, in case I have done something funny in my code, or have missed some SwaggerSchema initialization. – mathkid91 Jul 26 '21 at 13:06
  • 1
    @mathkid91 yes, initialization with `c.EnableAnnotations()` – Hanabi Aug 02 '21 at 06:30
  • 1
    Thank you! Everyone suggests using JsonIgnore, but that prevents serialization, so even your GET requests hide the field. Glad to see there was an actual solution out there, and a totally straight-forward one at that. – Oliver Nicholls Jan 17 '23 at 21:53
3

You can add the [JsonIgnore] attribute to the AlertId field to ensure that the post request will not get the content of the AlertId.

  public class Alert
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [JsonIgnore]
        public string AlertId { get; set; }
        public string Type { get; set; }

    }

Here it the test result:

enter image description here

LouraQ
  • 6,443
  • 2
  • 6
  • 16
1

If you are using AzureFunctions.Extensions.Swashbuckle then simply changing the Setter to internal will prevent it from showing in the Post request schema:

public class Alert
{
    public string AlertId { get; internal set; }
    public string Type { get; set; }
}
Michael Rodrigues
  • 5,057
  • 3
  • 25
  • 51