How to configure Swashbuckle to ignore property on model
If you need to do this but without using JsonIgnore (maybe you still need to serialize/deserialize the property) then just create a custom attribute.
[AttributeUsage(AttributeTargets.Property)]
public class SwaggerExcludeAttribute : Attribute
{
}
Then a schema filter similar to Johng's
public class SwaggerExcludeFilter : ISchemaFilter
{
#region ISchemaFilter Members
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
{
if (schema?.properties == null || type == null)
return;
var excludedProperties = type.GetProperties()
.Where(t =>
t.GetCustomAttribute<SwaggerExcludeAttribute>()
!= null);
foreach (var excludedProperty in excludedProperties)
{
if (schema.properties.ContainsKey(excludedProperty.Name))
schema.properties.Remove(excludedProperty.Name);
}
}
#endregion
}
Don't forget to register the filter
c.SchemaFilter<SwaggerExcludeFilter>();
SpringFox - Hide certain fields in Swagger-ui that aren't required for the call to an endpoint
Use the @ApiModelProperty
(from io.swagger.annotations
)
- With
required
you define whether the property is mandatory or optional. - With
hidden
you can hide the property in Swagger UI, however if it's set it is returned anyway.
For example:
public class Car {
@ApiModelProperty(value = "id", required = true)
long id;
@ApiModelProperty(value = "wheels", required = true)
int wheels;
@ApiModelProperty(value = "name", hidden = true)
String name;
@ApiModelProperty(value = "type", hidden = true)
String type;
@ApiModelProperty(value = "canFly", hidden = true)
boolean canFly;
}
Since you use the same model for request and response (with the example above) the attributes in the documentation for GET endpoint will be also hidden (keep that in mind). If you don't want such behaviour then use separate models.
How to hide a property just in post request description of swagger using swashbuckle?
You can use the Swashbuckle.AspNetCore.Annotations
package, it allows you to mark that some properties are only displayed in the input parameters, and some are only displayed in the output.
In your case, you want to hide the AlertId
in the input parameter of the post, you just need to do this by the [SwaggerSchema]
:
public class Alert
{
[SwaggerSchema(ReadOnly = true)]
public string AlertId { get; set; }
public string Type { get; set; }
}
See more about it in the Documentation
In the ConfigureServices
method of Startup.cs
, enable annotations within in the Swagger config block:
services.AddSwaggerGen(c =>
{
...
c.EnableAnnotations();
});
Swagger 2 How to exclude a property from request example only
You can achieve the same using @ApiModelProperty(readOnly = true)
.
Allows a model property to be designated as read only. It will hide property from request and shows for a response only.
@ApiModelProperty(readOnly = true)
private Long id;
How to exclude fields from RequestBody - Swagger
You can hide parameters from swagger ui, by using
@ApiModelProperty(hidden = true)
with the fields you do not want to show. But the hidden fields will be hidden for every api in your swagger ui.
There is another way which requires you to create another dto. You can create another Dto and ask springfox show the new alternate dto instead of the original one.
@PostMapping(value = "/someapi")
@ApiImplicitParams({
@ApiImplicitParam(
name = "request",
dataTypeClass = AdditionalModel.class
)
})
public Response hello(@RequestBody Request request){
return new Response();
}
This approach will not make you change your code, but you will be required to create another dto.
Also you also need to register the AdditionalModel in your Docket Bean.
@Bean
public Docket petApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/")
.additionalModels(typeResolver.resolve(AdditionalModel.class));
}
Related Topics
How to Get a List of All the Implementations of an Interface Programmatically in Java
Springboot Data - No Property Registration Found
How to Find Index of First Vowel in a String
How to Fix Mass Assignment: Insecure Binder Configuration (Api Abuse, Structural) in Java
How to Do Sorting in Array List Without Using Collections in Java
Httpclienterrorexception 400 Null Using Resttemplate in Microservices
Spring Boot - Exception Encountered During Context Initialization. How to Know the Reason of Error
How to Tell a Mockito Mock Object to Return Something Different the Next Time It Is Called
Java - Replace New Line Character by \N
How to Store User Input into an Existing Array
How to Put a Scanner Input into an Array... for Example a Couple of Numbers
How to Pass the Same Object Across Multiple Classes Java
Splitting Data Inside Quotes and Comma Using Regex
Query in Repository With Limit
Spring @Transaction Method Call by the Method Within the Same Class, Does Not Work