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 APIView dependency and minor fix #5319

Merged
merged 11 commits into from
Feb 8, 2023
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
@@ -1,5 +1,11 @@
# Release History

# 1.0.6 (2-2-2023)

+ Remove APIView and APIViewWeb dependency
+ Fix don't resolve response issue
+ Add null check for array item

# 1.0.5 (1-18-2023)

+ Fix root schema circular reference issue by adding root schema ref in refChain.
Expand All @@ -10,6 +16,7 @@
+ To keep APIView revision consistent. Json serialization use alwaysMultiline option.
+ Remove unused code and comments.


## 1.0.3 (11-17-2022)

+ Remove trailing colon from API path
Expand Down
165 changes: 82 additions & 83 deletions tools/apiview/parsers/swagger-api-parser/SwaggerApiParser/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,115 +5,114 @@
using System.CommandLine;
using System.Linq;

namespace SwaggerApiParser
namespace SwaggerApiParser;

internal class Program
{
internal class Program
static Task<int> Main(string[] args)
{
static Task<int> Main(string[] args)
{
var swaggers = new Option<IEnumerable<string>>("--swaggers",
"The input swagger file. Can be a single file or multiple files separated by space.");
var swaggers = new Option<IEnumerable<string>>("--swaggers",
"The input swagger file. Can be a single file or multiple files separated by space.");

var output = new Option<string>(name: "--output", description: "The output file path.",
getDefaultValue: () => "swagger.json");
var output = new Option<string>(name: "--output", description: "The output file path.",
getDefaultValue: () => "swagger.json");

var readmeFile = new Option<string>(name: "--readme", "The input readme file.");
var readmeFile = new Option<string>(name: "--readme", "The input readme file.");

var readmeTag = new Option<string>(name: "--tag", description: "Readme tag used to generate swagger apiView",
getDefaultValue: () => "default");
var readmeTag = new Option<string>(name: "--tag", description: "Readme tag used to generate swagger apiView",
getDefaultValue: () => "default");

var packageName = new Option<string>(name: "--package-name",
description: "The package name for the generated code file.",
getDefaultValue: () => "swagger");
var packageName = new Option<string>(name: "--package-name",
description: "The package name for the generated code file.",
getDefaultValue: () => "swagger");


var swaggerLinks = new Option<IEnumerable<string>>(name: "--swaggerLinks",
description: "The input swagger links. Can be a single URL or multiple URLs separated by space.", getDefaultValue:
() => new List<string>()) {AllowMultipleArgumentsPerToken = true};
var swaggerLinks = new Option<IEnumerable<string>>(name: "--swaggerLinks",
description: "The input swagger links. Can be a single URL or multiple URLs separated by space.", getDefaultValue:
() => new List<string>()) {AllowMultipleArgumentsPerToken = true};

var cmd = new RootCommand
{
swaggers,
output,
packageName,
swaggerLinks,
readmeFile,
readmeTag
};
var cmd = new RootCommand
{
swaggers,
output,
packageName,
swaggerLinks,
readmeFile,
readmeTag
};


cmd.Description = "Parse swagger file into codefile.";
cmd.Description = "Parse swagger file into codefile.";

cmd.SetHandler(async (IEnumerable<string> swaggerFiles, string outputFile, string package, IEnumerable<string> links, string readme, string tag) =>
{
var swaggerLinksArray = links.ToList();
cmd.SetHandler(async (IEnumerable<string> swaggerFiles, string outputFile, string package, IEnumerable<string> links, string readme, string tag) =>
{
var swaggerLinksArray = links.ToList();

var enumerable = swaggerFiles as string[] ?? swaggerFiles.ToArray();
var enumerable = swaggerFiles as string[] ?? swaggerFiles.ToArray();

if (readme != null)
{
readme = Path.GetFullPath(readme);
}
await HandleGenerateCodeFile(enumerable, outputFile, package, swaggerLinksArray, readme, tag);
}, swaggers, output, packageName, swaggerLinks, readmeFile, readmeTag);
if (readme != null)
{
readme = Path.GetFullPath(readme);
}
await HandleGenerateCodeFile(enumerable, outputFile, package, swaggerLinksArray, readme, tag);
}, swaggers, output, packageName, swaggerLinks, readmeFile, readmeTag);

return Task.FromResult(cmd.Invoke(args));
}
return Task.FromResult(cmd.Invoke(args));
}

static async Task HandleGenerateCodeFile(IEnumerable<string> swaggers, string output, string packageName, List<string> swaggerLinks, string readmeFile, string readmeTag)
{
static async Task HandleGenerateCodeFile(IEnumerable<string> swaggers, string output, string packageName, List<string> swaggerLinks, string readmeFile, string readmeTag)
{

var swaggerFilePaths = swaggers.ToList();
if (readmeFile != null)
{
var readmeFileDir = Path.GetDirectoryName(readmeFile);
var swaggerFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFile, readmeTag);
swaggerFilePaths = swaggerFilePaths.Concat(swaggerFiles.Select(it => Path.Join(readmeFileDir, it))).ToList();
}
var swaggerFilePaths = swaggers.ToList();
if (readmeFile != null)
{
var readmeFileDir = Path.GetDirectoryName(readmeFile);
var swaggerFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFile, readmeTag);
swaggerFilePaths = swaggerFilePaths.Concat(swaggerFiles.Select(it => Path.Join(readmeFileDir, it))).ToList();
}

if (!swaggerFilePaths.Any())
{
Console.WriteLine("No swagger files specified. For usage, run with --help");
return;
}
if (!swaggerFilePaths.Any())
{
Console.WriteLine("No swagger files specified. For usage, run with --help");
return;
}


SwaggerApiViewRoot root = new SwaggerApiViewRoot(packageName, packageName);
SwaggerApiViewRoot root = new SwaggerApiViewRoot(packageName, packageName);


List<SwaggerSpec> swaggerSpecs = new List<SwaggerSpec>();
List<SwaggerSpec> swaggerSpecs = new List<SwaggerSpec>();

var idx = 0;
foreach (var swaggerFilePath in swaggerFilePaths)
var idx = 0;
foreach (var swaggerFilePath in swaggerFilePaths)
{
if (!File.Exists(swaggerFilePath))
{
if (!File.Exists(swaggerFilePath))
{
throw new FileNotFoundException(swaggerFilePath);
}

var swaggerLink = idx < swaggerLinks.Count ? swaggerLinks[idx] : "";
idx++;

var input = Path.GetFullPath(swaggerFilePath);
Console.WriteLine("Generating codefile for swagger file: {0}", Path.GetFileName(input));
var swaggerSpec = await SwaggerDeserializer.Deserialize(input);
swaggerSpec.swaggerFilePath = Path.GetFullPath(swaggerFilePath);
swaggerSpec.swaggerLink = swaggerLink;
swaggerSpecs.Add(swaggerSpec);
root.AddDefinitionToCache(swaggerSpec, swaggerSpec.swaggerFilePath);

throw new FileNotFoundException(swaggerFilePath);
}

foreach (var swaggerSpec in swaggerSpecs)
{
root.AddSwaggerSpec(swaggerSpec, swaggerSpec.swaggerFilePath, packageName, swaggerSpec.swaggerLink);
}
var swaggerLink = idx < swaggerLinks.Count ? swaggerLinks[idx] : "";
idx++;

var codeFile = root.GenerateCodeFile();
var outputFilePath = Path.GetFullPath(output);
await using FileStream writer = File.Open(outputFilePath, FileMode.Create);
Console.WriteLine($"Generate codefile {output} successfully.");
await codeFile.SerializeAsync(writer);
var input = Path.GetFullPath(swaggerFilePath);
Console.WriteLine("Generating codefile for swagger file: {0}", Path.GetFileName(input));
var swaggerSpec = await SwaggerDeserializer.Deserialize(input);
swaggerSpec.swaggerFilePath = Path.GetFullPath(swaggerFilePath);
swaggerSpec.swaggerLink = swaggerLink;
swaggerSpecs.Add(swaggerSpec);
root.AddDefinitionToCache(swaggerSpec, swaggerSpec.swaggerFilePath);

}

foreach (var swaggerSpec in swaggerSpecs)
{
root.AddSwaggerSpec(swaggerSpec, swaggerSpec.swaggerFilePath, packageName, swaggerSpec.swaggerLink);
}

var codeFile = root.GenerateCodeFile();
var outputFilePath = Path.GetFullPath(output);
await using FileStream writer = File.Open(outputFilePath, FileMode.Create);
Console.WriteLine($"Generate codefile {output} successfully.");
await codeFile.SerializeAsync(writer);
}
}
}
Loading