Skip to content

Commit 1b40298

Browse files
committed
Refactor code
1 parent 66c06b6 commit 1b40298

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static class OpenApiService
2525
{
2626
public static void ProcessOpenApiDocument(
2727
string openapi,
28+
string csdl,
2829
FileInfo output,
2930
OpenApiSpecVersion? version,
3031
OpenApiFormat? format,
@@ -34,9 +35,9 @@ public static void ProcessOpenApiDocument(
3435
bool inline,
3536
bool resolveexternal)
3637
{
37-
if (string.IsNullOrEmpty(openapi))
38+
if (string.IsNullOrEmpty(openapi) && string.IsNullOrEmpty(csdl))
3839
{
39-
throw new ArgumentNullException(nameof(openapi));
40+
throw new ArgumentNullException("Please input a file path");
4041
}
4142
if(output == null)
4243
{
@@ -47,21 +48,25 @@ public static void ProcessOpenApiDocument(
4748
throw new IOException("The file you're writing to already exists. Please input a new output path.");
4849
}
4950

50-
var stream = GetStream(input);
51-
52-
ReadResult result = null;
53-
51+
Stream stream;
5452
OpenApiDocument document;
53+
OpenApiFormat openApiFormat;
5554

56-
if (input.Contains(".xml") || input.Contains(".csdl"))
55+
if (!string.IsNullOrEmpty(csdl))
5756
{
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);
5962
}
6063
else
6164
{
62-
result = new OpenApiStreamReader(new OpenApiReaderSettings
65+
stream = GetStream(openapi);
66+
67+
var result = new OpenApiStreamReader(new OpenApiReaderSettings
6368
{
64-
ReferenceResolution = resolveExternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
69+
ReferenceResolution = resolveexternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
6570
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
6671
}
6772
).ReadAsync(stream).GetAwaiter().GetResult();
@@ -81,8 +86,11 @@ public static void ProcessOpenApiDocument(
8186

8287
throw new ArgumentException(string.Join(Environment.NewLine, context.Errors.Select(e => e.Message).ToArray()));
8388
}
89+
90+
openApiFormat = format ?? GetOpenApiFormat(openapi);
91+
version ??= result.OpenApiDiagnostic.SpecificationVersion;
8492
}
85-
93+
8694
Func<string, OperationType?, OpenApiOperation, bool> predicate;
8795

8896
// Check if filter options are provided, then execute
@@ -100,7 +108,6 @@ public static void ProcessOpenApiDocument(
100108
predicate = OpenApiFilterService.CreatePredicate(tags: filterbytags);
101109
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
102110
}
103-
104111
if (!string.IsNullOrEmpty(filterbycollection))
105112
{
106113
var fileStream = GetStream(filterbycollection);
@@ -118,15 +125,13 @@ public static void ProcessOpenApiDocument(
118125
ReferenceInline = inline ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
119126
};
120127

121-
var openApiFormat = format ?? GetOpenApiFormat(openapi);
122-
var openApiVersion = version ?? result.OpenApiDiagnostic.SpecificationVersion;
123128
IOpenApiWriter writer = openApiFormat switch
124129
{
125130
OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings),
126131
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
127132
_ => throw new ArgumentException("Unknown format"),
128133
};
129-
document.Serialize(writer, openApiVersion);
134+
document.Serialize(writer, (OpenApiSpecVersion)version);
130135

131136
textWriter.Flush();
132137
}
@@ -139,7 +144,7 @@ public static void ProcessOpenApiDocument(
139144
public static OpenApiDocument ConvertCsdlToOpenApi(Stream csdl)
140145
{
141146
using var reader = new StreamReader(csdl);
142-
var csdlText = reader.ReadToEndAsync().GetAwaiter().GetResult();
147+
var csdlText = reader.ReadToEndAsync().GetAwaiter().GetResult();
143148
var edmModel = CsdlReader.Parse(XElement.Parse(csdlText).CreateReader());
144149

145150
var settings = new OpenApiConvertSettings()
@@ -179,7 +184,7 @@ public static OpenApiDocument FixReferences(OpenApiDocument document)
179184
return doc;
180185
}
181186

182-
private static Stream GetStream(string input)
187+
private static Stream GetStream(string openapi)
183188
{
184189
Stream stream;
185190
if (openapi.StartsWith("http"))

0 commit comments

Comments
 (0)