Skip to content

ApiExplorer doesn't select the correct action when using MapToApiVersion #117

Closed
@doubret

Description

@doubret

I have this simple controller :

[ApiVersion("1.0")]
[ApiVersion("2.0")]
[ApiVersion("3.0")]
[RoutePrefix("api/v{v:apiVersion}/healthcheck")]
public class HealthCheckController : ApiController
{
    [HttpGet]
    [AllowAnonymous]
    [Route("")]
    public HttpResponseMessage Get()
    {
        return new HttpResponseMessage { Content = new StringContent("OK") };
    }

    [HttpGet, AllowAnonymous]
    [Route("")]
    [MapToApiVersion("2.0")]
    public ActionResponse GetV2()
    {
        return ActionResponse.Create(null, "toto");
    }
}

The VersionedApiExplorer fails to pick the correct action for version 2.0.

This is because in the InitializeApiDescriptions method, when it sees the Get() method of my controller, it considers that the method fits the version, then later it sees the GetV2 method but doesn't select it because it already found an action for the route.

As a quick fix i modified the InitializeApiDescriptions like this :

                    if (!apiDescriptionGroup.ApiDescriptions.Contains(description, Comparer))
                    {
                        description.GroupName = apiDescriptionGroup.Name;
                        apiDescriptionGroup.ApiDescriptions.Add(description);
                    }
                    else
                    {
                        var model = description.ActionDescriptor.GetApiVersionModel();
                        var mapTo = model.DeclaredApiVersions.Contains(apiVersion);

                        if (mapTo)
                        {
                            var previous = apiDescriptionGroup.ApiDescriptions.Single(x => Comparer.Equals(x, description));

                            apiDescriptionGroup.ApiDescriptions.Remove(previous);

                            description.GroupName = apiDescriptionGroup.Name;
                            apiDescriptionGroup.ApiDescriptions.Add(description);
                        }
                    }

Probably not the best solution but at least it solved the problem for now.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions