-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathOpenApiRequestBodyGenerator.cs
More file actions
163 lines (147 loc) · 6.75 KB
/
OpenApiRequestBodyGenerator.cs
File metadata and controls
163 lines (147 loc) · 6.75 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Linq;
using System.Collections.Generic;
using Microsoft.OData.Edm;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.Common;
namespace Microsoft.OpenApi.OData.Generator
{
/// <summary>
/// Extension methods to create <see cref="OpenApiRequestBody"/> for Edm elements.
/// </summary>
internal static class OpenApiRequestBodyGenerator
{
/// <summary>
/// Create a <see cref="OpenApiRequestBody"/> for a <see cref="IEdmActionImport"/>.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="actionImport">The Edm action import.</param>
/// <param name="document">The OpenApi document to lookup references.</param>
/// <returns>The created <see cref="OpenApiRequestBody"/> or null.</returns>
public static OpenApiRequestBody? CreateRequestBody(this ODataContext context, IEdmActionImport actionImport, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(actionImport, nameof(actionImport));
Utils.CheckArgumentNull(document, nameof(document));
return context.CreateRequestBody(actionImport.Action, document);
}
/// <summary>
/// Create a <see cref="OpenApiRequestBody"/> for a <see cref="IEdmAction"/>.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="action">The Edm action.</param>
/// <param name="document">The OpenApi document to lookup references.</param>
/// <returns>The created <see cref="OpenApiRequestBody"/> or null.</returns>
public static OpenApiRequestBody? CreateRequestBody(this ODataContext context, IEdmAction action, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(action, nameof(action));
Utils.CheckArgumentNull(document, nameof(document));
// return null for empty action parameters
int skip = 0;
if (action.IsBound)
{
if (action.Parameters.Count() <= 1)
{
return null;
}
skip = 1; //skip the binding parameter
}
else
{
if (!action.Parameters.Any())
{
return null;
}
}
OpenApiSchema parametersSchema = new OpenApiSchema
{
Type = JsonSchemaType.Object,
Properties = new Dictionary<string, IOpenApiSchema>()
};
foreach (var parameter in action.Parameters.Skip(skip))
{
parametersSchema.Properties.Add(parameter.Name, context.CreateEdmTypeSchema(parameter.Type, document));
}
OpenApiRequestBody requestBody = new OpenApiRequestBody
{
Description = "Action parameters",
Required = true,
Content = new Dictionary<string, IOpenApiMediaType>()
};
requestBody.Content.Add(Constants.ApplicationJsonMediaType, new OpenApiMediaType
{
Schema = parametersSchema
});
return requestBody;
}
/// <summary>
/// Create a dictionary of <see cref="OpenApiRequestBody"/> indexed by ref name.
/// </summary>
/// <param name="context">The OData context.</param>
/// <param name="document">The OpenApi document to lookup references.</param>
public static void AddRequestBodiesToDocument(this ODataContext context, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(document, nameof(document));
document.AddComponent(Constants.ReferencePostRequestBodyName, CreateRefPostRequestBody(document));
document.AddComponent(Constants.ReferencePutRequestBodyName, CreateRefPutRequestBody(document));
// add request bodies for actions targeting multiple related paths
foreach (IEdmAction action in context.Model.SchemaElements.OfType<IEdmAction>()
.Where(context.Model.OperationTargetsMultiplePaths))
{
if (context.CreateRequestBody(action, document) is OpenApiRequestBody requestBody)
document.AddComponent($"{action.Name}RequestBody", requestBody);
}
}
/// <summary>
/// Create a <see cref="OpenApiRequestBody"/> to be reused across ref POST operations
/// </summary>
/// <returns>The created <see cref="OpenApiRequestBody"/></returns>
/// <param name="document">The OpenApi document to lookup references.</param>
private static OpenApiRequestBody CreateRefPostRequestBody(OpenApiDocument document)
{
var schema = new OpenApiSchemaReference(Constants.ReferenceCreateSchemaName, document);
return new OpenApiRequestBody
{
Required = true,
Description = "New navigation property ref value",
Content = new Dictionary<string, IOpenApiMediaType>
{
{
Constants.ApplicationJsonMediaType, new OpenApiMediaType
{
Schema = schema
}
}
}
};
}
/// <summary>
/// Create a <see cref="OpenApiRequestBody"/> to be reused across ref PUT operations
/// </summary>
/// <returns>The created <see cref="OpenApiRequestBody"/></returns>
/// <param name="document">The OpenApi document to lookup references.</param>
private static OpenApiRequestBody CreateRefPutRequestBody(OpenApiDocument document)
{
var schema = new OpenApiSchemaReference(Constants.ReferenceUpdateSchemaName, document);
return new OpenApiRequestBody
{
Required = true,
Description = "New navigation property ref values",
Content = new Dictionary<string, IOpenApiMediaType>
{
{
Constants.ApplicationJsonMediaType, new OpenApiMediaType
{
Schema = schema
}
}
}
};
}
}
}