* Update Swagger Parser to save file using the tag used for generationg the APIView token

* Add tests for default tag
This commit is contained in:
Chidozie Ononiwu 2024-11-13 16:13:04 -08:00 коммит произвёл GitHub
Родитель 7b46596987
Коммит 26ca7fa291
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 42 добавлений и 14 удалений

Просмотреть файл

@ -19,6 +19,9 @@ namespace SwaggerApiParser
var output = new Option<string>(name: "--output", description: "The output file path.", var output = new Option<string>(name: "--output", description: "The output file path.",
getDefaultValue: () => "swagger.json"); getDefaultValue: () => "swagger.json");
var useTagForOutput = new Option<bool>(name: "--use-tag-for-output", description: "If set to true the output file will be named with the readmeTag used for the run",
getDefaultValue: () => false);
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", var readmeTag = new Option<string>(name: "--tag", description: "Readme tag used to generate swagger apiView",
@ -37,6 +40,7 @@ namespace SwaggerApiParser
{ {
swaggers, swaggers,
output, output,
useTagForOutput,
packageName, packageName,
swaggerLinks, swaggerLinks,
readmeFile, readmeFile,
@ -46,7 +50,7 @@ namespace SwaggerApiParser
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) => cmd.SetHandler(async (IEnumerable<string> swaggerFiles, string outputFile, bool useTagForOutputFileName, string package, IEnumerable<string> links, string readme, string tag) =>
{ {
var swaggerLinksArray = links.ToList(); var swaggerLinksArray = links.ToList();
@ -56,20 +60,20 @@ namespace SwaggerApiParser
{ {
readme = Path.GetFullPath(readme); readme = Path.GetFullPath(readme);
} }
await HandleGenerateCodeFile(enumerable, outputFile, package, swaggerLinksArray, readme, tag); await HandleGenerateCodeFile(enumerable, outputFile, useTagForOutputFileName, package, swaggerLinksArray, readme, tag);
}, swaggers, output, packageName, swaggerLinks, readmeFile, readmeTag); }, swaggers, output, useTagForOutput, 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, bool useTagForOutput, string packageName, List<string> swaggerLinks, string readmeFile, string readmeTag)
{ {
var swaggerFilePaths = swaggers.ToList(); var swaggerFilePaths = swaggers.ToList();
if (readmeFile != null) if (readmeFile != null)
{ {
var readmeFileDir = Path.GetDirectoryName(readmeFile); var readmeFileDir = Path.GetDirectoryName(readmeFile);
var swaggerFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFile, readmeTag); var swaggerFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFile, ref readmeTag);
swaggerFilePaths = swaggerFilePaths.Concat(swaggerFiles.Select(it => Path.Join(readmeFileDir, it))).ToList(); swaggerFilePaths = swaggerFilePaths.Concat(swaggerFiles.Select(it => Path.Join(readmeFileDir, it))).ToList();
} }
@ -114,6 +118,13 @@ namespace SwaggerApiParser
var codeFile = root.GenerateCodeFile(); var codeFile = root.GenerateCodeFile();
var outputFilePath = Path.GetFullPath(output); var outputFilePath = Path.GetFullPath(output);
if (useTagForOutput)
{
output = $"{readmeTag}.json";
outputFilePath = Path.Combine(Path.GetDirectoryName(outputFilePath), output);
}
await using FileStream writer = File.Open(outputFilePath, FileMode.Create); await using FileStream writer = File.Open(outputFilePath, FileMode.Create);
Console.WriteLine($"Generate codefile {output} successfully."); Console.WriteLine($"Generate codefile {output} successfully.");
await codeFile.SerializeAsync(writer); await codeFile.SerializeAsync(writer);

Просмотреть файл

