Skip to content

Instantly share code, notes, and snippets.

@jacobmohl
Last active December 16, 2021 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobmohl/a7eb2f3b508b61a2bd04ef0fe6d6e1de to your computer and use it in GitHub Desktop.
Save jacobmohl/a7eb2f3b508b61a2bd04ef0fe6d6e1de to your computer and use it in GitHub Desktop.
IOperationFilter for OpenAPI models to support OData parameters
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
Copy link

rgudkov-uss commented Dec 16, 2021

Very useful, thank you. Is there a way to not show $expand in Swagger, if it's not allowed?

No $expand in OData setup:

builder.Services
    .AddControllers()
    .AddOData(options => options.Select().Filter().OrderBy().SetMaxTop(50));

@jacobmohl
Copy link
Author

@rgudkov-uss then remove the block starting at linie 34 and ending at 44. But it is for all operations.

@rgudkov-uss
Copy link

rgudkov-uss commented Dec 16, 2021

@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

@jacobmohl
Copy link
Author

@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