-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathOpenApiCallback.cs
More file actions
198 lines (172 loc) · 7.09 KB
/
OpenApiCallback.cs
File metadata and controls
198 lines (172 loc) · 7.09 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Expressions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
using static Microsoft.OpenApi.Extensions.OpenApiSerializableExtensions;
namespace Microsoft.OpenApi.Models
{
/// <summary>
/// Callback Object: A map of possible out-of band callbacks related to the parent operation.
/// </summary>
public class OpenApiCallback : IOpenApiSerializable, IOpenApiReferenceable, IOpenApiExtensible, IEffective<OpenApiCallback>
{
/// <summary>
/// A Path Item Object used to define a callback request and expected responses.
/// </summary>
public Dictionary<RuntimeExpression, OpenApiPathItem> PathItems { get; set; }
= new Dictionary<RuntimeExpression, OpenApiPathItem>();
/// <summary>
/// Indicates if object is populated with data or is just a reference to the data
/// </summary>
public bool UnresolvedReference { get; set; }
/// <summary>
/// Reference pointer.
/// </summary>
public OpenApiReference Reference { get; set; }
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiCallback() { }
/// <summary>
/// Initializes a copy of an <see cref="OpenApiCallback"/> object
/// </summary>
public OpenApiCallback(OpenApiCallback callback)
{
PathItems = callback?.PathItems != null ? new(callback?.PathItems) : null;
UnresolvedReference = callback?.UnresolvedReference ?? UnresolvedReference;
Reference = callback?.Reference != null ? new(callback?.Reference) : null;
Extensions = callback?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(callback.Extensions) : null;
}
/// <summary>
/// Add a <see cref="OpenApiPathItem"/> into the <see cref="PathItems"/>.
/// </summary>
/// <param name="expression">The runtime expression.</param>
/// <param name="pathItem">The path item.</param>
public void AddPathItem(RuntimeExpression expression, OpenApiPathItem pathItem)
{
if (expression == null)
{
throw Error.ArgumentNull(nameof(expression));
}
if (pathItem == null)
{
throw Error.ArgumentNull(nameof(pathItem));
}
if (PathItems == null)
{
PathItems = new Dictionary<RuntimeExpression, OpenApiPathItem>();
}
PathItems.Add(expression, pathItem);
}
/// <summary>
/// Serialize <see cref="OpenApiCallback"/> to Open Api v3.1
/// </summary>
/// <param name="writer"></param>
/// <exception cref="System.NotImplementedException"></exception>
public void SerializeAsV31(IOpenApiWriter writer)
{
SerializeInternal(writer, (writer, element) => element.SerializeAsV31(writer),
(writer, referenceElement) => referenceElement.SerializeAsV31WithoutReference(writer));
}
/// <summary>
/// Serialize <see cref="OpenApiCallback"/> to Open Api v3.0
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
SerializeInternal(writer, (writer, element) => element.SerializeAsV3(writer),
(writer, referenceElement) => referenceElement.SerializeAsV3WithoutReference(writer));
}
/// <summary>
/// Serialize <see cref="OpenApiCallback"/>
/// </summary>
/// <param name="writer"></param>
/// <param name="callback"></param>
/// <param name="action"></param>
private void SerializeInternal(IOpenApiWriter writer,
Action<IOpenApiWriter, IOpenApiSerializable> callback,
Action<IOpenApiWriter, IOpenApiReferenceable> action)
{
writer = writer ?? throw Error.ArgumentNull(nameof(writer));
var target = this;
if (Reference != null)
{
if (!writer.GetSettings().ShouldInlineReference(Reference))
{
callback(writer, Reference);
return;
}
else
{
target = GetEffective(Reference.HostDocument);
}
}
action(writer, target);
}
/// <summary>
/// Returns an effective OpenApiCallback object based on the presence of a $ref
/// </summary>
/// <param name="doc">The host OpenApiDocument that contains the reference.</param>
/// <returns>OpenApiCallback</returns>
public OpenApiCallback GetEffective(OpenApiDocument doc)
{
if (this.Reference != null)
{
return doc.ResolveReferenceTo<OpenApiCallback>(this.Reference);
}
else
{
return this;
}
}
/// <summary>
/// Serialize to OpenAPI V31 document without using reference.
/// </summary>
public void SerializeAsV31WithoutReference(IOpenApiWriter writer)
{
SerializeInternalWithoutReference(writer, OpenApiSpecVersion.OpenApi3_1,
(writer, element) => element.SerializeAsV31(writer));
}
/// <summary>
/// Serialize to OpenAPI V3 document without using reference.
/// </summary>
public void SerializeAsV3WithoutReference(IOpenApiWriter writer)
{
SerializeInternalWithoutReference(writer, OpenApiSpecVersion.OpenApi3_0,
(writer, element) => element.SerializeAsV3(writer));
}
private void SerializeInternalWithoutReference(IOpenApiWriter writer, OpenApiSpecVersion version,
Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
writer.WriteStartObject();
// path items
foreach (var item in PathItems)
{
writer.WriteRequiredObject(item.Key.Expression, item.Value, callback);
}
// extensions
writer.WriteExtensions(Extensions, version);
writer.WriteEndObject();
}
/// <summary>
/// Serialize <see cref="OpenApiCallback"/> to Open Api v2.0
/// </summary>
public void SerializeAsV2(IOpenApiWriter writer)
{
// Callback object does not exist in V2.
}
/// <summary>
/// Serialize to OpenAPI V2 document without using reference.
/// </summary>
public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
{
// Callback object does not exist in V2.
}
}
}