@@ -25,6 +25,7 @@ public static class OpenApiService
25
25
{
26
26
public static void ProcessOpenApiDocument (
27
27
string openapi ,
28
+ string csdl ,
28
29
FileInfo output ,
29
30
OpenApiSpecVersion ? version ,
30
31
OpenApiFormat ? format ,
@@ -34,9 +35,9 @@ public static void ProcessOpenApiDocument(
34
35
bool inline ,
35
36
bool resolveexternal )
36
37
{
37
- if ( string . IsNullOrEmpty ( openapi ) )
38
+ if ( string . IsNullOrEmpty ( openapi ) && string . IsNullOrEmpty ( csdl ) )
38
39
{
39
- throw new ArgumentNullException ( nameof ( openapi ) ) ;
40
+ throw new ArgumentNullException ( "Please input a file path" ) ;
40
41
}
41
42
if ( output == null )
42
43
{
@@ -47,21 +48,25 @@ public static void ProcessOpenApiDocument(
47
48
throw new IOException ( "The file you're writing to already exists. Please input a new output path." ) ;
48
49
}
49
50
50
- var stream = GetStream ( input ) ;
51
-
52
- ReadResult result = null ;
53
-
51
+ Stream stream ;
54
52
OpenApiDocument document ;
53
+ OpenApiFormat openApiFormat ;
55
54
56
- if ( input . Contains ( ".xml" ) || input . Contains ( ". csdl" ) )
55
+ if ( ! string . IsNullOrEmpty ( csdl ) )
57
56
{
58
- document = ConvertCsdlToOpenApi ( stream ) ;
57
+ // Default to yaml during csdl to OpenApi conversion
58
+ openApiFormat = format ?? GetOpenApiFormat ( csdl ) ;
59
+
60
+ stream = GetStream ( csdl ) ;
61
+ document = ConvertCsdlToOpenApi ( stream ) ;
59
62
}
60
63
else
61
64
{
62
- result = new OpenApiStreamReader ( new OpenApiReaderSettings
65
+ stream = GetStream ( openapi ) ;
66
+
67
+ var result = new OpenApiStreamReader ( new OpenApiReaderSettings
63
68
{
64
- ReferenceResolution = resolveExternal ? ReferenceResolutionSetting . ResolveAllReferences : ReferenceResolutionSetting . ResolveLocalReferences ,
69
+ ReferenceResolution = resolveexternal ? ReferenceResolutionSetting . ResolveAllReferences : ReferenceResolutionSetting . ResolveLocalReferences ,
65
70
RuleSet = ValidationRuleSet . GetDefaultRuleSet ( )
66
71
}
67
72
) . ReadAsync ( stream ) . GetAwaiter ( ) . GetResult ( ) ;
@@ -81,8 +86,11 @@ public static void ProcessOpenApiDocument(
81
86
82
87
throw new ArgumentException ( string . Join ( Environment . NewLine , context . Errors . Select ( e => e . Message ) . ToArray ( ) ) ) ;
83
88
}
89
+
90
+ openApiFormat = format ?? GetOpenApiFormat ( openapi ) ;
91
+ version ??= result . OpenApiDiagnostic . SpecificationVersion ;
84
92
}
85
-
93
+
86
94
Func < string , OperationType ? , OpenApiOperation , bool > predicate ;
87
95
88
96
// Check if filter options are provided, then execute
@@ -100,7 +108,6 @@ public static void ProcessOpenApiDocument(
100
108
predicate = OpenApiFilterService . CreatePredicate ( tags : filterbytags ) ;
101
109
document = OpenApiFilterService . CreateFilteredDocument ( document , predicate ) ;
102
110
}
103
-
104
111
if ( ! string . IsNullOrEmpty ( filterbycollection ) )
105
112
{
106
113
var fileStream = GetStream ( filterbycollection ) ;
@@ -118,15 +125,13 @@ public static void ProcessOpenApiDocument(
118
125
ReferenceInline = inline ? ReferenceInlineSetting . InlineLocalReferences : ReferenceInlineSetting . DoNotInlineReferences
119
126
} ;
120
127
121
- var openApiFormat = format ?? GetOpenApiFormat ( openapi ) ;
122
- var openApiVersion = version ?? result . OpenApiDiagnostic . SpecificationVersion ;
123
128
IOpenApiWriter writer = openApiFormat switch
124
129
{
125
130
OpenApiFormat . Json => new OpenApiJsonWriter ( textWriter , settings ) ,
126
131
OpenApiFormat . Yaml => new OpenApiYamlWriter ( textWriter , settings ) ,
127
132
_ => throw new ArgumentException ( "Unknown format" ) ,
128
133
} ;
129
- document . Serialize ( writer , openApiVersion ) ;
134
+ document . Serialize ( writer , ( OpenApiSpecVersion ) version ) ;
130
135
131
136
textWriter . Flush ( ) ;
132
137
}
@@ -139,7 +144,7 @@ public static void ProcessOpenApiDocument(
139
144
public static OpenApiDocument ConvertCsdlToOpenApi ( Stream csdl )
140
145
{
141
146
using var reader = new StreamReader ( csdl ) ;
142
- var csdlText = reader . ReadToEndAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
147
+ var csdlText = reader . ReadToEndAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
143
148
var edmModel = CsdlReader . Parse ( XElement . Parse ( csdlText ) . CreateReader ( ) ) ;
144
149
145
150
var settings = new OpenApiConvertSettings ( )
@@ -179,7 +184,7 @@ public static OpenApiDocument FixReferences(OpenApiDocument document)
179
184
return doc ;
180
185
}
181
186
182
- private static Stream GetStream ( string input )
187
+ private static Stream GetStream ( string openapi )
183
188
{
184
189
Stream stream ;
185
190
if ( openapi . StartsWith ( "http" ) )
0 commit comments