25
25
using System . Xml . Xsl ;
26
26
using System . Xml ;
27
27
using System . Reflection ;
28
+ using Microsoft . OpenApi . Hidi . Options ;
28
29
using Microsoft . Extensions . Configuration ;
30
+ using Microsoft . OpenApi . Hidi . Utilities ;
29
31
using Microsoft . OpenApi . Hidi . Formatters ;
30
32
31
33
namespace Microsoft . OpenApi . Hidi
32
34
{
33
- public class OpenApiService
35
+ internal class OpenApiService
34
36
{
35
37
/// <summary>
36
38
/// Implementation of the transform command
37
39
/// </summary>
38
- public static async Task TransformOpenApiDocument (
39
- string openapi ,
40
- string csdl ,
41
- string csdlFilter ,
42
- FileInfo output ,
43
- bool cleanoutput ,
44
- string ? version ,
45
- string metadataVersion ,
46
- OpenApiFormat ? format ,
47
- bool terseOutput ,
48
- string settingsFile ,
49
- bool inlineLocal ,
50
- bool inlineExternal ,
51
- string ? languageFormatOption ,
52
- string filterbyoperationids ,
53
- string filterbytags ,
54
- string filterbycollection ,
55
- ILogger logger ,
56
- CancellationToken cancellationToken
57
- )
40
+ public static async Task TransformOpenApiDocument ( HidiOptions options , ILogger logger , CancellationToken cancellationToken )
58
41
{
59
- if ( string . IsNullOrEmpty ( openapi ) && string . IsNullOrEmpty ( csdl ) )
42
+ if ( string . IsNullOrEmpty ( options . OpenApi ) && string . IsNullOrEmpty ( options . Csdl ) )
60
43
{
61
44
throw new ArgumentException ( "Please input a file path or URL" ) ;
62
45
}
63
46
64
47
try
65
48
{
66
- if ( output == null )
49
+ if ( options . Output == null )
67
50
{
68
- var inputExtension = GetInputPathExtension ( openapi , csdl ) ;
69
- output = new FileInfo ( $ "./output{ inputExtension } ") ;
51
+ var inputExtension = GetInputPathExtension ( options . OpenApi , options . Csdl ) ;
52
+ options . Output = new FileInfo ( $ "./output{ inputExtension } ") ;
70
53
} ;
71
54
72
- if ( cleanoutput && output . Exists )
55
+ if ( options . CleanOutput && options . Output . Exists )
73
56
{
74
- output . Delete ( ) ;
57
+ options . Output . Delete ( ) ;
75
58
}
76
- if ( output . Exists )
59
+ if ( options . Output . Exists )
77
60
{
78
- throw new IOException ( $ "The file { output } already exists. Please input a new file path.") ;
61
+ throw new IOException ( $ "The file { options . Output } already exists. Please input a new file path.") ;
79
62
}
80
63
81
64
// Default to yaml and OpenApiVersion 3 during csdl to OpenApi conversion
82
- OpenApiFormat openApiFormat = format ?? ( ! string . IsNullOrEmpty ( openapi ) ? GetOpenApiFormat ( openapi , logger ) : OpenApiFormat . Yaml ) ;
83
- OpenApiSpecVersion openApiVersion = version != null ? TryParseOpenApiSpecVersion ( version ) : OpenApiSpecVersion . OpenApi3_0 ;
84
-
85
- OpenApiDocument document = await GetOpenApi ( openapi , csdl , csdlFilter , settingsFile , inlineExternal , logger , cancellationToken , metadataVersion ) ;
86
- document = await FilterOpenApiDocument ( filterbyoperationids , filterbytags , filterbycollection , document , logger , cancellationToken ) ;
87
- if ( ! string . IsNullOrWhiteSpace ( languageFormatOption ) && languageFormatOption . Equals ( "PowerShell" , StringComparison . InvariantCultureIgnoreCase ) )
65
+ OpenApiFormat openApiFormat = options . OpenApiFormat ?? ( ! string . IsNullOrEmpty ( options . OpenApi ) ? GetOpenApiFormat ( options . OpenApi , logger ) : OpenApiFormat . Yaml ) ;
66
+ OpenApiSpecVersion openApiVersion = options . Version != null ? TryParseOpenApiSpecVersion ( options . Version ) : OpenApiSpecVersion . OpenApi3_0 ;
67
+
68
+ OpenApiDocument document = await GetOpenApi ( options . OpenApi , options . Csdl , options . CsdlFilter , options . SettingsConfig , options . InlineExternal , logger , cancellationToken , options . MetadataVersion ) ;
69
+ if ( options . FilterOptions != null )
70
+ document = await FilterOpenApiDocument ( options . FilterOptions . FilterByOperationIds , options . FilterOptions . FilterByTags , options . FilterOptions . FilterByCollection , document , logger , cancellationToken ) ;
71
+ // TODO: Handle PS formating
72
+ var languageFormat = options . SettingsConfig . GetSection ( "LanguageFormat" ) . Value ;
73
+ if ( ! string . IsNullOrWhiteSpace ( languageFormat ) && languageFormat . Equals ( "PowerShell" , StringComparison . InvariantCultureIgnoreCase ) )
88
74
{
89
75
// PowerShell Walker.
90
76
var powerShellFormatter = new PowerShellFormatter ( ) ;
91
77
var walker = new OpenApiWalker ( powerShellFormatter ) ;
92
78
walker . Walk ( document ) ;
93
79
}
94
- WriteOpenApi ( output , terseOutput , inlineLocal , inlineExternal , openApiFormat , openApiVersion , document , logger ) ;
80
+ WriteOpenApi ( options . Output , options . TerseOutput , options . InlineLocal , options . InlineExternal , openApiFormat , openApiVersion , document , logger ) ;
95
81
}
96
82
catch ( TaskCanceledException )
97
83
{
@@ -140,7 +126,7 @@ private static void WriteOpenApi(FileInfo output, bool terseOutput, bool inlineL
140
126
}
141
127
142
128
// Get OpenAPI document either from OpenAPI or CSDL
143
- private static async Task < OpenApiDocument > GetOpenApi ( string openapi , string csdl , string csdlFilter , string settingsFile , bool inlineExternal , ILogger logger , CancellationToken cancellationToken , string metadataVersion = null )
129
+ private static async Task < OpenApiDocument > GetOpenApi ( string openapi , string csdl , string csdlFilter , IConfiguration settings , bool inlineExternal , ILogger logger , CancellationToken cancellationToken , string metadataVersion = null )
144
130
{
145
131
OpenApiDocument document ;
146
132
Stream stream ;
@@ -162,7 +148,7 @@ private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csd
162
148
stream = null ;
163
149
}
164
150
165
- document = await ConvertCsdlToOpenApi ( filteredStream ?? stream , metadataVersion , settingsFile , cancellationToken ) ;
151
+ document = await ConvertCsdlToOpenApi ( filteredStream ?? stream , metadataVersion , settings , cancellationToken ) ;
166
152
stopwatch . Stop ( ) ;
167
153
logger . LogTrace ( "{timestamp}ms: Generated OpenAPI with {paths} paths." , stopwatch . ElapsedMilliseconds , document . Paths . Count ) ;
168
154
}
@@ -309,39 +295,19 @@ private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inli
309
295
return result ;
310
296
}
311
297
312
- internal static IConfiguration GetConfiguration ( string settingsFile )
313
- {
314
- settingsFile ??= "appsettings.json" ;
315
-
316
- IConfiguration config = new ConfigurationBuilder ( )
317
- . AddJsonFile ( settingsFile , true )
318
- . Build ( ) ;
319
-
320
- return config ;
321
- }
322
-
323
298
/// <summary>
324
299
/// Converts CSDL to OpenAPI
325
300
/// </summary>
326
301
/// <param name="csdl">The CSDL stream.</param>
327
302
/// <returns>An OpenAPI document.</returns>
328
- public static async Task < OpenApiDocument > ConvertCsdlToOpenApi ( Stream csdl , string metadataVersion = null , string settingsFile = null , CancellationToken token = default )
303
+ public static async Task < OpenApiDocument > ConvertCsdlToOpenApi ( Stream csdl , string metadataVersion = null , IConfiguration settings = null , CancellationToken token = default )
329
304
{
330
305
using var reader = new StreamReader ( csdl ) ;
331
306
var csdlText = await reader . ReadToEndAsync ( token ) ;
332
307
var edmModel = CsdlReader . Parse ( XElement . Parse ( csdlText ) . CreateReader ( ) ) ;
308
+ settings ??= SettingsUtilities . GetConfiguration ( ) ;
333
309
334
- var config = GetConfiguration ( settingsFile ) ;
335
- var settings = new OpenApiConvertSettings ( ) ;
336
-
337
- if ( ! string . IsNullOrEmpty ( metadataVersion ) )
338
- {
339
- settings . SemVerVersion = metadataVersion ;
340
- }
341
-
342
- config . GetSection ( "OpenApiConvertSettings" ) . Bind ( settings ) ;
343
-
344
- OpenApiDocument document = edmModel . ConvertToOpenApi ( settings ) ;
310
+ OpenApiDocument document = edmModel . ConvertToOpenApi ( SettingsUtilities . GetOpenApiConvertSettings ( settings , metadataVersion ) ) ;
345
311
document = FixReferences ( document ) ;
346
312
347
313
return document ;
0 commit comments