Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (83 loc) · 3.74 KB
/
Copy pathProgram.cs
File metadata and controls
98 lines (83 loc) · 3.74 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//---------------------------------------------------------------------
// <copyright file="Program.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 System.Xml.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.OData;
using Microsoft.OpenApi.Extensions;
namespace UpdateDocs
{
class Program
{
static int Main(string[] args)
{
// we assume the path are existed for simplicity.
string path = Directory.GetCurrentDirectory();
string csdl = path + "/../../../../../docs/csdl";
string oas20 = path + "/../../../../../docs/oas_2_0";
string oas30 = path + "/../../../../../docs/oas3_0_0";
foreach (var filePath in Directory.GetFiles(csdl, "*.xml"))
{
Console.WriteLine(filePath);
IEdmModel model = LoadEdmModel(filePath);
if (model == null)
{
continue;
}
FileInfo fileInfo = new FileInfo(filePath);
string fileName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
OpenApiConvertSettings settings = new OpenApiConvertSettings();
if (fileName.Contains("graph.beta"))
{
settings.PrefixEntityTypeNameBeforeKey = true;
settings.ServiceRoot = new Uri("https://graph.microsoft.com/beta");
}
else if (fileName.Contains("graph1.0"))
{
settings.PrefixEntityTypeNameBeforeKey = true;
settings.ServiceRoot = new Uri("https://graph.microsoft.com/v1.0");
}
OpenApiDocument document = model.ConvertToOpenApi(settings);
string output;/* = oas20 + "/" + fileName + ".yaml";
File.WriteAllText(output, document.SerializeAsYaml(OpenApiSpecVersion.OpenApi2_0));
output = oas20 + "/" + fileName + ".json";
File.WriteAllText(output, document.SerializeAsJson(OpenApiSpecVersion.OpenApi2_0));
output = oas30 + "/" + fileName + ".yaml";
File.WriteAllText(output, document.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0));
output = oas30 + "/" + fileName + ".json";
File.WriteAllText(output, document.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0));
*/
settings.EnableKeyAsSegment = true;
settings.EnableUnqualifiedCall = true;
output = oas30 + "/" + fileName + ".json";
document = model.ConvertToOpenApi(settings);
File.WriteAllText(output, document.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0));
output = oas20 + "/" + fileName + ".json";
File.WriteAllText(output, document.SerializeAsJson(OpenApiSpecVersion.OpenApi2_0));
Console.WriteLine("Output [ " + fileName + " ] Succeessful!");
}
Console.WriteLine("\n==> All Done!");
return 0;
}
public static IEdmModel LoadEdmModel(string file)
{
try
{
string csdl = File.ReadAllText(file);
return CsdlReader.Parse(XElement.Parse(csdl).CreateReader());
}
catch
{
Console.WriteLine("Cannot load EDM from file: " + file);
return null;
}
}
}
}