.NET API hide a property in base class from swagger

Viewed 239

I'm working on a REST API in C# that is a new version (call it 2) of an existing API . It has been decided in the interest of time, that the new version's models should inherit from the old version. Someone else had already set that up, and our new controllers for version 2. Now we are going through making the required changes for the new version. Sometimes 2 needs field 1 didn't have or requires things that were optional in one. That part is fine. But in some cases items that were required in the old version (for example pretend it's middle name) are not required in this version, and in fact are supposed to be completely not part of the request.

We have properties in the version 2 model marked as new with the same name as inherited version 1, to hide the one in the base class, and then put a [JsonIgnore] on that property. So for example, you have a Person model, where Middle Name is required, then you Version2, Person model inherits from Version 1 person model, and we want Version 2 to not require middle name, not take in a middle name, not have it showing up in swagger or anything similar.

This seemed to be working..then I looked at swagger and saw the field was still there.
How can I do this?

And yes..a refactor would be better, but in the interest of time it has been decided at higher levels we aren't doing that.

0 Answers
Related