2727using System . Xml . Xsl ;
2828using System . Xml ;
2929using System . Reflection ;
30+ using Microsoft . Extensions . Configuration ;
31+ using System . Runtime . CompilerServices ;
3032
3133namespace Microsoft . OpenApi . Hidi
3234{
@@ -44,6 +46,7 @@ public static async Task TransformOpenApiDocument(
4446 string ? version ,
4547 OpenApiFormat ? format ,
4648 bool terseOutput ,
49+ string settingsFile ,
4750 LogLevel logLevel ,
4851 bool inlineLocal ,
4952 bool inlineExternal ,
@@ -98,7 +101,8 @@ CancellationToken cancellationToken
98101 stream = ApplyFilter ( csdl , csdlFilter , transform ) ;
99102 stream . Position = 0 ;
100103 }
101- document = await ConvertCsdlToOpenApi ( stream ) ;
104+
105+ document = await ConvertCsdlToOpenApi ( stream , settingsFile ) ;
102106 stopwatch . Stop ( ) ;
103107 logger . LogTrace ( "{timestamp}ms: Generated OpenAPI with {paths} paths." , stopwatch . ElapsedMilliseconds , document . Paths . Count ) ;
104108 }
@@ -304,25 +308,37 @@ public static async Task ValidateOpenApiDocument(
304308 }
305309 }
306310
311+ internal static IConfiguration GetConfiguration ( string settingsFile )
312+ {
313+ settingsFile ??= "appsettings.json" ;
314+
315+ IConfiguration config = new ConfigurationBuilder ( )
316+ . AddJsonFile ( settingsFile , true )
317+ . Build ( ) ;
318+
319+ return config ;
320+ }
321+
307322 /// <summary>
308323 /// Converts CSDL to OpenAPI
309324 /// </summary>
310325 /// <param name="csdl">The CSDL stream.</param>
311326 /// <returns>An OpenAPI document.</returns>
312- public static async Task < OpenApiDocument > ConvertCsdlToOpenApi ( Stream csdl )
327+ public static async Task < OpenApiDocument > ConvertCsdlToOpenApi ( Stream csdl , string settingsFile = null )
313328 {
314329 using var reader = new StreamReader ( csdl ) ;
315330 var csdlText = await reader . ReadToEndAsync ( ) ;
316331 var edmModel = CsdlReader . Parse ( XElement . Parse ( csdlText ) . CreateReader ( ) ) ;
317-
332+
333+ var config = GetConfiguration ( settingsFile ) ;
318334 var settings = new OpenApiConvertSettings ( )
319335 {
320336 AddSingleQuotesForStringParameters = true ,
321337 AddEnumDescriptionExtension = true ,
322338 DeclarePathParametersOnPathItem = true ,
323339 EnableKeyAsSegment = true ,
324340 EnableOperationId = true ,
325- ErrorResponsesAsDefault = false ,
341+ ErrorResponsesAsDefault = false ,
326342 PrefixEntityTypeNameBeforeKey = true ,
327343 TagDepth = 2 ,
328344 EnablePagination = true ,
@@ -335,6 +351,8 @@ public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl)
335351 EnableCount = true ,
336352 UseSuccessStatusCodeRange = true
337353 } ;
354+ config . GetSection ( "OpenApiConvertSettings" ) . Bind ( settings ) ;
355+
338356 OpenApiDocument document = edmModel . ConvertToOpenApi ( settings ) ;
339357
340358 document = FixReferences ( document ) ;
0 commit comments