-
Notifications
You must be signed in to change notification settings - Fork 676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to hide property from displaying in Swagger? #1230
Comments
@yahyanajar It doesn't work. It still show ignored properties with [JsonIgnore] in request model on get actions. |
This works for me. |
JsonIgnore doesn't work for me |
Use the [DataContract] for the model and use [DataMember] for the properties which you want to show in Swagger. Any property which isn't marked as [DataMember] doesn't show up in your swagger definition. Let me know if this works or not. |
None of the methods above work for me. I am using .NET Core 2.2.
|
I've solved this issue before. You can create a custom attribute, then make a SchemaFilter that detects that attribute and removes the property from the Schema model. |
Have you tried |
[JsonIgnore] seems to be a more natural choice for this feature. |
That prevents it from being serialized. |
In FromQuery case, JsonIgnore is not working. Is there other tag we could use? Obsolete is not suitable, as it's not phase out. Doesn't have to be system attribute, does swagger provide any custom tag for this purpose? |
@arisliang Please check and try out my answer above, it's working for me. |
This works for .net core 3.0 also. |
In the end, DataContract and DataMember worked for us. |
Guys, we are having the same problem on net Core 3.1.10 Swagger is not respecting "[DataMember]" for displaying or not properties on [DataContract] classes. |
Same with JsonIgnore and/or IgnoreDataMember, works up to -rc4 |
I'm using .net core 3.1, using the [JsonIgnore] from System.Text.Json.Serialization works. (if it used from NewtonSoft.Json, it doesn't!) |
I added "services.AddSwaggerGenNewtonsoftSupport(); // explicit opt-in - needs to be placed after AddSwaggerGen()", but it still doesn't respect newtonsoft JsonIgnore. |
Thank you @shankarab, your solution works. |
@shankarab "I'm using .net core 3.1, using the [JsonIgnore] from System.Text.Json.Serialization works. (if it used from NewtonSoft.Json, it doesn't!)" Thanks for your answer. it work's for me. |
me as well |
I added "services.AddSwaggerGenNewtonsoftSupport(); // explicit opt-in - needs to be placed after AddSwaggerGen()", but it still doesn't respect newtonsoft JsonIgnore. |
Yes this helps I was using JsonIgnore from "Newtonsoft.Json" |
[JsonIgnore] will work if the property is in the request's body, as it prevents it from being deserialized. If the property comes from the query string, it will not work. In that case you could use [BindNever] from Microsoft.AspNetCore.Mvc.ModelBinding, this will make the model binder ignore that property and it will no longer be shown in swagger. |
If using .netcore below v3 and Swashbuckle 5.xxx either add services.AddSwaggerGenNewtonsoftSupport() or downgrade to Swashbuckle 4.xxx. If on .netcore 3 then use Swashbuckle 5.xxx and ensure you reference [JsonIgnore] from System.Text.Json.Serialization and not NewtonSoft.Json. This is because .netcore v3+ no longer uses NewtonSoft.Json as default |
FWIW, I wanted to do this for the request parameter and non of the above worked. I had to make my properties |
You my create a new Create a attribute
Create a filter
Add to Startup.cs
Use in your class
|
Latest version,
|
Yes but as mentioned above, that prevents the property from being serialized at all. |
This doesn't work in a scenario where you derive from a class and have a property with the "new" keyword to hide the base property. |
This doesn't seem to work if your controller argument is a contract class that contains a |
Hope this can help someone, but when using FromQuery there is no serialization happening as they are simply key/values, so to ignore fields when using FromQuery, decorate your property with the [BindNever] attribute. |
This solution worked for me, thanks a lot also, to add a greater solution put [IgnoreDataMember] to ignore some attributes or properties you want to ignore |
@breaker05 - Thanks, your solution was exactly what I needed for my project! |
If you want to use the Create a attribute
Create a schema processor
Add to Startup.cs
Use in your class
|
i find finall solution : it 's work for me |
It's working. Thank you. I've never heard of this attribute. |
It's working for my case using FromQuery. Thank you! |
For some reason this works with base classes but if you have a child class that also has [SwaggerIgnore] then it wont actually be ignored. |
As an update to my last comment, I figured out how to fix it using this updated version of the code. Not really sure why it is this way, but if your class is a child class it may have multiple The other change I made is to use And the final change is that I used If someone understands this better than me please explain because I don't really understand why it is this way. It's also possible that it's a slight bug |
Only the schema filter answer is correct. |
The above solutions didn't work for me, because I'm using a single class to collect all parameters. There are currently general issues collecting all parameters in a single class (dotnet/AspNetCore.Docs#29295). Options to hide are |
Hi, |
So... Did anyone managed to achieve this? :( |
didnt work :( |
cannot found ISchemaProcessor |
same bro |
I wanted to do it like this: public class User |
Try this:
Define an empty marker attribute:
Regiter in Program.cs:
String extension:
Use like this:
|
Swashbuckle.AspNetCore.Annotations 6.6.2 has SwaggerIgnore/SwaggerIgnoreAttribute, it works fine. Not sure when it was introduced. |
This doesn't work on properties |
I have a property that I want to serialize but dont want it visible in Swagger.
Example.
public long Id { get; set; }
Things I tried
Any help?
Thanks.
The text was updated successfully, but these errors were encountered: