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

WIP aspnetcore2swaggercommand #1034

Closed
wants to merge 1 commit into from
Closed
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
48 changes: 48 additions & 0 deletions src/NSwag.Commands.AspNetCore/ApplicationServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace NSwag.Commands.AspNetCore
{
internal static class ApplicationServiceProvider
{
public static IServiceProvider GetServiceProvider(string applicationName)
{
var assemblyName = new AssemblyName(applicationName);
var assembly = Assembly.Load(assemblyName);

var entryPoint = assembly.EntryPoint?.DeclaringType;
var buildWebHostMethod = entryPoint?.GetMethod("BuildWebHost");
var args = new string[0];

IWebHost webHost = null;
if (buildWebHostMethod != null)
{
webHost = (IWebHost)buildWebHostMethod.Invoke(null, new object[] { args });
}
else
{
var createWebHostMethod = entryPoint?.GetMethod("CreateWebHostBuilder");
if (createWebHostMethod != null)
{
var webHostBuilder = (IWebHostBuilder)createWebHostMethod.Invoke(null, new object[] { args });
webHost = webHostBuilder.Build();
}
}

if (webHost != null)
{
return webHost.Services
.GetRequiredService<IServiceScopeFactory>()
.CreateScope()
.ServiceProvider;
}

throw new InvalidOperationException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//-----------------------------------------------------------------------
// <copyright file="WebApiAssemblyToSwaggerGeneratorSettings.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using NSwag.SwaggerGeneration.AspNetCore;

namespace NSwag.Commands.AspNetCore
{
/// <summary>Settings for the WebApiAssemblyToSwaggerGenerator.</summary>
internal class AspNetCoreToSwaggerGeneratorCommandSettings : AspNetCoreToSwaggerGeneratorSettings
{
/// <summary>Gets or sets the additional document processor type names in the form 'assemblyName:fullTypeName' or 'fullTypeName' which are instantiated during generation.</summary>
public string[] DocumentProcessorTypes { get; set; }

/// <summary>Gets or sets the additional operation processor type names in the form 'assemblyName:fullTypeName' or 'fullTypeName' which are instantiated during generation.</summary>
public string[] OperationProcessorTypes { get; set; }
}
}
129 changes: 129 additions & 0 deletions src/NSwag.Commands.AspNetCore/AspNetToSwaggerInnerCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//using NConsole;
//using System.Threading.Tasks;
//using NJsonSchema;
//using Microsoft.AspNetCore.Mvc.ApiExplorer;
//using Microsoft.Extensions.DependencyInjection;
//using NSwag.SwaggerGeneration.AspNetCore;
//using System.IO;

//namespace NSwag.Commands.AspNetCore
//{
// internal class AspNetToSwaggerInnerCommand : IConsoleCommand
// {
// public AspNetCoreToSwaggerGeneratorCommandSettings Settings { get; } = new AspNetCoreToSwaggerGeneratorCommandSettings();

// [Argument(Name = "ApplicationName", IsRequired = true, Description = "The application name.")]
// public string ApplicationName { get; set; }

// [Argument(Name = "OutputPath", IsRequired = true, Description = "The output path.")]
// public string OutputPath { get; set; }

// [Argument(Name = "DefaultPropertyNameHandling", IsRequired = false, Description = "The default property name handling ('Default' or 'CamelCase').")]
// public PropertyNameHandling DefaultPropertyNameHandling
// {
// get => Settings.DefaultPropertyNameHandling;
// set => Settings.DefaultPropertyNameHandling = value;
// }

// [Argument(Name = "DefaultReferenceTypeNullHandling", IsRequired = false, Description = "The default null handling (if NotNullAttribute and CanBeNullAttribute are missing, default: Null, Null or NotNull).")]
// public ReferenceTypeNullHandling DefaultReferenceTypeNullHandling
// {
// get => Settings.DefaultReferenceTypeNullHandling;
// set => Settings.DefaultReferenceTypeNullHandling = value;
// }

// [Argument(Name = "DefaultEnumHandling", IsRequired = false, Description = "The default enum handling ('String' or 'Integer'), default: Integer.")]
// public EnumHandling DefaultEnumHandling
// {
// get => Settings.DefaultEnumHandling;
// set => Settings.DefaultEnumHandling = value;
// }

// [Argument(Name = "FlattenInheritanceHierarchy", IsRequired = false, Description = "Flatten the inheritance hierarchy instead of using allOf to describe inheritance (default: false).")]
// public bool FlattenInheritanceHierarchy
// {
// get => Settings.FlattenInheritanceHierarchy;
// set => Settings.FlattenInheritanceHierarchy = value;
// }

// [Argument(Name = "GenerateKnownTypes", IsRequired = false, Description = "Generate schemas for types in KnownTypeAttribute attributes (default: true).")]
// public bool GenerateKnownTypes
// {
// get => Settings.GenerateKnownTypes;
// set => Settings.GenerateKnownTypes = value;
// }

// [Argument(Name = "GenerateXmlObjects", IsRequired = false, Description = "Generate xmlObject representation for definitions (default: false).")]
// public bool GenerateXmlObjects
// {
// get => Settings.GenerateXmlObjects;
// set => Settings.GenerateXmlObjects = value;
// }

// [Argument(Name = "GenerateAbstractProperties", IsRequired = false, Description = "Generate abstract properties (i.e. interface and abstract properties. Properties may defined multiple times in a inheritance hierarchy, default: false).")]
// public bool GenerateAbstractProperties
// {
// get => Settings.GenerateAbstractProperties;
// set => Settings.GenerateAbstractProperties = value;
// }

// [Argument(Name = "ServiceHost", IsRequired = false, Description = "Overrides the service host of the web service (optional, use '.' to remove the hostname).")]
// public string ServiceHost { get; set; }

// [Argument(Name = "ServiceBasePath", IsRequired = false, Description = "The basePath of the Swagger specification (optional).")]
// public string ServiceBasePath { get; set; }

// [Argument(Name = "ServiceSchemes", IsRequired = false, Description = "Overrides the allowed schemes of the web service (optional, comma separated, 'http', 'https', 'ws', 'wss').")]
// public string[] ServiceSchemes { get; set; }

// [Argument(Name = "InfoTitle", IsRequired = false, Description = "Specify the title of the Swagger specification.")]
// public string InfoTitle
// {
// get => Settings.Title;
// set => Settings.Title = value;
// }

// [Argument(Name = "InfoDescription", IsRequired = false, Description = "Specify the description of the Swagger specification.")]
// public string InfoDescription
// {
// get => Settings.Description;
// set => Settings.Description = value;
// }

// [Argument(Name = "InfoVersion", IsRequired = false, Description = "Specify the version of the Swagger specification (default: 1.0.0).")]
// public string InfoVersion
// {
// get => Settings.Version;
// set => Settings.Version = value;
// }

// [Argument(Name = "DocumentTemplate", IsRequired = false, Description = "Specifies the Swagger document template (may be a path or JSON, default: none).")]
// public string DocumentTemplate { get; set; }

// [Argument(Name = "DocumentProcessors", IsRequired = false, Description = "Gets the document processor type names in the form 'assemblyName:fullTypeName' or 'fullTypeName').")]
// public string[] DocumentProcessorTypes
// {
// get => Settings.DocumentProcessorTypes;
// set => Settings.DocumentProcessorTypes = value;
// }

// [Argument(Name = "OperationProcessors", IsRequired = false, Description = "Gets the operation processor type names in the form 'assemblyName:fullTypeName' or 'fullTypeName').")]
// public string[] OperationProcessorTypes
// {
// get => Settings.OperationProcessorTypes;
// set => Settings.OperationProcessorTypes = value;
// }

// public async Task<object> RunAsync(CommandLineProcessor processor, IConsoleHost host)
// {
// var serviceProvider = ApplicationServiceProvider.GetServiceProvider(ApplicationName);
// var apiDescriptionProvider = serviceProvider.GetRequiredService<IApiDescriptionGroupCollectionProvider>();

// var swaggerGenerator = new AspNetCoreToSwaggerGenerator(Settings);
// var swaggerDocument = await swaggerGenerator.GenerateAsync(apiDescriptionProvider.ApiDescriptionGroups);
// File.WriteAllText(OutputPath, swaggerDocument.ToJson());

// return swaggerDocument;
// }
// }
//}
29 changes: 29 additions & 0 deletions src/NSwag.Commands.AspNetCore/NSwag.Commands.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp1.0;net451</TargetFrameworks>
<OutputType>Exe</OutputType>
<Description>NSwag: The Swagger API toolchain for .NET and TypeScript</Description>
<Version>11.9.3</Version>
<PackageTags>Swagger Documentation WebApi AspNet TypeScript CodeGen</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>http://NSwag.org</PackageProjectUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\NSwag.snk</AssemblyOriginatorKeyFile>
<Authors>Rico Suter</Authors>
<PackageIconUrl>https://raw.githubusercontent.com/NSwag/NSwag/master/assets/NuGetIcon.png</PackageIconUrl>
<Company />
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<IncludeBuildOutput>false</IncludeBuildOutput>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\NSwag.SwaggerGeneration.AspNetCore\NSwag.SwaggerGeneration.AspNetCore.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="build\**\*.*" Pack="true" PackagePath="%(Identity)" />
<None Include="$(OutputPath)netcoreapp1.0\$(AssemblyName).dll" Pack="true" PackagePath="build\netstandard1.0\$(AssemblyName).dll" />
</ItemGroup>
</Project>
33 changes: 33 additions & 0 deletions src/NSwag.Commands.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

using System.IO;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;
using NSwag.SwaggerGeneration.AspNetCore;

namespace NSwag.Commands.AspNetCore
{
internal class Program
{
public static int Main(string[] args)
{
//var consoleHost = new ConsoleHost();
//var processor = new CommandLineProcessor(consoleHost);

//processor.RegisterCommand<AspNetToSwaggerInnerCommand>();
//processor.Process(args);

//return 0;


var serviceProvider = ApplicationServiceProvider.GetServiceProvider(args[0]);

var apiDescriptionProvider = serviceProvider.GetRequiredService<IApiDescriptionGroupCollectionProvider>();

var swaggerGenerator = new AspNetCoreToSwaggerGenerator(new AspNetCoreToSwaggerGeneratorSettings());
var swaggerDocument = swaggerGenerator.GenerateAsync(apiDescriptionProvider.ApiDescriptionGroups).GetAwaiter().GetResult();
File.WriteAllText(args[1], swaggerDocument.ToJson());

return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<NSwagExecutorDirectory>$(MSBuildThisFileDirectory)</NSwagExecutorDirectory>
</PropertyGroup>
</Project>
Loading