Skip to content

Commit

Permalink
Use latest NJS and Namotion.Reflection (v13) (#2178)
Browse files Browse the repository at this point in the history
* Use latest NJS and Namotion.Reflection

* Fix comments

* Use more contextual types

* Refactor NJS types

* Fix tests

* Fix stuff, remove obsolete methods

* Fix window

* Fix IsolatedCommandBase

* Revert to packages

* Update NJS

* Refactoring

* Update NJS

* Fixes

* Change TransformToExternalPath default and update for NJS

* Fix web api OperationParameterProcessor

* Correctly set parameter value, closes #2010

* Fix test

* Update njs

* Fix project

* Fix tests
  • Loading branch information
RicoSuter authored May 24, 2019
1 parent 2dd4fa6 commit 9265cee
Show file tree
Hide file tree
Showing 99 changed files with 676 additions and 854 deletions.
2 changes: 1 addition & 1 deletion src/NSwag.AspNet.Owin/NSwag.AspNet.Owin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Owin" Version="3.0.1" />
<PackageReference Include="Microsoft.Owin.StaticFiles" Version="3.0.1" />
<PackageReference Include="NJsonSchema" Version="9.14.1" />
<PackageReference Include="NJsonSchema" Version="10.0.5" />
<PackageReference Include="Owin" Version="1.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.AspNet.WebApi/NSwag.AspNet.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
<DefineConstants>TRACE;DEBUG;NET45</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NJsonSchema" Version="9.14.1" />
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.3" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.3" />
<PackageReference Include="NJsonSchema" Version="10.0.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NSwag.Annotations\NSwag.Annotations.csproj" />
Expand Down
1 change: 1 addition & 0 deletions src/NSwag.AspNetCore.Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal class Program
["Newtonsoft.Json"] = new AssemblyLoadInfo(new Version(9, 0, 0)),
["NConsole"] = new AssemblyLoadInfo(new Version(3, 9, 0, 0)),
["NJsonSchema"] = new AssemblyLoadInfo(new Version(9, 7, 7)),
["Namotion.Reflection"] = new AssemblyLoadInfo(new Version(1, 0, 0)),
["NSwag.AssemblyLoader"] = new AssemblyLoadInfo(NSwagVersion),
["NSwag.Commands"] = new AssemblyLoadInfo(NSwagVersion),
["NSwag.Core"] = new AssemblyLoadInfo(NSwagVersion),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using NSwag.AspNetCore;
using NSwag.AspNetCore.Middlewares;
using NSwag.SwaggerGeneration.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -31,17 +31,15 @@ public static IApplicationBuilder UseOpenApi(this IApplicationBuilder app, Actio
/// The methods <see cref="UseOpenApi"/> and <see cref="UseSwagger"/> are the same, but <see cref="UseSwagger"/> will be deprecated eventually.</remarks>
/// <param name="app">The app.</param>
/// <param name="configure">Configure additional settings.</param>
[Obsolete("Use UseOpenApi() instead.")]
public static IApplicationBuilder UseSwagger(this IApplicationBuilder app, Action<SwaggerDocumentMiddlewareSettings> configure = null)
{
return UseSwaggerWithApiExplorerCore(app, configure);
}

private static IApplicationBuilder UseSwaggerWithApiExplorerCore(IApplicationBuilder app, Action<SwaggerDocumentMiddlewareSettings> configure)
{
// TODO(v12): Add IOptions support when SwaggerUi3Settings<> T has been removed
//var settings = configure == null && app.ApplicationServices.GetService<IOptions<SwaggerMiddlewareSettings>>()?.Value ?? new SwaggerMiddlewareSettings();

var settings = new SwaggerDocumentMiddlewareSettings();
var settings = configure == null ? app.ApplicationServices.GetService<IOptions<SwaggerDocumentMiddlewareSettings>>()?.Value : null ?? new SwaggerDocumentMiddlewareSettings();
configure?.Invoke(settings);

if (settings.Path.Contains("{documentName}"))
Expand All @@ -67,23 +65,19 @@ private static IApplicationBuilder UseSwaggerWithApiExplorerCore(IApplicationBui
/// <returns>The app builder.</returns>
public static IApplicationBuilder UseSwaggerUi3(
this IApplicationBuilder app,
Action<SwaggerUi3Settings<WebApiToSwaggerGeneratorSettings>> configure = null)
Action<SwaggerUi3Settings> configure = null)
{
// TODO(v12): Add IOptions support when SwaggerUi3Settings<> T has been removed
//var settings = configure == null && app.ApplicationServices.GetService<IOptions<SwaggerUi3Settings<WebApiToSwaggerGeneratorSettings>>>()?.Value ??
// new SwaggerUi3Settings<WebApiToSwaggerGeneratorSettings>();

var settings = new SwaggerUi3Settings<WebApiToSwaggerGeneratorSettings>();
var settings = configure == null ? app.ApplicationServices.GetService<IOptions<SwaggerUi3Settings>>()?.Value : null ?? new SwaggerUi3Settings();
configure?.Invoke(settings);

UseSwaggerUiWithDocumentNamePlaceholderExpanding(app, settings, (swaggerRoute, swaggerUiRoute) =>
{
app.UseMiddleware<RedirectToIndexMiddleware>(swaggerUiRoute, swaggerRoute, settings.TransformToExternalPath);
app.UseMiddleware<SwaggerUiIndexMiddleware<WebApiToSwaggerGeneratorSettings>>(swaggerUiRoute + "/index.html", settings, "NSwag.AspNetCore.SwaggerUi3.index.html");
app.UseMiddleware<SwaggerUiIndexMiddleware>(swaggerUiRoute + "/index.html", settings, "NSwag.AspNetCore.SwaggerUi3.index.html");
app.UseFileServer(new FileServerOptions
{
RequestPath = new PathString(swaggerUiRoute),
FileProvider = new EmbeddedFileProvider(typeof(SwaggerExtensions).GetTypeInfo().Assembly, "NSwag.AspNetCore.SwaggerUi3")
FileProvider = new EmbeddedFileProvider(typeof(NSwagApplicationBuilderExtensions).GetTypeInfo().Assembly, "NSwag.AspNetCore.SwaggerUi3")
});
},
(documents) =>
Expand All @@ -110,27 +104,51 @@ public static IApplicationBuilder UseSwaggerUi3(
/// <returns>The app builder.</returns>
public static IApplicationBuilder UseReDoc(
this IApplicationBuilder app,
Action<SwaggerReDocSettings<WebApiToSwaggerGeneratorSettings>> configure = null)
Action<SwaggerReDocSettings> configure = null)
{
var settings = new SwaggerReDocSettings<WebApiToSwaggerGeneratorSettings>();
var settings = configure == null ? app.ApplicationServices.GetService<IOptions<SwaggerReDocSettings>>()?.Value : null ?? new SwaggerReDocSettings();
configure?.Invoke(settings);

UseSwaggerUiWithDocumentNamePlaceholderExpanding(app, settings, (swaggerRoute, swaggerUiRoute) =>
{
app.UseMiddleware<RedirectToIndexMiddleware>(swaggerUiRoute, swaggerRoute, settings.TransformToExternalPath);
app.UseMiddleware<SwaggerUiIndexMiddleware<WebApiToSwaggerGeneratorSettings>>(swaggerUiRoute + "/index.html", settings, "NSwag.AspNetCore.ReDoc.index.html");
app.UseMiddleware<SwaggerUiIndexMiddleware>(swaggerUiRoute + "/index.html", settings, "NSwag.AspNetCore.ReDoc.index.html");
app.UseFileServer(new FileServerOptions
{
RequestPath = new PathString(swaggerUiRoute),
FileProvider = new EmbeddedFileProvider(typeof(SwaggerExtensions).GetTypeInfo().Assembly, "NSwag.AspNetCore.ReDoc")
FileProvider = new EmbeddedFileProvider(typeof(NSwagApplicationBuilderExtensions).GetTypeInfo().Assembly, "NSwag.AspNetCore.ReDoc")
});
}, (documents) => false);

return app;
}

/// <summary>Adds the Swagger UI (only) to the pipeline.</summary>
/// <param name="app">The app.</param>
/// <param name="configure">Configure the Swagger UI settings.</param>
/// <returns>The app builder.</returns>
[Obsolete("Use " + nameof(UseSwaggerUi3) + " instead.")]
public static IApplicationBuilder UseSwaggerUi(
this IApplicationBuilder app,
Action<SwaggerUiSettings> configure = null)
{
var settings = new SwaggerUiSettings();
settings.DocumentPath = "/swagger/v1/swagger.json";
configure?.Invoke(settings);

app.UseMiddleware<RedirectToIndexMiddleware>(settings.ActualSwaggerUiPath, settings.ActualSwaggerDocumentPath, settings.TransformToExternalPath);
app.UseMiddleware<SwaggerUiIndexMiddleware>(settings.ActualSwaggerUiPath + "/index.html", settings, "NSwag.AspNetCore.SwaggerUi.index.html");
app.UseFileServer(new FileServerOptions
{
RequestPath = new PathString(settings.ActualSwaggerUiPath),
FileProvider = new EmbeddedFileProvider(typeof(NSwagApplicationBuilderExtensions).GetTypeInfo().Assembly, "NSwag.AspNetCore.SwaggerUi")
});

return app;
}

private static void UseSwaggerUiWithDocumentNamePlaceholderExpanding(IApplicationBuilder app,
SwaggerUiSettingsBase<WebApiToSwaggerGeneratorSettings> settings,
SwaggerUiSettingsBase settings,
Action<string, string> register,
Func<IEnumerable<SwaggerDocumentRegistration>, bool> registerMultiple)
{
Expand Down
12 changes: 6 additions & 6 deletions src/NSwag.AspNetCore/Middlewares/SwaggerUiIndexMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using NSwag.SwaggerGeneration;

namespace NSwag.AspNetCore.Middlewares
{
internal class SwaggerUiIndexMiddleware<T>
where T : SwaggerGeneratorSettings, new()
internal class SwaggerUiIndexMiddleware
{
private readonly RequestDelegate _nextDelegate;
private readonly string _indexPath;
private readonly SwaggerUiSettingsBase<T> _settings;
private readonly SwaggerUiSettingsBase _settings;
private readonly string _resourcePath;

public SwaggerUiIndexMiddleware(RequestDelegate nextDelegate, string indexPath, SwaggerUiSettingsBase<T> settings, string resourcePath)
public SwaggerUiIndexMiddleware(RequestDelegate nextDelegate, string indexPath, SwaggerUiSettingsBase settings, string resourcePath)
{
_nextDelegate = nextDelegate;
_indexPath = indexPath;
Expand All @@ -27,7 +25,7 @@ public async Task Invoke(HttpContext context)
{
if (context.Request.Path.HasValue && string.Equals(context.Request.Path.Value.Trim('/'), _indexPath.Trim('/'), StringComparison.OrdinalIgnoreCase))
{
var stream = typeof(SwaggerUiIndexMiddleware<T>).GetTypeInfo().Assembly.GetManifestResourceStream(_resourcePath);
var stream = typeof(SwaggerUiIndexMiddleware).GetTypeInfo().Assembly.GetManifestResourceStream(_resourcePath);
using (var reader = new StreamReader(stream))
{
context.Response.Headers["Content-Type"] = "text/html; charset=utf-8";
Expand All @@ -36,7 +34,9 @@ public async Task Invoke(HttpContext context)
}
}
else
{
await _nextDelegate(context);
}
}
}
}
112 changes: 0 additions & 112 deletions src/NSwag.AspNetCore/Middlewares/WebApiToSwaggerMiddleware.cs

This file was deleted.

3 changes: 1 addition & 2 deletions src/NSwag.AspNetCore/NSwag.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks>net451;netstandard1.6;netstandard2.0</TargetFrameworks>
<Description>NSwag: The Swagger API toolchain for .NET and TypeScript</Description>
<Version>12.3.1</Version>
<PackageTags>Swagger Documentation WebApi AspNet TypeScript CodeGen</PackageTags>
<PackageTags>Swagger Documentation AspNetCore NetCore TypeScript CodeGen</PackageTags>
<Copyright>Copyright © Rico Suter, 2019</Copyright>
<PackageLicenseUrl>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>http://NSwag.org</PackageProjectUrl>
Expand Down Expand Up @@ -39,7 +39,6 @@
<ProjectReference Include="..\NSwag.Annotations\NSwag.Annotations.csproj" />
<ProjectReference Include="..\NSwag.Core\NSwag.Core.csproj" />
<ProjectReference Include="..\NSwag.SwaggerGeneration.AspNetCore\NSwag.SwaggerGeneration.AspNetCore.csproj" />
<ProjectReference Include="..\NSwag.SwaggerGeneration.WebApi\NSwag.SwaggerGeneration.WebApi.csproj" />
<ProjectReference Include="..\NSwag.SwaggerGeneration\NSwag.SwaggerGeneration.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit 9265cee

Please sign in to comment.