-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathOpenApiSerializableExtensions.cs
More file actions
executable file
·199 lines (180 loc) · 9.49 KB
/
OpenApiSerializableExtensions.cs
File metadata and controls
executable file
·199 lines (180 loc) · 9.49 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
199
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.OpenApi
{
/// <summary>
/// Extension methods for <see cref="IOpenApiSerializable"/> serialization.
/// </summary>
public static class OpenApiSerializableExtensions
{
/// <summary>
/// Serialize the <see cref="IOpenApiSerializable"/> to the Open API document (JSON) using the given stream and specification version.
/// </summary>
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
/// <param name="element">The Open API element.</param>
/// <param name="stream">The output stream.</param>
/// <param name="specVersion">The Open API specification version.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static Task SerializeAsJsonAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, CancellationToken cancellationToken = default)
where T : IOpenApiSerializable
{
return element.SerializeAsync(stream, specVersion, OpenApiConstants.Json, cancellationToken);
}
/// <summary>
/// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document (YAML) using the given stream and specification version.
/// </summary>
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
/// <param name="element">The Open API element.</param>
/// <param name="stream">The output stream.</param>
/// <param name="specVersion">The Open API specification version.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static Task SerializeAsYamlAsync<T>(this T element, Stream stream, OpenApiSpecVersion specVersion, CancellationToken cancellationToken = default)
where T : IOpenApiSerializable
{
return element.SerializeAsync(stream, specVersion, OpenApiConstants.Yaml, cancellationToken);
}
/// <summary>
/// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document using
/// the given stream, specification version and the format.
/// </summary>
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
/// <param name="element">The Open API element.</param>
/// <param name="stream">The given stream.</param>
/// <param name="specVersion">The Open API specification version.</param>
/// <param name="format">The output format (JSON or YAML).</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static Task SerializeAsync<T>(
this T element,
Stream stream,
OpenApiSpecVersion specVersion,
string format,
CancellationToken cancellationToken = default)
where T : IOpenApiSerializable
{
return element.SerializeAsync(stream, specVersion, format, null, cancellationToken);
}
/// <summary>
/// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document using
/// the given stream, specification version and the format.
/// </summary>
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
/// <param name="element">The Open API element.</param>
/// <param name="stream">The given stream.</param>
/// <param name="specVersion">The Open API specification version.</param>
/// <param name="format">The output format (JSON or YAML).</param>
/// <param name="settings">Provide configuration settings for controlling writing output</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static Task SerializeAsync<T>(
this T element,
Stream stream,
OpenApiSpecVersion specVersion,
string format,
OpenApiWriterSettings? settings = null,
CancellationToken cancellationToken = default)
where T : IOpenApiSerializable
{
Utils.CheckArgumentNull(stream);
var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture);
IOpenApiWriter writer = format.ToLowerInvariant() switch
{
OpenApiConstants.Json when settings is OpenApiJsonWriterSettings jsonSettings => new OpenApiJsonWriter(streamWriter, jsonSettings),
OpenApiConstants.Json => new OpenApiJsonWriter(streamWriter, settings),
OpenApiConstants.Yaml => new OpenApiYamlWriter(streamWriter, settings),
_ => throw new OpenApiException(string.Format(SRResource.OpenApiFormatNotSupported, format)),
};
return element.SerializeAsync(writer, specVersion, cancellationToken);
}
/// <summary>
/// Serializes the <see cref="IOpenApiSerializable"/> to Open API document using the given specification version and writer.
/// </summary>
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
/// <param name="element">The Open API element.</param>
/// <param name="writer">The output writer.</param>
/// <param name="specVersion">Version of the specification the output should conform to</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static Task SerializeAsync<T>(this T element, IOpenApiWriter writer, OpenApiSpecVersion specVersion, CancellationToken cancellationToken = default)
where T : IOpenApiSerializable
{
Utils.CheckArgumentNull(element);
Utils.CheckArgumentNull(writer);
switch (specVersion)
{
case OpenApiSpecVersion.OpenApi3_2:
element.SerializeAsV32(writer);
break;
case OpenApiSpecVersion.OpenApi3_1:
element.SerializeAsV31(writer);
break;
case OpenApiSpecVersion.OpenApi3_0:
element.SerializeAsV3(writer);
break;
case OpenApiSpecVersion.OpenApi2_0:
element.SerializeAsV2(writer);
break;
default:
throw new OpenApiException(string.Format(SRResource.OpenApiSpecVersionNotSupported, specVersion));
}
return writer.FlushAsync(cancellationToken);
}
/// <summary>
/// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document as a string in JSON format.
/// </summary>
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
/// <param name="element">The Open API element.</param>
/// <param name="specVersion">The Open API specification version.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static Task<string> SerializeAsJsonAsync<T>(
this T element,
OpenApiSpecVersion specVersion,
CancellationToken cancellationToken = default)
where T : IOpenApiSerializable
{
return element.SerializeAsync(specVersion, OpenApiConstants.Json, cancellationToken);
}
/// <summary>
/// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document as a string in YAML format.
/// </summary>
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
/// <param name="element">The Open API element.</param>
/// <param name="specVersion">The Open API specification version.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static Task<string> SerializeAsYamlAsync<T>(
this T element,
OpenApiSpecVersion specVersion,
CancellationToken cancellationToken = default)
where T : IOpenApiSerializable
{
return element.SerializeAsync(specVersion, OpenApiConstants.Yaml, cancellationToken);
}
/// <summary>
/// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document as a string in the given format.
/// </summary>
/// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
/// <param name="element">The Open API element.</param>
/// <param name="specVersion">The Open API specification version.</param>
/// <param name="format">Open API document format.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static async Task<string> SerializeAsync<T>(
this T element,
OpenApiSpecVersion specVersion,
string format,
CancellationToken cancellationToken = default)
where T : IOpenApiSerializable
{
Utils.CheckArgumentNull(element);
using var stream = new MemoryStream();
await element.SerializeAsync(stream, specVersion, format, cancellationToken).ConfigureAwait(false);
stream.Position = 0;
using var streamReader = new StreamReader(stream);
#if NET7_0_OR_GREATER
return await streamReader.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
#else
return await streamReader.ReadToEndAsync().ConfigureAwait(false);
#endif
}
}
}