Search code examples
c#swagger-ui

Is there a way in Swagger UI you can hide a certain field (example Employee ID) on POST but available on PATCH and GET?


So on POST, obviously we don't want the EmployeeID appearing on the BODY (since this will just get created) but on the PATCH and GET, I want to show the user they need to input EmployeeID on the body. This is just an example scenario, we could of course put the employee ID on the path, but I need some conditional field in the body.


Solution

  • You have to use different models for your different calls! Otherwise bad things could happen (code injection, etc.). To reduce code duplication you could use a class that holds all needed properties about your employee and then make a CreateEmployee class that derives from it and has additionally the id property. Also the classes used in swagger (or more exactly: all classes publicity exposed via your api) should be dumb DTO classes and not the classes used in your storage (like EF classes). After recieving the DTO class through REST call you have to verify and copy the information into the classes used in your business logic. The easiest way for this validation process is FluentValidation and for copy process is AutoMapper.