@ -43,17 +43,17 @@ namespace SwaggerApiParser
return matchResult.Success ? matchResult.Groups[1].Value : ""; return matchResult.Success ? matchResult.Groups[1].Value : "";
} }
public static IEnumerable<string> GetSwaggerFilesFromReadme(string readme, string tag) public static IEnumerable<string> GetSwaggerFilesFromReadme(string readme, ref string tag)
{ {
ReadmeParser parser = new ReadmeParser(readme); ReadmeParser parser = new ReadmeParser(readme);
parser.ParseReadmeConfig(); parser.ParseReadmeConfig();
string readmeTag = tag;
if (tag == "default" && parser.basicConfig != null) if (tag == "default" && parser.basicConfig != null)
{ {
readmeTag = parser.basicConfig.tag; tag = parser.basicConfig.tag;
} }
parser.inputSwaggerFilesMap.TryGetValue(readmeTag, out InputSwaggerFiles inputFiles); parser.inputSwaggerFilesMap.TryGetValue(tag, out InputSwaggerFiles inputFiles);
return inputFiles?.input ?? Enumerable.Empty<string>(); return inputFiles?.input ?? Enumerable.Empty<string>();
} }
private void ParseReadmeConfig() private void ParseReadmeConfig()

Просмотреть файл

@ -18,7 +18,8 @@ public class ReadmeParserTest
public void TestParseReadme() public void TestParseReadme()
{ {
const string readmeFilePath = "./fixtures/apimanagementReadme.md"; const string readmeFilePath = "./fixtures/apimanagementReadme.md";
var inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, "default"); var tag = "default";
var inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
Assert.Equal(43, inputFile.ToList().Count); Assert.Equal(43, inputFile.ToList().Count);
} }
@ -26,13 +27,14 @@ public class ReadmeParserTest
public void TestGetSwaggerFileFromReadmeForAppConfiguration() public void TestGetSwaggerFileFromReadmeForAppConfiguration()
{ {
const string readmeFilePath = "./fixtures/appconfigurationreadme.md"; const string readmeFilePath = "./fixtures/appconfigurationreadme.md";
var inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, "default"); var tag = "default";
var inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
var enumerable = inputFile as string[] ?? inputFile.ToArray(); var enumerable = inputFile as string[] ?? inputFile.ToArray();
Assert.Equal("Microsoft.AppConfiguration/stable/2022-05-01/appconfiguration.json", enumerable.ToArray()[0]); Assert.Equal("Microsoft.AppConfiguration/stable/2022-05-01/appconfiguration.json", enumerable.ToArray()[0]);
Assert.Single(enumerable.ToList()); Assert.Single(enumerable.ToList());
tag = "package-2020-06-01";
inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, "package-2020-06-01"); inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
enumerable = inputFile as string[] ?? inputFile.ToArray(); enumerable = inputFile as string[] ?? inputFile.ToArray();
Assert.Equal("Microsoft.AppConfiguration/stable/2020-06-01/appconfiguration.json", enumerable.ToArray()[0]); Assert.Equal("Microsoft.AppConfiguration/stable/2020-06-01/appconfiguration.json", enumerable.ToArray()[0]);
Assert.Single(enumerable.ToList()); Assert.Single(enumerable.ToList());
@ -77,7 +79,22 @@ public class ReadmeParserTest
public void TestOrderedInputFiles() public void TestOrderedInputFiles()
{ {
const string readmeFilePath = "./fixtures/unordered.md"; const string readmeFilePath = "./fixtures/unordered.md";
var inputFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, "package-2023-02"); var tag = "package-2023-02";
var inputFiles = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
Assert.Collection(inputFiles, x => Assert.Equal("a.json", x), x => Assert.Equal("z.json", x)); Assert.Collection(inputFiles, x => Assert.Equal("a.json", x), x => Assert.Equal("z.json", x));
} }
[Fact]
public void TestTagRetrievalUsingGetSwaggerFilesFromReadme()
{
string readmeFilePath = "./fixtures/appconfigurationreadme.md";
var tag = "default";
var inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
Assert.Equal("package-2022-05-01", tag);
readmeFilePath = "./fixtures/unordered.md";
tag = "package-2023-02";
inputFile = ReadmeParser.GetSwaggerFilesFromReadme(readmeFilePath, ref tag);
Assert.Equal("package-2023-02", tag);
}
} }