Last active
December 16, 2021 19:31
-
-
Save jacobmohl/a7eb2f3b508b61a2bd04ef0fe6d6e1de to your computer and use it in GitHub Desktop.
IOperationFilter for OpenAPI models to support OData parameters
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.Authorization; | |
using Microsoft.AspNetCore.Mvc.Controllers; | |
using Microsoft.OpenApi.Models; | |
using Swashbuckle.AspNetCore.SwaggerGen; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace MySolution | |
{ | |
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; | |
if (descriptor != null && descriptor.FilterDescriptors.Any(filter => filter.Filter is Microsoft.AspNetCore.OData.Query.EnableQueryAttribute)) | |
{ | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$select", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Description = "Returns only the selected properties. (ex. FirstName, LastName, City)", | |
Required = false | |
}); | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$expand", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Description = "Include only the selected objects. (ex. Childrens, Locations)", | |
Required = false | |
}); | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$filter", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Description = "Filter the response with OData filter queries.", | |
Required = false | |
}); | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$top", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Description = "Number of objects to return. (ex. 25)", | |
Required = false | |
}); | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$skip", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Description = "Number of objects to skip in the current order (ex. 50)", | |
Required = false | |
}); | |
operation.Parameters.Add(new OpenApiParameter() | |
{ | |
Name = "$orderby", | |
In = ParameterLocation.Query, | |
Schema = new OpenApiSchema | |
{ | |
Type = "string", | |
}, | |
Description = "Define the order by one or more fields (ex. LastModified)", | |
Required = false | |
}); | |
} | |
} | |
} | |
} |
@rgudkov-uss then remove the block starting at linie 34 and ending at 44. But it is for all operations.
@jacobmohl this would exclude $expand for all endpoints. Instead, I did a modification of your code to only show allowed odata parameters. Check it out: https://gist.github.com/rgudkov-uss/564a7faf92d90b7f2257b10000ac2515
@rgudkov-uss that’s great. 🙌👌🏻 I didn’t researched that much. Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful, thank you. Is there a way to not show $expand in Swagger, if it's not allowed?
No $expand in OData setup: