1- // Copyright (c) Microsoft Corporation. All rights reserved.
1+ // Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
44using System . Globalization ;
@@ -21,12 +21,28 @@ public static class OpenApiSerializableExtensions
2121 /// <param name="stream">The output stream.</param>
2222 /// <param name="specVersion">The Open API specification version.</param>
2323 /// <param name="cancellationToken">The cancellation token.</param>
24+ #pragma warning disable RS0027 // The settings overload below has the same parameter count but different types; no ambiguity exists.
2425 public static Task SerializeAsJsonAsync < T > ( this T element , Stream stream , OpenApiSpecVersion specVersion , CancellationToken cancellationToken = default )
26+ #pragma warning restore RS0027
2527 where T : IOpenApiSerializable
2628 {
2729 return element . SerializeAsync ( stream , specVersion , OpenApiConstants . Json , cancellationToken ) ;
2830 }
2931
32+ /// <summary>
33+ /// Serialize the <see cref="IOpenApiSerializable"/> to the Open API document (JSON) using the given stream, specification version and settings.
34+ /// </summary>
35+ /// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
36+ /// <param name="element">The Open API element.</param>
37+ /// <param name="stream">The output stream.</param>
38+ /// <param name="specVersion">The Open API specification version.</param>
39+ /// <param name="settings">Settings controlling JSON output, including <see cref="OpenApiJsonWriterSettings.Terse"/> for compact formatting.</param>
40+ public static Task SerializeAsJsonAsync < T > ( this T element , Stream stream , OpenApiSpecVersion specVersion , OpenApiJsonWriterSettings settings )
41+ where T : IOpenApiSerializable
42+ {
43+ return element . SerializeAsync ( stream , specVersion , OpenApiConstants . Json , settings , CancellationToken . None ) ;
44+ }
45+
3046 /// <summary>
3147 /// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document (YAML) using the given stream and specification version.
3248 /// </summary>
@@ -104,7 +120,9 @@ public static Task SerializeAsync<T>(
104120 /// <param name="writer">The output writer.</param>
105121 /// <param name="specVersion">Version of the specification the output should conform to</param>
106122 /// <param name="cancellationToken">The cancellation token.</param>
123+ #pragma warning disable RS0027 // The settings-bearing SerializeAsync overloads below have the same parameter count but different types; no ambiguity exists.
107124 public static Task SerializeAsync < T > ( this T element , IOpenApiWriter writer , OpenApiSpecVersion specVersion , CancellationToken cancellationToken = default )
125+ #pragma warning restore RS0027
108126 where T : IOpenApiSerializable
109127 {
110128 Utils . CheckArgumentNull ( element ) ;
@@ -142,15 +160,33 @@ public static Task SerializeAsync<T>(this T element, IOpenApiWriter writer, Open
142160 /// <param name="element">The Open API element.</param>
143161 /// <param name="specVersion">The Open API specification version.</param>
144162 /// <param name="cancellationToken">The cancellation token.</param>
163+ #pragma warning disable RS0027 // The settings overload below has the same parameter count but different types; no ambiguity exists.
145164 public static Task < string > SerializeAsJsonAsync < T > (
146165 this T element ,
147166 OpenApiSpecVersion specVersion ,
148167 CancellationToken cancellationToken = default )
168+ #pragma warning restore RS0027
149169 where T : IOpenApiSerializable
150170 {
151171 return element . SerializeAsync ( specVersion , OpenApiConstants . Json , cancellationToken ) ;
152172 }
153173
174+ /// <summary>
175+ /// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document as a string in JSON format using the given settings.
176+ /// </summary>
177+ /// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
178+ /// <param name="element">The Open API element.</param>
179+ /// <param name="specVersion">The Open API specification version.</param>
180+ /// <param name="settings">Settings controlling JSON output, including <see cref="OpenApiJsonWriterSettings.Terse"/> for compact formatting.</param>
181+ public static Task < string > SerializeAsJsonAsync < T > (
182+ this T element ,
183+ OpenApiSpecVersion specVersion ,
184+ OpenApiJsonWriterSettings settings )
185+ where T : IOpenApiSerializable
186+ {
187+ return element . SerializeAsync ( specVersion , OpenApiConstants . Json , settings ) ;
188+ }
189+
154190 /// <summary>
155191 /// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document as a string in YAML format.
156192 /// </summary>
@@ -175,11 +211,13 @@ public static Task<string> SerializeAsYamlAsync<T>(
175211 /// <param name="specVersion">The Open API specification version.</param>
176212 /// <param name="format">Open API document format.</param>
177213 /// <param name="cancellationToken">The cancellation token.</param>
214+ #pragma warning disable RS0027 // The settings overload below has the same parameter count but different types; no ambiguity exists.
178215 public static async Task < string > SerializeAsync < T > (
179216 this T element ,
180217 OpenApiSpecVersion specVersion ,
181218 string format ,
182219 CancellationToken cancellationToken = default )
220+ #pragma warning restore RS0027
183221 where T : IOpenApiSerializable
184222 {
185223 Utils . CheckArgumentNull ( element ) ;
@@ -195,5 +233,30 @@ public static async Task<string> SerializeAsync<T>(
195233 return await streamReader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
196234#endif
197235 }
236+
237+ /// <summary>
238+ /// Serializes the <see cref="IOpenApiSerializable"/> to the Open API document as a string in the given format using the given settings.
239+ /// </summary>
240+ /// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
241+ /// <param name="element">The Open API element.</param>
242+ /// <param name="specVersion">The Open API specification version.</param>
243+ /// <param name="format">Open API document format.</param>
244+ /// <param name="settings">Provide configuration settings for controlling writing output.</param>
245+ public static async Task < string > SerializeAsync < T > (
246+ this T element ,
247+ OpenApiSpecVersion specVersion ,
248+ string format ,
249+ OpenApiWriterSettings ? settings )
250+ where T : IOpenApiSerializable
251+ {
252+ Utils . CheckArgumentNull ( element ) ;
253+
254+ using var stream = new MemoryStream ( ) ;
255+ await element . SerializeAsync ( stream , specVersion , format , settings , CancellationToken . None ) . ConfigureAwait ( false ) ;
256+ stream . Position = 0 ;
257+
258+ using var streamReader = new StreamReader ( stream ) ;
259+ return await streamReader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
260+ }
198261 }
199262}
0 commit comments