c1aa5a6f94
* Support many conversion options in the command-line tool. Support local reference files in file-based csdl in the library and command-line tool. * Added tests for references. * Updates from github review.
84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
//---------------------------------------------------------------------
|
|
// <copyright file="OpenApiGenerator.cs" company="Microsoft">
|
|
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
|
|
// </copyright>
|
|
//---------------------------------------------------------------------
|
|
|
|
using System;
|
|
using System.IO;
|
|
using Microsoft.OData.Edm;
|
|
using Microsoft.OpenApi;
|
|
using Microsoft.OpenApi.Extensions;
|
|
using Microsoft.OpenApi.Models;
|
|
using Microsoft.OpenApi.OData;
|
|
|
|
namespace OoasUtil
|
|
{
|
|
/// <summary>
|
|
/// Open Api generator.
|
|
/// </summary>
|
|
internal abstract class OpenApiGenerator
|
|
{
|
|
/// <summary>
|
|
/// Output format.
|
|
/// </summary>
|
|
public OpenApiFormat Format { get; }
|
|
|
|
/// <summary>
|
|
/// Output file.
|
|
/// </summary>
|
|
public string Output { get; }
|
|
|
|
/// <summary>
|
|
/// Settings to use for conversion.
|
|
/// </summary>
|
|
public OpenApiConvertSettings Settings { get; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="OpenApiGenerator"/> class.
|
|
/// </summary>
|
|
/// <param name="output">The output.</param>
|
|
/// <param name="format">The output format.</param>
|
|
/// <param name="settings">Conversion settings.</param>
|
|
public OpenApiGenerator(string output, OpenApiFormat format, OpenApiConvertSettings settings)
|
|
{
|
|
Output = output;
|
|
Format = format;
|
|
Settings = settings;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate the Open Api.
|
|
/// </summary>
|
|
public bool Generate()
|
|
{
|
|
try
|
|
{
|
|
IEdmModel edmModel = GetEdmModel();
|
|
|
|
this.ModifySettings();
|
|
|
|
using (FileStream fs = File.Create(Output))
|
|
{
|
|
OpenApiDocument document = edmModel.ConvertToOpenApi(Settings);
|
|
document.Serialize(fs, Settings.OpenApiSpecVersion, Format);
|
|
fs.Flush();
|
|
}
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected virtual void ModifySettings()
|
|
{
|
|
}
|
|
|
|
protected abstract IEdmModel GetEdmModel();
|
|
}
|
|
}
|