Skip to content

Can I get OData Query Options to API Explorer with ApiController? #522

Closed
@Olli-palkkaus

Description

@Olli-palkkaus

We have an existing API with about 20 CRUD services each with CRUD + some business logic methods. It is not generally OData (more traditional CRUD API) and we do not want to generally change it much.

However, we would like to add $filter, paging ($top, $skip, pageSize), $count, $order and perhaps $select to our 20-30 Get / List methods that now have usecase specific filtering by property (e.g. .../cms/articles?type=blogPost).

We can add these features very nicely using aspnet-api-versioning (thanks for that)...


// In Config:
var constraintResolver = new DefaultInlineConstraintResolver() {
  ConstraintMap = { ["apiVersion"] = typeof(ApiVersionRouteConstraint) } };
config.MapHttpAttributeRoutes(constraintResolver);

// In Controller:
[ApiVersion("2")]
[RoutePrefix("v0{version:apiVersion}/api/cms/articles")]
public class CmsController : ApiController

// New v3.0 method with OData support: Works, but $top etc. do not show in Swagger
[Route]
[HttpGet]
[SwaggerResponse(HttpStatusCode.OK, type: typeof(Article[]))]
[ResponseType(typeof(ODataValue<IEnumerable<Article>>))]
[EnableQuery(MaxTop = 100, AllowedQueryOptions = Select | Top | Skip | Count)]
[ApiVersion("3.0-rc")]
public PageResult<Article> ContentQueryable(ODataQueryOptions<Article> options)

// Non-OData method for existing v2.0. This needs to be kept as is.
[Route]
[HttpGet]
[SwaggerResponse(HttpStatusCode.OK, type: typeof(Article[]))]
public HttpResponseMessage CrudList([FromUri] AbcSection[] filter = null)

This works fine, but I am not able to get the OData Query options to show in Swagger. All the examples use ODataController and ODataRouting.

The question is: Do I need to move away from ApiController to ODataController AND/OR from MapHttpAttributeRoutes to MapVersionedODataRoutes to get the OData query options showing in Swagger?

Or can I use some option or extension point to get them showing with the code above? I do not really understand what triggers the generation of these Options to Swagger.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions