-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathEdmModelOpenApiExtensions.cs
More file actions
61 lines (55 loc) · 2.36 KB
/
EdmModelOpenApiExtensions.cs
File metadata and controls
61 lines (55 loc) · 2.36 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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Collections.Generic;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Validation;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.Generator;
namespace Microsoft.OpenApi.OData
{
/// <summary>
/// Extension methods to convert <see cref="IEdmModel"/> to <see cref="OpenApiDocument"/>.
/// </summary>
public static class EdmModelOpenApiExtensions
{
/// <summary>
/// Convert <see cref="IEdmModel"/> to <see cref="OpenApiDocument"/> using default settings.
/// </summary>
/// <param name="model">The Edm model.</param>
/// <returns>The converted Open API document object, <see cref="OpenApiDocument"/>.</returns>
public static OpenApiDocument ConvertToOpenApi(this IEdmModel model)
{
return model.ConvertToOpenApi(new OpenApiConvertSettings());
}
/// <summary>
/// Convert <see cref="IEdmModel"/> to <see cref="OpenApiDocument"/> using a convert settings.
/// </summary>
/// <param name="model">The Edm model.</param>
/// <param name="settings">The convert settings.</param>
/// <returns>The converted Open API document object, <see cref="OpenApiDocument"/>.</returns>
public static OpenApiDocument ConvertToOpenApi(this IEdmModel model, OpenApiConvertSettings settings)
{
Utils.CheckArgumentNull(model, nameof(model));
Utils.CheckArgumentNull(settings, nameof(settings));
if (settings.VerifyEdmModel && !model.Validate(out var errors))
{
OpenApiDocument document = new();
int index = 1;
document.Extensions ??= new Dictionary<string, IOpenApiExtension>();
foreach (var error in errors)
{
document.Extensions.Add(Constants.xMsEdmModelError + index++, new JsonNodeExtension(error.ToString()));
}
return document;
}
ODataContext context = new(model, settings);
return context.CreateDocument();
}
}
}