-
Notifications
You must be signed in to change notification settings - Fork 198
updated cli tool to be used to generate openapi document #337
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a729a9f
updated cli tool to be used to generate openapi document
7208707
reverted back to .net5.0
ca3ef37
Merge branch 'main' into cli-refactor
dd8a750
Merge branch 'main' into cli-refactor
5affcb2
updated to .net6.0
dda5092
added back cli npm build code
fb07b8c
changed namespaces
9340d00
Merge branch 'main' into cli-refactor
be46695
Changed to net6.0 and OpenApiVersionType.V2
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI/Abstractions/ICustomApiMockCreator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI.Model; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI.Abstractions | ||
{ | ||
public interface ICustomApiMockCreator | ||
{ | ||
ApiMock SetupApi(string projectPath, string configuration, string targetFramework); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI/Abstractions/ICustomOpenApiCreator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI.Abstractions | ||
{ | ||
public interface ICustomOpenApiCreator | ||
{ | ||
Task<string> CreateOpenApiDocument( | ||
string apiBaseUrl, | ||
string compiledDllPath, | ||
string routePrefix, | ||
OpenApiInfo openApiInfo, | ||
OpenApiVersionType version, | ||
OpenApiFormatType format); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI/Abstractions/ICustomOpenApiWriter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI.Abstractions | ||
{ | ||
public interface ICustomOpenApiWriter | ||
{ | ||
Task WriteOpenApiToFile(string openApiDocument, string outputPath, OpenApiFormatType format); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI/ConsoleApp/GenerateSwaggerConsoleApp.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Threading.Tasks; | ||
using Cocona; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI.Abstractions; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI.Extensions; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI.ConsoleApp | ||
{ | ||
public class GenerateSwaggerConsoleApp | ||
{ | ||
/// <summary> | ||
/// Generates the OpenAPI document. | ||
/// </summary> | ||
/// <param name="project">Project path.</param> | ||
/// <param name="apibaseurl"></param> | ||
/// <param name="configuration">Compile configuration.</param> | ||
/// <param name="target"></param> | ||
/// <param name="version">OpenAPI version.</param> | ||
/// <param name="format">OpenAPI output format.</param> | ||
/// <param name="output">Output path.</param> | ||
public async Task Run( | ||
[FromService] ICustomApiMockCreator customApiMockCreator, | ||
[FromService] ICustomOpenApiCreator customOpenApiCreator, | ||
[FromService] ICustomOpenApiWriter customOpenApiWriter, | ||
[Option('p', Description = "Api Project path. Default is current directory")] | ||
string project = ".", | ||
[Option('a', Description = "ApiBaseUrl. Default is 'localhost'")] | ||
string apibaseurl = "localhost", | ||
[Option('c', Description = "Configuration. Default is 'Debug'")] | ||
string configuration = "Debug", | ||
[Option('t', Description = "Target framework. Default is 'net6.0'")] | ||
string target = "net6.0", | ||
[Option('v', Description = "OpenAPI spec version. Value can be either 'v2' or 'v3'. Default is 'v2'")] | ||
OpenApiVersionType version = OpenApiVersionType.V2, | ||
[Option('f', Description = "OpenAPI output format. Value can be either 'json' or 'yaml'. Default is 'json'")] | ||
OpenApiFormatType format = OpenApiFormatType.Json, | ||
[Option('o', Description = "Generated OpenAPI output location. Default is 'output'")] | ||
string output = "output") | ||
{ | ||
var apiMock = customApiMockCreator.SetupApi(project, configuration, target); | ||
|
||
var openApiDocument = await customOpenApiCreator.CreateOpenApiDocument( | ||
apibaseurl, | ||
apiMock.CompiledDllPath, | ||
apiMock.HttpSettings.RoutePrefix, | ||
apiMock.OpenApiInfo, | ||
version, | ||
format); | ||
|
||
await customOpenApiWriter.WriteOpenApiToFile(openApiDocument, output.GetOutputPath(apiMock.CompiledPath), format); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI/Extensions/ProjectPathExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Xml; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI.Extensions | ||
{ | ||
public static class ProjectPathExtensions | ||
{ | ||
public static readonly char DirectorySeparator = Path.DirectorySeparatorChar; | ||
|
||
public static string TrimProjectPath(this string path) | ||
{ | ||
var filePath = !Path.IsPathFullyQualified(path) | ||
? $"{Environment.CurrentDirectory.TrimEnd(DirectorySeparator)}{DirectorySeparator}{path}" | ||
: path; | ||
|
||
return new DirectoryInfo(filePath).FullName.TrimEnd(DirectorySeparator); | ||
} | ||
|
||
public static string GetCsProjFileName(this string path) | ||
{ | ||
var filePath = !Path.IsPathFullyQualified(path) | ||
? $"{Environment.CurrentDirectory.TrimEnd(DirectorySeparator)}{DirectorySeparator}{path}" | ||
: path; | ||
|
||
var segments = new DirectoryInfo(filePath).FullName.Split(new[] | ||
{ | ||
DirectorySeparator | ||
}, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
return $"{segments.Last()}.csproj"; | ||
} | ||
|
||
public static string GetProjectDllFileName(this string projectPath, string csprojFileName) | ||
{ | ||
var doc = new XmlDocument(); | ||
|
||
doc.Load($"{projectPath}{DirectorySeparator}{csprojFileName}"); | ||
|
||
var elements = doc.GetElementsByTagName(nameof(AssemblyName)); | ||
|
||
var dllName = elements?.Cast<XmlNode>()?.FirstOrDefault()?.InnerText; | ||
|
||
return string.IsNullOrWhiteSpace(dllName) | ||
? csprojFileName.Replace(".csproj", ".dll") | ||
: $"{dllName}.dll"; | ||
} | ||
|
||
public static string GetProjectCompiledPath(this string path, string configuration, string targetFramework) | ||
{ | ||
return $"{path.TrimEnd(DirectorySeparator)}{DirectorySeparator}bin{DirectorySeparator}{configuration}{DirectorySeparator}{targetFramework}"; | ||
} | ||
|
||
public static string GetProjectCompiledDllPath(this string compiledPath, string dllFileName) | ||
{ | ||
return $"{compiledPath}{DirectorySeparator}{dllFileName}"; | ||
} | ||
|
||
public static string GetProjectHostJsonPath(this string compiledPath) | ||
{ | ||
return $"{compiledPath}{DirectorySeparator}host.json"; | ||
} | ||
|
||
public static string GetOutputPath(this string output, string compiledPath) | ||
{ | ||
return !Path.IsPathFullyQualified(output) | ||
? $"{compiledPath}{DirectorySeparator}{output}" | ||
: output; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI/Extensions/SetupHostExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Configurations.AppSettings.Extensions; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI.Extensions | ||
{ | ||
public static class SetupHostExtensions | ||
{ | ||
public static HttpSettings SetHostSettings(this string hostJsonPath) | ||
{ | ||
var host = new ConfigurationBuilder() | ||
.SetBasePath(hostJsonPath.Replace("host.json", "")) | ||
.AddJsonFile("host.json") | ||
.AddEnvironmentVariables() | ||
.Build(); | ||
|
||
var version = host.GetSection("version").Value; | ||
|
||
HttpSettings hostJsonSetting; | ||
if (string.IsNullOrWhiteSpace(version)) | ||
hostJsonSetting = host.Get<HttpSettings>("http"); | ||
else if (version.Equals("2.0", StringComparison.CurrentCultureIgnoreCase)) | ||
hostJsonSetting = host.Get<HttpSettings>("extensions:http"); | ||
else | ||
hostJsonSetting = host.Get<HttpSettings>("http"); | ||
|
||
return hostJsonSetting; | ||
} | ||
|
||
public static OpenApiInfo SetOpenApiInfo(this string compiledDllPath) | ||
{ | ||
var assembly = Assembly.LoadFrom(compiledDllPath); | ||
var type = assembly.GetLoadableTypes().SingleOrDefault(p => p.GetInterface(nameof(IOpenApiConfigurationOptions), true).IsNullOrDefault() == false); | ||
return !type.IsNullOrDefault() ? (Activator.CreateInstance(type) as IOpenApiConfigurationOptions)?.Info : new DefaultOpenApiConfigurationOptions().Info; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI/Model/ApiMock.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.CLI.Model | ||
{ | ||
public class ApiMock | ||
{ | ||
public string CompiledPath { set; get; } | ||
public string CompiledDllPath { set; get; } | ||
public HttpSettings HttpSettings { set; get; } | ||
public OpenApiInfo OpenApiInfo { set; get; } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we put XML comments on the class and each property?