0

I am trying to make a .net web-api application with default model validation and automatic documentation generation using swashbuckle swagger. I faced such problem. I have a model, but depending on the action, the same field can be required or optional. For example I have something like this - the full model that I will return to api user.

public class Entity
{
   public string ID { get; set; }
   public string Name { get; set; }
   public string Manufacturer { get; set; }
   public string Family { get; set; }
   public string CatalogNumber { get; set; }
}

So when user want to POST new Entity then all fields are required except for ID because it will be generated automatically. So I don't want to see in swagger documentation request example ID field at all.

POST

In other hand when user want to update an Entity and use PUT then all fields should be optional except for ID in this case it should have [Required] attribute.

PUT

At this question stackoverflowers suggest to create model per action. But it will make documentation's section Schemas very bloated as well as client's side code when they will use swagger codegen. I really want to avoid it.

So, basically, I have one model, and the very same property should have a [Required] attribute, but for some actions it does not, and in the third case it should not be displayed at all in the documentation.

I wanted to ask if anyone know an approach for this kind of situation.

BeygelTheP
  • 57
  • 5

0 Answers0