Skip to content

Commit f23b0a9

Browse files
committed
Merge remote-tracking branch 'origin/vnext' into mk/feature/convert-csdl-to-openapi
2 parents 07169a4 + cd45ea1 commit f23b0a9

16 files changed

+24118
-63
lines changed

src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<PackAsTool>true</PackAsTool>
77
<ToolCommandName>hidi</ToolCommandName>
88
<PackageOutputPath>./../../artifacts</PackageOutputPath>
9-
<Version>0.5.0-preview</Version>
9+
<Version>0.5.0-preview2</Version>
1010
</PropertyGroup>
1111

1212
<ItemGroup>

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
78
using System.Net;
89
using System.Net.Http;
910
using System.Text;
11+
using System.Text.Json;
1012
using System.Threading.Tasks;
1113
using System.Xml.Linq;
1214
using Microsoft.OData.Edm.Csdl;
@@ -20,15 +22,16 @@
2022

2123
namespace Microsoft.OpenApi.Hidi
2224
{
23-
static class OpenApiService
25+
public static class OpenApiService
2426
{
2527
public static void ProcessOpenApiDocument(
2628
string input,
2729
FileInfo output,
28-
OpenApiSpecVersion version,
29-
OpenApiFormat format,
30+
OpenApiSpecVersion? version,
31+
OpenApiFormat? format,
3032
string filterByOperationIds,
3133
string filterByTags,
34+
string filterByCollection,
3235
bool inline,
3336
bool resolveExternal)
3437
{
@@ -61,21 +64,29 @@ public static void ProcessOpenApiDocument(
6164
).ReadAsync(stream).GetAwaiter().GetResult();
6265

6366
document = result.OpenApiDocument;
64-
67+
Func<string, OperationType?, OpenApiOperation, bool> predicate;
68+
6569
// Check if filter options are provided, then execute
6670
if (!string.IsNullOrEmpty(filterByOperationIds) && !string.IsNullOrEmpty(filterByTags))
6771
{
6872
throw new InvalidOperationException("Cannot filter by operationIds and tags at the same time.");
6973
}
70-
7174
if (!string.IsNullOrEmpty(filterByOperationIds))
7275
{
73-
var predicate = OpenApiFilterService.CreatePredicate(operationIds: filterByOperationIds);
76+
predicate = OpenApiFilterService.CreatePredicate(operationIds: filterByOperationIds);
7477
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
7578
}
7679
if (!string.IsNullOrEmpty(filterByTags))
7780
{
78-
var predicate = OpenApiFilterService.CreatePredicate(tags: filterByTags);
81+
predicate = OpenApiFilterService.CreatePredicate(tags: filterByTags);
82+
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
83+
}
84+
85+
if (!string.IsNullOrEmpty(filterByCollection))
86+
{
87+
var fileStream = GetStream(filterByCollection);
88+
var requestUrls = ParseJsonCollectionFile(fileStream);
89+
predicate = OpenApiFilterService.CreatePredicate(requestUrls: requestUrls, source:document);
7990
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
8091
}
8192

@@ -101,13 +112,16 @@ public static void ProcessOpenApiDocument(
101112
{
102113
ReferenceInline = inline ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
103114
};
104-
IOpenApiWriter writer = format switch
115+
116+
var openApiFormat = format ?? GetOpenApiFormat(input);
117+
var openApiVersion = version ?? result.OpenApiDiagnostic.SpecificationVersion;
118+
IOpenApiWriter writer = openApiFormat switch
105119
{
106120
OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings),
107121
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
108122
_ => throw new ArgumentException("Unknown format"),
109123
};
110-
document.Serialize(writer, version);
124+
document.Serialize(writer, openApiVersion);
111125

112126
textWriter.Flush();
113127
}
@@ -178,6 +192,38 @@ private static Stream GetStream(string input)
178192
return stream;
179193
}
180194

195+
/// <summary>
196+
/// Takes in a file stream, parses the stream into a JsonDocument and gets a list of paths and Http methods
197+
/// </summary>
198+
/// <param name="stream"> A file stream.</param>
199+
/// <returns> A dictionary of request urls and http methods from a collection.</returns>
200+
public static Dictionary<string, List<string>> ParseJsonCollectionFile(Stream stream)
201+
{
202+
var requestUrls = new Dictionary<string, List<string>>();
203+
204+
// Convert file to JsonDocument
205+
using var document = JsonDocument.Parse(stream);
206+
var root = document.RootElement;
207+
var itemElement = root.GetProperty("item");
208+
foreach (var requestObject in itemElement.EnumerateArray().Select(item => item.GetProperty("request")))
209+
{
210+
// Fetch list of methods and urls from collection, store them in a dictionary
211+
var path = requestObject.GetProperty("url").GetProperty("raw").ToString();
212+
var method = requestObject.GetProperty("method").ToString();
213+
214+
if (!requestUrls.ContainsKey(path))
215+
{
216+
requestUrls.Add(path, new List<string> { method });
217+
}
218+
else
219+
{
220+
requestUrls[path].Add(method);
221+
}
222+
}
223+
224+
return requestUrls;
225+
}
226+
181227
internal static void ValidateOpenApiDocument(string input)
182228
{
183229
if (input == null)
@@ -209,5 +255,10 @@ internal static void ValidateOpenApiDocument(string input)
209255

210256
Console.WriteLine(statsVisitor.GetStatisticsReport());
211257
}
258+
259+
private static OpenApiFormat GetOpenApiFormat(string input)
260+
{
261+
return !input.StartsWith("http") && Path.GetExtension(input) == ".json" ? OpenApiFormat.Json : OpenApiFormat.Yaml;
262+
}
212263
}
213264
}

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ static async Task<int> Main(string[] args)
3030
new Option("--inline", "Inline $ref instances", typeof(bool) ),
3131
new Option("--resolveExternal","Resolve external $refs", typeof(bool)),
3232
new Option("--filterByOperationIds", "Filters OpenApiDocument by OperationId(s) provided", typeof(string)),
33-
new Option("--filterByTags", "Filters OpenApiDocument by Tag(s) provided", typeof(string))
33+
new Option("--filterByTags", "Filters OpenApiDocument by Tag(s) provided", typeof(string)),
34+
new Option("--filterByCollection", "Filters OpenApiDocument by Postman collection provided", typeof(string))
3435
};
35-
transformCommand.Handler = CommandHandler.Create<string, FileInfo, OpenApiSpecVersion, OpenApiFormat, string, string, bool, bool>(
36+
transformCommand.Handler = CommandHandler.Create<string, FileInfo, OpenApiSpecVersion?, OpenApiFormat?, string, string, string, bool, bool>(
3637
OpenApiService.ProcessOpenApiDocument);
3738

3839
rootCommand.Add(transformCommand);

src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi.Readers</Title>
1212
<PackageId>Microsoft.OpenApi.Readers</PackageId>
13-
<Version>1.3.0-preview</Version>
13+
<Version>1.3.1-preview2</Version>
1414
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
1515
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1616
<PackageTags>OpenAPI .NET</PackageTags>
1717
<RepositoryUrl>https://github.com/Microsoft/OpenAPI.NET</RepositoryUrl>
1818
<PackageReleaseNotes>
1919
- Publish symbols.
20-
</PackageReleaseNotes>
20+
</PackageReleaseNotes>
2121
<AssemblyName>Microsoft.OpenApi.Readers</AssemblyName>
2222
<RootNamespace>Microsoft.OpenApi.Readers</RootNamespace>
2323
<SignAssembly>true</SignAssembly>

src/Microsoft.OpenApi.Readers/V3/OpenApiEncodingDeserializer.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,7 @@ internal static partial class OpenApiV3Deserializer
3131
{
3232
"style", (o, n) =>
3333
{
34-
ParameterStyle style;
35-
if (Enum.TryParse(n.GetScalarValue(), out style))
36-
{
37-
o.Style = style;
38-
}
39-
else
40-
{
41-
o.Style = null;
42-
}
34+
o.Style = n.GetScalarValue().GetEnumFromDisplayName<ParameterStyle>();
4335
}
4436
},
4537
{

src/Microsoft.OpenApi.Readers/V3/OpenApiMediaTypeDeserializer.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ public static OpenApiMediaType LoadMediaType(ParseNode node)
8181
{
8282
var mapNode = node.CheckMapNode(OpenApiConstants.Content);
8383

84-
if (!mapNode.Any())
85-
{
86-
return null;
87-
}
88-
8984
var mediaType = new OpenApiMediaType();
9085

9186
ParseMap(mapNode, mediaType, _mediaTypeFixedFields, _mediaTypePatternFields);

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<Company>Microsoft</Company>
1212
<Title>Microsoft.OpenApi</Title>
1313
<PackageId>Microsoft.OpenApi</PackageId>
14-
<Version>1.3.0-preview</Version>
14+
<Version>1.3.1-preview2</Version>
1515
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
1616
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1717
<PackageTags>OpenAPI .NET</PackageTags>
@@ -38,6 +38,7 @@
3838

3939
<ItemGroup>
4040
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
41+
<PackageReference Include="System.Text.Json" Version="6.0.1" />
4142
</ItemGroup>
4243

4344
<ItemGroup>

0 commit comments

Comments
 (0)