-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathOpenApiRequestBodyGenerator.cs
187 lines (170 loc) · 6.9 KB
/
OpenApiRequestBodyGenerator.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// ------------------------------------------------------------
// 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.Models;
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>
/// <returns>The created <see cref="OpenApiRequestBody"/> or null.</returns>
public static OpenApiRequestBody CreateRequestBody(this ODataContext context, IEdmActionImport actionImport)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(actionImport, nameof(actionImport));
return context.CreateRequestBody(actionImport.Action);
}
/// <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>
/// <returns>The created <see cref="OpenApiRequestBody"/> or null.</returns>
public static OpenApiRequestBody CreateRequestBody(this ODataContext context, IEdmAction action)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(action, nameof(action));
// 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 = "object",
Properties = new Dictionary<string, OpenApiSchema>()
};
bool allParamsNullable = true;
foreach (var parameter in action.Parameters.Skip(skip))
{
allParamsNullable &= parameter.Type.IsNullable;
parametersSchema.Properties.Add(parameter.Name, context.CreateEdmTypeSchema(parameter.Type));
}
OpenApiRequestBody requestBody = new()
{
Description = "Action parameters",
Required = !allParamsNullable,
Content = new Dictionary<string, OpenApiMediaType>()
};
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>
/// <returns>The created dictionary of <see cref="OpenApiRequestBody"/> indexed by ref name</returns>
public static IDictionary<string, OpenApiRequestBody> CreateRequestBodies(this ODataContext context)
{
Utils.CheckArgumentNull(context, nameof(context));
Dictionary<string, OpenApiRequestBody> requestBodies = new()
{
{
Constants.ReferencePostRequestBodyName,
CreateRefPostRequestBody()
},
{
Constants.ReferencePutRequestBodyName,
CreateRefPutRequestBody()
}
};
// add request bodies for actions targeting multiple related paths
foreach (IEdmAction action in context.Model.SchemaElements.OfType<IEdmAction>()
.Where(action => context.Model.OperationTargetsMultiplePaths(action)))
{
OpenApiRequestBody requestBody = context.CreateRequestBody(action);
if (requestBody != null)
requestBodies.Add($"{action.Name}RequestBody", requestBody);
}
return requestBodies;
}
/// <summary>
/// Create a <see cref="OpenApiRequestBody"/> to be reused across ref POST operations
/// </summary>
/// <returns>The created <see cref="OpenApiRequestBody"/></returns>
private static OpenApiRequestBody CreateRefPostRequestBody()
{
OpenApiSchema schema = new()
{
UnresolvedReference = true,
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = Constants.ReferenceCreateSchemaName
}
};
return new OpenApiRequestBody
{
Required = true,
Description = "New navigation property ref value",
Content = new Dictionary<string, OpenApiMediaType>
{
{
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>
private static OpenApiRequestBody CreateRefPutRequestBody()
{
OpenApiSchema schema = new()
{
UnresolvedReference = true,
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = Constants.ReferenceUpdateSchemaName
}
};
return new OpenApiRequestBody
{
Required = true,
Description = "New navigation property ref values",
Content = new Dictionary<string, OpenApiMediaType>
{
{
Constants.ApplicationJsonMediaType, new OpenApiMediaType
{
Schema = schema
}
}
}
};
}
}
}