33
44using System . Globalization ;
55using System . IO ;
6+ using System . Threading ;
7+ using System . Threading . Tasks ;
68using Microsoft . OpenApi . Exceptions ;
79using Microsoft . OpenApi . Interfaces ;
810using Microsoft . OpenApi . Properties ;
@@ -22,10 +24,11 @@ public static class OpenApiSerializableExtensions
2224 /// <param name="element">The Open API element.</param>
2325 /// <param name="stream">The output stream.</param>
2426 /// <param name="specVersion">The Open API specification version.</param>
25- public static void SerializeAsJson < T > ( this T element , Stream stream , OpenApiSpecVersion specVersion )
27+ /// <param name="cancellationToken">The cancellation token.</param>
28+ public static Task SerializeAsJsonAsync < T > ( this T element , Stream stream , OpenApiSpecVersion specVersion , CancellationToken cancellationToken = default )
2629 where T : IOpenApiSerializable
2730 {
28- element . Serialize ( stream , specVersion , OpenApiFormat . Json ) ;
31+ return element . SerializeAsync ( stream , specVersion , OpenApiFormat . Json , cancellationToken ) ;
2932 }
3033
3134 /// <summary>
@@ -35,10 +38,11 @@ public static void SerializeAsJson<T>(this T element, Stream stream, OpenApiSpec
3538 /// <param name="element">The Open API element.</param>
3639 /// <param name="stream">The output stream.</param>
3740 /// <param name="specVersion">The Open API specification version.</param>
38- public static void SerializeAsYaml < T > ( this T element , Stream stream , OpenApiSpecVersion specVersion )
41+ /// <param name="cancellationToken">The cancellation token.</param>
42+ public static Task SerializeAsYamlAsync < T > ( this T element , Stream stream , OpenApiSpecVersion specVersion , CancellationToken cancellationToken = default )
3943 where T : IOpenApiSerializable
4044 {
41- element . Serialize ( stream , specVersion , OpenApiFormat . Yaml ) ;
45+ return element . SerializeAsync ( stream , specVersion , OpenApiFormat . Yaml , cancellationToken ) ;
4246 }
4347
4448 /// <summary>
@@ -50,14 +54,16 @@ public static void SerializeAsYaml<T>(this T element, Stream stream, OpenApiSpec
5054 /// <param name="stream">The given stream.</param>
5155 /// <param name="specVersion">The Open API specification version.</param>
5256 /// <param name="format">The output format (JSON or YAML).</param>
53- public static void Serialize < T > (
57+ /// <param name="cancellationToken">The cancellation token.</param>
58+ public static Task SerializeAsync < T > (
5459 this T element ,
5560 Stream stream ,
5661 OpenApiSpecVersion specVersion ,
57- OpenApiFormat format )
62+ OpenApiFormat format ,
63+ CancellationToken cancellationToken = default )
5864 where T : IOpenApiSerializable
5965 {
60- element . Serialize ( stream , specVersion , format , null ) ;
66+ return element . SerializeAsync ( stream , specVersion , format , null , cancellationToken ) ;
6167 }
6268
6369 /// <summary>
@@ -70,12 +76,14 @@ public static void Serialize<T>(
7076 /// <param name="specVersion">The Open API specification version.</param>
7177 /// <param name="format">The output format (JSON or YAML).</param>
7278 /// <param name="settings">Provide configuration settings for controlling writing output</param>
73- public static void Serialize < T > (
79+ /// <param name="cancellationToken">The cancellation token.</param>
80+ public static Task SerializeAsync < T > (
7481 this T element ,
7582 Stream stream ,
7683 OpenApiSpecVersion specVersion ,
7784 OpenApiFormat format ,
78- OpenApiWriterSettings settings )
85+ OpenApiWriterSettings settings ,
86+ CancellationToken cancellationToken = default )
7987 where T : IOpenApiSerializable
8088 {
8189 Utils . CheckArgumentNull ( stream ) ;
@@ -88,7 +96,7 @@ public static void Serialize<T>(
8896 OpenApiFormat . Yaml => new OpenApiYamlWriter ( streamWriter , settings ) ,
8997 _ => throw new OpenApiException ( string . Format ( SRResource . OpenApiFormatNotSupported , format ) ) ,
9098 } ;
91- element . Serialize ( writer , specVersion ) ;
99+ return element . SerializeAsync ( writer , specVersion , cancellationToken ) ;
92100 }
93101
94102 /// <summary>
@@ -98,7 +106,8 @@ public static void Serialize<T>(
98106 /// <param name="element">The Open API element.</param>
99107 /// <param name="writer">The output writer.</param>
100108 /// <param name="specVersion">Version of the specification the output should conform to</param>
101- public static void Serialize < T > ( this T element , IOpenApiWriter writer , OpenApiSpecVersion specVersion )
109+ /// <param name="cancellationToken">The cancellation token.</param>
110+ public static Task SerializeAsync < T > ( this T element , IOpenApiWriter writer , OpenApiSpecVersion specVersion , CancellationToken cancellationToken = default )
102111 where T : IOpenApiSerializable
103112 {
104113 Utils . CheckArgumentNull ( element ) ;
@@ -122,7 +131,7 @@ public static void Serialize<T>(this T element, IOpenApiWriter writer, OpenApiSp
122131 throw new OpenApiException ( string . Format ( SRResource . OpenApiSpecVersionNotSupported , specVersion ) ) ;
123132 }
124133
125- writer . Flush ( ) ;
134+ return writer . FlushAsync ( cancellationToken ) ;
126135 }
127136
128137 /// <summary>
@@ -131,12 +140,14 @@ public static void Serialize<T>(this T element, IOpenApiWriter writer, OpenApiSp
131140 /// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
132141 /// <param name="element">The Open API element.</param>
133142 /// <param name="specVersion">The Open API specification version.</param>
134- public static string SerializeAsJson < T > (
143+ /// <param name="cancellationToken">The cancellation token.</param>
144+ public static Task < string > SerializeAsJsonAsync < T > (
135145 this T element ,
136- OpenApiSpecVersion specVersion )
146+ OpenApiSpecVersion specVersion ,
147+ CancellationToken cancellationToken = default )
137148 where T : IOpenApiSerializable
138149 {
139- return element . Serialize ( specVersion , OpenApiFormat . Json ) ;
150+ return element . SerializeAsync ( specVersion , OpenApiFormat . Json , cancellationToken ) ;
140151 }
141152
142153 /// <summary>
@@ -145,12 +156,14 @@ public static string SerializeAsJson<T>(
145156 /// <typeparam name="T">the <see cref="IOpenApiSerializable"/></typeparam>
146157 /// <param name="element">The Open API element.</param>
147158 /// <param name="specVersion">The Open API specification version.</param>
148- public static string SerializeAsYaml < T > (
159+ /// <param name="cancellationToken">The cancellation token.</param>
160+ public static Task < string > SerializeAsYamlAsync < T > (
149161 this T element ,
150- OpenApiSpecVersion specVersion )
162+ OpenApiSpecVersion specVersion ,
163+ CancellationToken cancellationToken = default )
151164 where T : IOpenApiSerializable
152165 {
153- return element . Serialize ( specVersion , OpenApiFormat . Yaml ) ;
166+ return element . SerializeAsync ( specVersion , OpenApiFormat . Yaml , cancellationToken ) ;
154167 }
155168
156169 /// <summary>
@@ -160,20 +173,26 @@ public static string SerializeAsYaml<T>(
160173 /// <param name="element">The Open API element.</param>
161174 /// <param name="specVersion">The Open API specification version.</param>
162175 /// <param name="format">Open API document format.</param>
163- public static string Serialize < T > (
176+ /// <param name="cancellationToken">The cancellation token.</param>
177+ public static async Task < string > SerializeAsync < T > (
164178 this T element ,
165179 OpenApiSpecVersion specVersion ,
166- OpenApiFormat format )
180+ OpenApiFormat format ,
181+ CancellationToken cancellationToken = default )
167182 where T : IOpenApiSerializable
168183 {
169184 Utils . CheckArgumentNull ( element ) ;
170185
171186 using var stream = new MemoryStream ( ) ;
172- element . Serialize ( stream , specVersion , format ) ;
187+ await element . SerializeAsync ( stream , specVersion , format , cancellationToken ) . ConfigureAwait ( false ) ;
173188 stream . Position = 0 ;
174189
175190 using var streamReader = new StreamReader ( stream ) ;
176- return streamReader . ReadToEnd ( ) ;
191+ #if NET7_0_OR_GREATER
192+ return await streamReader . ReadToEndAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
193+ #else
194+ return await streamReader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
195+ #endif
177196 }
178197 }
179198}
0 commit comments