Last active
May 11, 2022 10:31
-
-
Save rgudkov-uss/564a7faf92d90b7f2257b10000ac2515 to your computer and use it in GitHub Desktop.
Filter to include odata parameters into Swagger UI (checked on AspNetCore 6.0). This is a modification of @jacobmohl code from https://gist.github.com/jacobmohl/a7eb2f3b508b61a2bd04ef0fe6d6e1de
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc.Controllers; | |
using Microsoft.AspNetCore.OData.Query; | |
using Microsoft.OpenApi.Models; | |
using Swashbuckle.AspNetCore.SwaggerGen; | |
public class ODataOperationFilter : IOperationFilter | |
{ | |
public void Apply(OpenApiOperation operation, OperationFilterContext context) | |
{ | |
if (operation.Parameters == null) operation.Parameters = new List<OpenApiParameter>(); | |
var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor; | |
var queryAttr = descriptor?.FilterDescriptors | |
.Where(fd => fd.Filter is EnableQueryAttribute) | |
.Select(fd => fd.Filter as EnableQueryAttribute) | |
.FirstOrDefault(); | |
if (queryAttr == null) | |
return; | |
if (queryAttr.AllowedQueryOptions.HasFlag(AllowedQueryOptions.Select)) | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$select", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Required = false | |
}); | |
if (queryAttr.AllowedQueryOptions.HasFlag(AllowedQueryOptions.Expand)) | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$expand", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Required = false | |
}); | |
if (queryAttr.AllowedQueryOptions.HasFlag(AllowedQueryOptions.Filter)) | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$filter", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Required = false | |
}); | |
if (queryAttr.AllowedQueryOptions.HasFlag(AllowedQueryOptions.Top)) | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$top", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Required = false | |
}); | |
if (queryAttr.AllowedQueryOptions.HasFlag(AllowedQueryOptions.Skip)) | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$skip", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Required = false | |
}); | |
if (queryAttr.AllowedQueryOptions.HasFlag(AllowedQueryOptions.OrderBy)) | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$orderby", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Required = false | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I need to setup OData, so that queryAttr contains the correct configuration?
Currently queryAttr says that Expand is activated, but I've never activated it. In fact I've explicitly disabled it the validateQuery method.