Skip to content

Commit

Permalink
Removes Parameter types from generated swagger path. yahehe#166
Browse files Browse the repository at this point in the history
  • Loading branch information
jnallard committed Feb 26, 2019
1 parent 951252d commit 60e9a66
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions samples/Nancy.Swagger.Annotations.Demo/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ protected override void ConfigureConventions(NancyConventions nancyConventions)
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{
pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Origin", "*"));
pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Headers", "*"));
pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Methods", "*"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ServiceDetailsModule(ISwaggerModelCatalog modelCatalog, ISwaggerTagCatalo

Get("/customers/{name}", parameters => GetServiceCustomer(parameters.name), null, "GetCustomer");

Post("/customer/{service}", parameters => PostServiceCustomer(parameters.service, this.Bind<ServiceCustomer>()), null, "PostNewCustomer");
Post("/customer/{serviceGuid:guid}", parameters => PostServiceCustomer(parameters.serviceGuid, this.Bind<ServiceCustomer>()), null, "PostNewCustomer");

Post("/customer/{name}/file", parameters => PostCustomerReview(parameters.name, null), null, "PostCustomerReview");

Expand Down Expand Up @@ -144,14 +144,13 @@ private ServiceCustomer GetServiceCustomer([RouteParam(ParameterIn.Path, Default
}

[Route("PostNewCustomer")]
[Route(HttpMethod.Post, "/customer/{service}")]
[Route(Summary = "Post Service Customer")]
[SwaggerResponse(HttpStatusCode.OK, Message = "OK", Model = typeof(ServiceCustomer))]
[Route(Produces = new[] { "application/json" })]
[Route(Consumes = new[] { "application/json", "application/xml" })]
[Route(Tags = new[] { ServiceTagName })]
private ServiceCustomer PostServiceCustomer(
[RouteParam(ParameterIn.Path, DefaultValue = "my-service")] string service,
[RouteParam(ParameterIn.Path, Description = "The GUID that identifies the service")] string serviceGuid,
[RouteParam(ParameterIn.Body)] ServiceCustomer customer)
{
return customer;
Expand Down
2 changes: 2 additions & 0 deletions samples/Nancy.Swagger.Autofac.Demo/AutofacBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ protected override void ConfigureApplicationContainer(ILifetimeScope container)

SwaggerAnnotationsConfig.ShowOnlyAnnotatedRoutes = true;
this.ApplicationPipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Origin", "*"));
this.ApplicationPipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Headers", "*"));
this.ApplicationPipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Methods", "*"));
}
}
}
2 changes: 2 additions & 0 deletions samples/Nancy.Swagger.Demo/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ protected override void ConfigureConventions(NancyConventions nancyConventions)
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{
pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Origin", "*"));
pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Headers", "*"));
pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Methods", "*"));
}
}
}
14 changes: 13 additions & 1 deletion src/Nancy.Swagger/Services/SwaggerRouteData.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using Swagger.ObjectModel;
using System.Text.RegularExpressions;

namespace Nancy.Swagger.Services
{
public class SwaggerRouteData
{
public SwaggerRouteData(string path, PathItem pathItem)
{
Path = path;
Path = RemovePathParameterTypes(path);
PathItem = pathItem;
Types = new Dictionary<HttpMethod, Type>();
}
Expand All @@ -30,5 +31,16 @@ public SwaggerRouteData Combine(SwaggerRouteData data)
}
return combined;
}

/// <summary>
/// Removes parameter types from Nancy routes - Swagger doesn't expect them.
/// Examples: "/service/customers/{name:guid}" becomes "/service/customers/{name}"
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private string RemovePathParameterTypes(string path)
{
return Regex.Replace(path, @":\w+}", "}");
}
}
}

0 comments on commit 60e9a66

Please sign in to comment.