-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathOpenApiCallback.cs
102 lines (85 loc) · 3.51 KB
/
OpenApiCallback.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
// 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.Models.Interfaces;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Models
{
/// <summary>
/// Callback Object: A map of possible out-of band callbacks related to the parent operation.
/// </summary>
public class OpenApiCallback : IOpenApiReferenceable, IOpenApiExtensible, IOpenApiCallback
{
/// <inheritdoc/>
public Dictionary<RuntimeExpression, IOpenApiPathItem> PathItems { 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(IOpenApiCallback callback)
{
PathItems = callback?.PathItems != null ? new(callback?.PathItems) : null;
Extensions = callback?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(callback.Extensions) : null;
}
/// <summary>
/// Add a <see cref="IOpenApiPathItem"/> 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, IOpenApiPathItem pathItem)
{
Utils.CheckArgumentNull(expression);
Utils.CheckArgumentNull(pathItem);
PathItems ??= new();
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, OpenApiSpecVersion.OpenApi3_1, (writer, element) => element.SerializeAsV31(writer));
}
/// <summary>
/// Serialize <see cref="OpenApiCallback"/> to Open Api v3.0
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer));
}
internal void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
Utils.CheckArgumentNull(writer);
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.
}
}
}