-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathOpenApiGenerator.cs
More file actions
83 lines (72 loc) · 2.6 KB
/
OpenApiGenerator.cs
File metadata and controls
83 lines (72 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//---------------------------------------------------------------------
// <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;
using System.Threading.Tasks;
using System.Threading;
namespace OoasUtil
{
/// <summary>
/// Open Api generator.
/// </summary>
internal abstract class OpenApiGenerator
{
/// <summary>
/// Output format.
/// </summary>
public string 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>
protected OpenApiGenerator(string output, string format, OpenApiConvertSettings settings)
{
Output = output;
Format = format;
Settings = settings;
}
/// <summary>
/// Generate the Open Api.
/// </summary>
public async Task<bool> GenerateAsync(CancellationToken cancellationToken = default)
{
try
{
IEdmModel edmModel = await GetEdmModelAsync(cancellationToken);
this.ModifySettings();
using FileStream fs = File.Create(Output);
OpenApiDocument document = edmModel.ConvertToOpenApi(Settings);
await document.SerializeAsync(fs, Settings.OpenApiSpecVersion, Format, cancellationToken).ConfigureAwait(false);
await fs.FlushAsync(cancellationToken).ConfigureAwait(false);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
protected virtual void ModifySettings()
{
}
protected abstract Task<IEdmModel> GetEdmModelAsync(CancellationToken cancellationToken = default);
}
}