Skip to content

Commit

Permalink
Add ASP.NET Core TestServer support
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed May 16, 2018
1 parent bbc87b4 commit 164a999
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 25 deletions.
19 changes: 19 additions & 0 deletions src/NSwag.Commands/Commands/IsolatedSwaggerOutputCommandBase.cs
Expand Up @@ -7,10 +7,15 @@
//-----------------------------------------------------------------------
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using NConsole;
using Newtonsoft.Json;
using NJsonSchema;
using NJsonSchema.Infrastructure;
using NSwag.AssemblyLoader.Utilities;

namespace NSwag.Commands
{
Expand All @@ -37,5 +42,19 @@ public override async Task<object> RunAsync(CommandLineProcessor processor, ICon
await this.TryWriteDocumentOutputAsync(host, () => document).ConfigureAwait(false);
return document;
}

protected async Task<Assembly[]> LoadAssembliesAsync(IEnumerable<string> assemblyPaths, AssemblyLoader.AssemblyLoader assemblyLoader)
{
#if FullNet
var assemblies = PathUtilities.ExpandFileWildcards(assemblyPaths)
.Select(path => Assembly.LoadFrom(path)).ToArray();
#else
var currentDirectory = await DynamicApis.DirectoryGetCurrentDirectoryAsync().ConfigureAwait(false);
var assemblies = PathUtilities.ExpandFileWildcards(assemblyPaths)
.Select(path => assemblyLoader.Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(path, currentDirectory)))
.ToArray();
#endif
return assemblies;
}
}
}
Expand Up @@ -10,8 +10,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using NConsole;
using Newtonsoft.Json;
using NSwag.SwaggerGeneration.AspNetCore;
Expand Down Expand Up @@ -169,19 +174,23 @@ public override async Task<object> RunAsync(CommandLineProcessor processor, ICon

protected override async Task<string> RunIsolatedAsync(AssemblyLoader.AssemblyLoader assemblyLoader)
{
// Run with .dll

Settings.DocumentTemplate = await GetDocumentTemplateAsync();
InitializeCustomTypes(assemblyLoader);

// TODO: Load TestServer, get ApiExplorer, run generator
var assemblies = await LoadAssembliesAsync(AssemblyPaths, assemblyLoader).ConfigureAwait(false);
var startupType = assemblies.First().ExportedTypes.First(t => t.Name == "Startup"); // TODO: Use .NET Core startup lookup or provide setting

using (var testServer = new TestServer(new WebHostBuilder().UseStartup(startupType)))
{
// See https://github.com/aspnet/Mvc/issues/5690

//var generator = new AspNetCoreToSwaggerGenerator(Settings);
//var document = await generator.GenerateAsync(controllerTypes).ConfigureAwait(false);
var apiDescriptionProvider = testServer.Host.Services.GetRequiredService<IApiDescriptionGroupCollectionProvider>();

//return PostprocessDocument(document);
var generator = new AspNetCoreToSwaggerGenerator(Settings);
var document = await generator.GenerateAsync(apiDescriptionProvider.ApiDescriptionGroups).ConfigureAwait(false);

return null;
return PostprocessDocument(document);
}
}

private static void TryDeleteFiles(List<string> files)
Expand Down
Expand Up @@ -101,14 +101,7 @@ private async Task<IEnumerable<Type>> GetControllerTypesAsync(IEnumerable<string
if (AssemblyPaths == null || AssemblyPaths.Length == 0)
throw new InvalidOperationException("No assembly paths have been provided.");

#if FullNet
var assemblies = PathUtilities.ExpandFileWildcards(AssemblyPaths)
.Select(path => Assembly.LoadFrom(path)).ToArray();
#else
var currentDirectory = await DynamicApis.DirectoryGetCurrentDirectoryAsync().ConfigureAwait(false);
var assemblies = PathUtilities.ExpandFileWildcards(AssemblyPaths)
.Select(path => assemblyLoader.Context.LoadFromAssemblyPath(PathUtilities.MakeAbsolutePath(path, currentDirectory))).ToArray();
#endif
var assemblies = await LoadAssembliesAsync(AssemblyPaths, assemblyLoader);

var allExportedNames = assemblies.SelectMany(a => a.ExportedTypes).Select(t => t.FullName).ToList();
var matchedControllerNames = controllerNames
Expand Down
1 change: 1 addition & 0 deletions src/NSwag.Commands/NSwag.Commands.csproj
Expand Up @@ -27,6 +27,7 @@
<EmbeddedResource Include="Commands\SwaggerGeneration\AspNetCore\AspNetCore.targets" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.3" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="NConsole" Version="3.9.6519.30868" />
Expand Down
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions src/NSwag.Sample.NETCore20.Part/SampleController.cs
@@ -0,0 +1,14 @@
using Microsoft.AspNetCore.Mvc;

namespace NSwag.Sample.NETCore20.Part
{
[Route("/sample")]
public class SampleController : Controller
{
[HttpPost]
public string GetSample()
{
return null;
}
}
}
3 changes: 2 additions & 1 deletion src/NSwag.Sample.NETCore20/NSwag.Sample.NETCore20.csproj
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
Expand All @@ -21,6 +21,7 @@

<ItemGroup>
<ProjectReference Include="..\NSwag.AspNetCore\NSwag.AspNetCore.csproj" />
<ProjectReference Include="..\NSwag.Sample.NETCore20.Part\NSwag.Sample.NETCore20.Part.csproj" />
</ItemGroup>

</Project>
9 changes: 1 addition & 8 deletions src/NSwag.Sample.NETCore20/Program.cs
@@ -1,12 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace NSwag.Sample.NETCore20
{
Expand Down
5 changes: 4 additions & 1 deletion src/NSwag.Sample.NETCore20/Startup.cs
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.DependencyInjection;
using NJsonSchema;
using NSwag.AspNetCore;
using NSwag.Sample.NETCore20.Part;

namespace NSwag.Sample.NETCore20
{
Expand All @@ -19,7 +20,9 @@ public Startup(IConfiguration configuration)

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddMvc().
AddApplicationPart(typeof(SampleController).GetTypeInfo().Assembly);

services.AddSwagger();
}

Expand Down
21 changes: 21 additions & 0 deletions src/NSwag.sln
Expand Up @@ -148,6 +148,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSwag.Sample.NETCore20", "N
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSwag.Core.Tests", "NSwag.Core.Tests\NSwag.Core.Tests.csproj", "{810AF444-D713-4CEE-BC9F-13D7875AE69B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSwag.Sample.NETCore20.Part", "NSwag.Sample.NETCore20.Part\NSwag.Sample.NETCore20.Part.csproj", "{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -910,6 +912,24 @@ Global
{810AF444-D713-4CEE-BC9F-13D7875AE69B}.ReleaseTypeScriptStrict|x64.Build.0 = Release|Any CPU
{810AF444-D713-4CEE-BC9F-13D7875AE69B}.ReleaseTypeScriptStrict|x86.ActiveCfg = Release|Any CPU
{810AF444-D713-4CEE-BC9F-13D7875AE69B}.ReleaseTypeScriptStrict|x86.Build.0 = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Debug|x64.ActiveCfg = Debug|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Debug|x64.Build.0 = Debug|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Debug|x86.ActiveCfg = Debug|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Debug|x86.Build.0 = Debug|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Release|Any CPU.Build.0 = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Release|x64.ActiveCfg = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Release|x64.Build.0 = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Release|x86.ActiveCfg = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.Release|x86.Build.0 = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.ReleaseTypeScriptStrict|Any CPU.ActiveCfg = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.ReleaseTypeScriptStrict|Any CPU.Build.0 = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.ReleaseTypeScriptStrict|x64.ActiveCfg = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.ReleaseTypeScriptStrict|x64.Build.0 = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.ReleaseTypeScriptStrict|x86.ActiveCfg = Release|Any CPU
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC}.ReleaseTypeScriptStrict|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -961,6 +981,7 @@ Global
{42F44479-032B-493A-87A7-E103275CB26B} = {F31829C6-CEE4-4242-A4D9-820C36127B0B}
{EC94C4C5-3822-46D0-A8B4-B241D34830C0} = {D8CC0D1C-8DAC-49FE-AA78-C028DC124DD5}
{810AF444-D713-4CEE-BC9F-13D7875AE69B} = {634E4ABD-29EC-4EB2-81EF-7E41D6D6F6E0}
{A8DE4571-6392-4165-8F60-F4DCB9E6E4EC} = {D8CC0D1C-8DAC-49FE-AA78-C028DC124DD5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {98FCEEE2-A45C-41E7-B2ED-1B14755E9067}
Expand Down

0 comments on commit 164a999

Please sign in to comment.