'dotnet api request / response classes

I am currently looking for an elegant way to decide between request and response objects. Let's say I create an animal and my API generates the ID for the animal. I don't want someone to provide the ID in the body of the request. I also don't want the ID to be displayed as a request example, e.g. in a Swagger UI.

If I return the created animal afterwards, then the ID should be given and also Swagger UI should show the ID for Responses as an example.

My only idea for this is to create two classes for the animal:

  • PetRequest & Pet (Response) and to cast the PetRequest to Pet.

Is there a better way to do this?

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source