1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

camelcase swagger/public apis

This commit is contained in:
Kyle Spearrin 2019-02-28 20:50:40 -05:00
parent 0a82f472ef
commit c02f732056
4 changed files with 59 additions and 4 deletions

View File

@ -14,7 +14,6 @@ using Stripe;
using Bit.Core.Utilities;
using IdentityModel;
using Microsoft.AspNetCore.HttpOverrides;
using Swashbuckle.AspNetCore.Swagger;
namespace Bit.Api
{
@ -112,9 +111,16 @@ namespace Bit.Api
services.AddMvc(config =>
{
config.Conventions.Add(new ApiExplorerGroupConvention());
config.Conventions.Add(new PublicApiControllersModelConvention());
config.Filters.Add(new ExceptionHandlerFilterAttribute());
config.Filters.Add(new ModelStateValidationFilterAttribute());
}).AddJsonOptions(o => o.SerializerSettings.ContractResolver = new DefaultContractResolver());
}).AddJsonOptions(options =>
{
if(Configuration["swaggerGen"] != "true")
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
}
});
services.AddSwagger(globalSettings);
@ -186,8 +192,8 @@ namespace Bit.Api
// Add MVC to the request pipeline.
app.UseMvc();
if(true || globalSettings.SelfHosted)
if(globalSettings.SelfHosted)
{
app.UseSwagger(config =>
{

View File

@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Bit.Api.Utilities
{
public class CamelCaseJsonResultFilterAttribute : IAsyncResultFilter
{
private static JsonSerializerSettings _jsonSerializerSettings;
static CamelCaseJsonResultFilterAttribute()
{
_jsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
}
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
if(context.Result is JsonResult jsonResult)
{
context.Result = new JsonResult(jsonResult.Value, _jsonSerializerSettings);
}
await next();
}
}
}

View File

@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc.ApplicationModels;
namespace Bit.Api.Utilities
{
public class PublicApiControllersModelConvention : IControllerModelConvention
{
public void Apply(ControllerModel controller)
{
var controllerNamespace = controller.ControllerType.Namespace;
if(controllerNamespace.Contains(".Public."))
{
controller.Filters.Add(new CamelCaseJsonResultFilterAttribute());
}
}
}
}

View File

@ -29,6 +29,9 @@ namespace Bit.Api.Utilities
{
{ "OAuth2 Client Credentials", new[] { "api.organization" } }
});
config.DescribeAllParametersInCamelCase();
// config.UseReferencedDefinitionsForEnums();
});
}
}