Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary string creations from OpenApiDocumentMiddleware #4553

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class OpenApiDocumentMiddleware : OwinMiddleware
public OpenApiDocumentMiddleware(OwinMiddleware next, string path, IEnumerable<Type> controllerTypes, SwaggerSettings<WebApiOpenApiDocumentGeneratorSettings> settings)
: base(next)
{
_path = path;
_path = path.StartsWith("/") ? path : '/' + path;
_controllerTypes = controllerTypes;
_settings = settings;
}
Expand All @@ -44,7 +44,7 @@ public OpenApiDocumentMiddleware(OwinMiddleware next, string path, IEnumerable<T
/// <returns>The task.</returns>
public override async Task Invoke(IOwinContext context)
{
if (context.Request.Path.HasValue && string.Equals(context.Request.Path.Value.Trim('/'), _path.Trim('/'), StringComparison.OrdinalIgnoreCase))
if (context.Request.Path.HasValue && string.Equals(context.Request.Path.Value, _path, StringComparison.OrdinalIgnoreCase))
RicoSuter marked this conversation as resolved.
Show resolved Hide resolved
{
var schemaJson = await GetDocumentAsync(context);

Expand Down
8 changes: 4 additions & 4 deletions src/NSwag.AspNetCore/Middlewares/OpenApiDocumentMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public OpenApiDocumentMiddleware(RequestDelegate nextDelegate, IServiceProvider
_nextDelegate = nextDelegate;

_documentName = documentName;
_path = path;
_path = path.StartsWith("/") ? path : '/' + path;

_apiDescriptionGroupCollectionProvider = serviceProvider.GetService<IApiDescriptionGroupCollectionProvider>() ??
throw new InvalidOperationException("API Explorer not registered in DI.");
Expand All @@ -59,11 +59,11 @@ public OpenApiDocumentMiddleware(RequestDelegate nextDelegate, IServiceProvider
/// <returns>The task.</returns>
public async Task Invoke(HttpContext context)
{
if (context.Request.Path.HasValue && string.Equals(context.Request.Path.Value.Trim('/'), _path.Trim('/'), StringComparison.OrdinalIgnoreCase))
if (context.Request.Path.HasValue && string.Equals(context.Request.Path.Value, _path, StringComparison.OrdinalIgnoreCase))
{
var schemaJson = await GetDocumentAsync(context);
context.Response.StatusCode = 200;
context.Response.Headers["Content-Type"] = _path.ToLowerInvariant().Contains(".yaml") ?
context.Response.Headers["Content-Type"] = _path.IndexOf(".yaml", StringComparison.OrdinalIgnoreCase) >= 0 ?
"application/yaml; charset=utf-8" :
"application/json; charset=utf-8";

Expand Down Expand Up @@ -104,7 +104,7 @@ protected virtual async Task<string> GetDocumentAsync(HttpContext context)
try
{
var openApiDocument = await GenerateDocumentAsync(context);
var data = _path.ToLowerInvariant().Contains(".yaml") ?
var data = _path.IndexOf(".yaml", StringComparison.OrdinalIgnoreCase) >= 0 ?
OpenApiYamlDocument.ToYaml(openApiDocument) :
openApiDocument.ToJson();

Expand Down
Loading