-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathOpenApiPathItemGenerator.cs
More file actions
92 lines (84 loc) · 3.39 KB
/
OpenApiPathItemGenerator.cs
File metadata and controls
92 lines (84 loc) · 3.39 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
// ------------------------------------------------------------
// 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 System.Linq;
using System.Net.Http;
using Microsoft.OData.Edm;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.PathItem;
namespace Microsoft.OpenApi.OData.Generator
{
/// <summary>
/// Class to create <see cref="OpenApiPathItem"/> by Edm elements.
/// </summary>
internal static class OpenApiPathItemGenerator
{
/// <summary>
/// Create a map of <see cref="OpenApiPathItem"/>.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="document">The Open API document to use to lookup references.</param>
public static void AddPathItemsToDocument(this ODataContext context, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(document, nameof(document));
if (context.EntityContainer == null)
{
return;
}
document.Paths ??= [];
OpenApiConvertSettings settings = context.Settings.Clone();
settings.EnableKeyAsSegment = context.KeyAsSegment;
foreach (ODataPath path in context.AllPaths)
{
IPathItemHandler handler = context.PathItemHandlerProvider.GetHandler(path.Kind, document);
if (handler == null)
{
continue;
}
OpenApiPathItem pathItem = handler.CreatePathItem(context, path);
if (!pathItem.Operations.Any())
{
continue;
}
document.Paths.TryAddPath(context, path, pathItem);
}
if (settings.ShowRootPath)
{
OpenApiPathItem rootPath = new()
{
Operations = new Dictionary<HttpMethod, OpenApiOperation> {
{
HttpMethod.Get, new OpenApiOperation {
OperationId = "graphService.GetGraphService",
Responses = new OpenApiResponses()
{
{ "200",new OpenApiResponse() {
Description = "OK",
Links = CreateRootLinks(context.EntityContainer)
}
}
}
}
}
}
};
document.Paths.Add("/", rootPath);
}
}
private static Dictionary<string, IOpenApiLink> CreateRootLinks(IEdmEntityContainer entityContainer)
{
var links = new Dictionary<string, IOpenApiLink>();
foreach (var element in entityContainer.Elements)
{
links.Add(element.Name, new OpenApiLink());
}
return links;
}
}
}