domaindrivendev / Swashbuckle.WebApi

Seamlessly adds a swagger to WebApi projects!
BSD 3-Clause "New" or "Revised" License
3.07k stars 677 forks source link

WebAPI Versioning with Swagger Error (ApiVersion unspecified) #1211

Open zerosnap opened 6 years ago

zerosnap commented 6 years ago

VERSION:

Swashbuckle: 5.6.0 AspNet.WebApi.Versioning 2.2.0

STEPS TO REPRODUCE:

Consume any endpoint (postman or swagger)

EXPECTED RESULT:

Hits the actual endpoint

ACTUAL RESULT:

Receive the error: ApiVersionUnspecified or Swagger UI requires a version input.

ADDITIONAL DETAILS

Using Owin with WebAPI. Owin's Setup is blank. Global.asax.cs:

protected void Application_Start(object sender, EventArgs e) { GlobalConfiguration.Configure((GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(WebApiConfig)) as WebApiConfig).Register); }

WebApiConfig.cs:

public void Register(HttpConfiguration config) { var constraintResolver = new DefaultInlineConstraintResolver { ConstraintMap = { ["apiVersion"] = typeof(ApiVersionRouteConstraint) } }; config.MapHttpAttributeRoutes(constraintResolver); config.AddApiVersioning(o => o.ReportApiVersions = true); this.restApiConfig.Register(config); }

Controller route formatting:

[ApiVersion("3.0")] [RoutePrefix("v3")] public class SomeController {

[Route("/someRoute")] public HttpResponseMessage GetStuff(){ }

We have attempted to use the [RoutePrefix(v{version : apiVersion)] attribute, which works with actually making the endpoints function under versioning, but forces Swagger to include a version parameter, which we do not want. We want our paths to be "/v2/controller/action" or "/v3/controller/action".

Happy to use the [RoutePrefix(v{version : apiVersion)] method, as long as we can display the paths properly in Swagger without having to input a version parameter. Otherwise, if we could use the [RoutePrefix("v3")] method without getting ApiVersionUnspecified errors, that would be even better.

commonsensesoftware commented 4 years ago

You need to also include the API Explorer extensions for API versioning for this to work. You would then add the configuration:

config.AddVersionedApiExplorer();

You can find a complete end-to-end sample here