@@ -106,6 +106,49 @@ public static void ProcessOpenApiDocument(
106
106
textWriter . Flush ( ) ;
107
107
}
108
108
109
+ /// <summary>
110
+ /// Converts CSDL to OpenAPI
111
+ /// </summary>
112
+ /// <param name="csdl">The CSDL stream.</param>
113
+ /// <returns>An OpenAPI document.</returns>
114
+ public static OpenApiDocument ConvertCsdlToOpenApi ( Stream csdl )
115
+ {
116
+ using var reader = new StreamReader ( csdl ) ;
117
+ var csdlText = reader . ReadToEndAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
118
+ var edmModel = CsdlReader . Parse ( XElement . Parse ( csdlText ) . CreateReader ( ) ) ;
119
+
120
+ var settings = new OpenApiConvertSettings ( )
121
+ {
122
+ EnableKeyAsSegment = true ,
123
+ EnableOperationId = true ,
124
+ PrefixEntityTypeNameBeforeKey = true ,
125
+ TagDepth = 2 ,
126
+ EnablePagination = true ,
127
+ EnableDiscriminatorValue = false ,
128
+ EnableDerivedTypesReferencesForRequestBody = false ,
129
+ EnableDerivedTypesReferencesForResponses = false ,
130
+ ShowRootPath = true ,
131
+ ShowLinks = true
132
+ } ;
133
+ OpenApiDocument document = edmModel . ConvertToOpenApi ( settings ) ;
134
+
135
+ document = FixReferences ( document ) ;
136
+
137
+ return document ;
138
+ }
139
+
140
+ public static OpenApiDocument FixReferences ( OpenApiDocument document )
141
+ {
142
+ // This method is only needed because the output of ConvertToOpenApi isn't quite a valid OpenApiDocument instance.
143
+ // So we write it out, and read it back in again to fix it up.
144
+
145
+ var sb = new StringBuilder ( ) ;
146
+ document . SerializeAsV3 ( new OpenApiYamlWriter ( new StringWriter ( sb ) ) ) ;
147
+ var doc = new OpenApiStringReader ( ) . Read ( sb . ToString ( ) , out _ ) ;
148
+
149
+ return doc ;
150
+ }
151
+
109
152
private static Stream GetStream ( string input )
110
153
{
111
154
Stream stream ;
0 commit comments