2222using Microsoft . OpenApi . Services ;
2323using Microsoft . OpenApi . Validations ;
2424using Microsoft . OpenApi . Writers ;
25+ using static Microsoft . OpenApi . Hidi . OpenApiSpecVersionHelper ;
2526using System . Threading ;
2627
2728namespace Microsoft . OpenApi . Hidi
@@ -33,7 +34,7 @@ public static async Task<int> ProcessOpenApiDocument(
3334 string csdl ,
3435 FileInfo output ,
3536 bool cleanoutput ,
36- OpenApiSpecVersion ? version ,
37+ string ? version ,
3738 OpenApiFormat ? format ,
3839 LogLevel loglevel ,
3940 bool inline ,
@@ -68,14 +69,15 @@ CancellationToken cancellationToken
6869 Stream stream ;
6970 OpenApiDocument document ;
7071 OpenApiFormat openApiFormat ;
72+ OpenApiSpecVersion openApiVersion ;
7173 var stopwatch = new Stopwatch ( ) ;
7274
7375 if ( ! string . IsNullOrEmpty ( csdl ) )
7476 {
7577 // Default to yaml and OpenApiVersion 3 during csdl to OpenApi conversion
7678 openApiFormat = format ?? GetOpenApiFormat ( csdl , logger ) ;
77- version ??= OpenApiSpecVersion . OpenApi3_0 ;
78-
79+ openApiVersion = version == null ? OpenApiSpecVersion . OpenApi3_0 : TryParseOpenApiSpecVersion ( version ) ;
80+
7981 stream = await GetStream ( csdl , logger , cancellationToken ) ;
8082 document = await ConvertCsdlToOpenApi ( stream ) ;
8183 }
@@ -86,12 +88,12 @@ CancellationToken cancellationToken
8688 // Parsing OpenAPI file
8789 stopwatch . Start ( ) ;
8890 logger . LogTrace ( "Parsing OpenApi file" ) ;
89- var result = new OpenApiStreamReader ( new OpenApiReaderSettings
91+ var result = await new OpenApiStreamReader ( new OpenApiReaderSettings
9092 {
9193 ReferenceResolution = resolveexternal ? ReferenceResolutionSetting . ResolveAllReferences : ReferenceResolutionSetting . ResolveLocalReferences ,
9294 RuleSet = ValidationRuleSet . GetDefaultRuleSet ( )
9395 }
94- ) . ReadAsync ( stream ) . GetAwaiter ( ) . GetResult ( ) ;
96+ ) . ReadAsync ( stream ) ;
9597
9698 document = result . OpenApiDocument ;
9799 stopwatch . Stop ( ) ;
@@ -116,7 +118,7 @@ CancellationToken cancellationToken
116118 }
117119
118120 openApiFormat = format ?? GetOpenApiFormat ( openapi , logger ) ;
119- version ??= result . OpenApiDiagnostic . SpecificationVersion ;
121+ openApiVersion = version == null ? TryParseOpenApiSpecVersion ( version ) : result . OpenApiDiagnostic . SpecificationVersion ;
120122 }
121123
122124 Func < string , OperationType ? , OpenApiOperation , bool > predicate ;
@@ -173,11 +175,10 @@ CancellationToken cancellationToken
173175 logger . LogTrace ( "Serializing to OpenApi document using the provided spec version and writer" ) ;
174176
175177 stopwatch . Start ( ) ;
176- document . Serialize ( writer , ( OpenApiSpecVersion ) version ) ;
178+ document . Serialize ( writer , openApiVersion ) ;
177179 stopwatch . Stop ( ) ;
178180
179181 logger . LogTrace ( $ "Finished serializing in { stopwatch . ElapsedMilliseconds } ms") ;
180-
181182 textWriter . Flush ( ) ;
182183
183184 return 0 ;
@@ -192,7 +193,7 @@ CancellationToken cancellationToken
192193 return 1 ;
193194 }
194195 }
195-
196+
196197 /// <summary>
197198 /// Converts CSDL to OpenAPI
198199 /// </summary>
0 commit comments