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

Feature/swagger parser upgrade #6338

Merged
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Release History

## 1.1.0 (6-26-2023)
+ Disabled flatteneing of nested model.
+ Updated definitions to include models all referenced models
+ Included definations referenced in external files
+ Ensure enum values are parsed
+ Ensure all inline schema is parsed
+ Handle allOf an additional Properties
+ Ensure definiton Properties are sorted

## 1.0.9 (5-22-2023)
+ Support schema for parameters referenced in relative files

Expand Down
174 changes: 88 additions & 86 deletions tools/apiview/parsers/swagger-api-parser/SwaggerApiParser/Program.cs
Original file line number Diff line number Diff line change
@@ -1,120 +1,122 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.CommandLine;
using System.Linq;
using SwaggerApiParser.SwaggerApiView;
using SwaggerApiParser.Specs;

namespace SwaggerApiParser;

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

var swaggerFilePaths = swaggers.ToList();
if (readmeFile != null)
static async Task HandleGenerateCodeFile(IEnumerable<string> swaggers, string output, string packageName, List<string> swaggerLinks, string readmeFile, string readmeTag)
{
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<Swagger> swaggerSpecs = new List<Swagger>();

var idx = 0;
foreach (var swaggerFilePath in swaggerFilePaths)
{
if (!File.Exists(swaggerFilePath))
var idx = 0;
foreach (var swaggerFilePath in swaggerFilePaths)
{
// Suppressing this error when swagger file is missing or path is incorrect
// Ideally any incorrect swagger file path reference in README should be caught by validation tool
Console.WriteLine($"Invalid file path to swagger file. Skipping {swaggerFilePath}.");
continue;
if (!File.Exists(swaggerFilePath))
{
// Suppressing this error when swagger file is missing or path is incorrect
// Ideally any incorrect swagger file path reference in README should be caught by validation tool
Console.WriteLine($"Invalid file path to swagger file. Skipping {swaggerFilePath}.");
continue;
}

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);
}

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);
}
foreach (var swaggerSpec in swaggerSpecs)
{
await root.AddSwaggerSpec(swaggerSpec, swaggerSpec.swaggerFilePath, packageName, swaggerSpec.swaggerLink);
}

foreach (var swaggerSpec in swaggerSpecs)
{
await 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);
}

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