Skip to content

Commit 4da5e7a

Browse files
Merge pull request #1199 from microsoft/vnext
Merge latest changes into master
2 parents 9155d11 + d432567 commit 4da5e7a

16 files changed

+97
-31
lines changed

src/Microsoft.OpenApi.Hidi/Handlers/TransformCommandHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal class TransformCommandHandler : ICommandHandler
1919
public Option<FileInfo> OutputOption { get; set; }
2020
public Option<bool> CleanOutputOption { get; set; }
2121
public Option<string?> VersionOption { get; set; }
22+
public Option<string?> MetadataVersionOption { get; set; }
2223
public Option<OpenApiFormat?> FormatOption { get; set; }
2324
public Option<bool> TerseOutputOption { get; set; }
2425
public Option<string> SettingsFileOption { get; set; }
@@ -41,6 +42,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
4142
FileInfo output = context.ParseResult.GetValueForOption(OutputOption);
4243
bool cleanOutput = context.ParseResult.GetValueForOption(CleanOutputOption);
4344
string? version = context.ParseResult.GetValueForOption(VersionOption);
45+
string metadataVersion = context.ParseResult.GetValueForOption(MetadataVersionOption);
4446
OpenApiFormat? format = context.ParseResult.GetValueForOption(FormatOption);
4547
bool terseOutput = context.ParseResult.GetValueForOption(TerseOutputOption);
4648
string settingsFile = context.ParseResult.GetValueForOption(SettingsFileOption);
@@ -57,7 +59,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
5759
var logger = loggerFactory.CreateLogger<OpenApiService>();
5860
try
5961
{
60-
await OpenApiService.TransformOpenApiDocument(openapi, csdl, csdlFilter, output, cleanOutput, version, format, terseOutput, settingsFile, inlineLocal, inlineExternal, filterbyoperationids, filterbytags, filterbycollection, logger, cancellationToken);
62+
await OpenApiService.TransformOpenApiDocument(openapi, csdl, csdlFilter, output, cleanOutput, version, metadataVersion, format, terseOutput, settingsFile, inlineLocal, inlineExternal, filterbyoperationids, filterbytags, filterbycollection, logger, cancellationToken);
6163

6264
return 0;
6365
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageId>Microsoft.OpenApi.Hidi</PackageId>
1616
<ToolCommandName>hidi</ToolCommandName>
1717
<PackageOutputPath>./../../artifacts</PackageOutputPath>
18-
<Version>1.2.4</Version>
18+
<Version>1.2.5-preview1</Version>
1919
<Description>OpenAPI.NET CLI tool for slicing OpenAPI documents</Description>
2020
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
2121
<PackageTags>OpenAPI .NET</PackageTags>
@@ -43,7 +43,7 @@
4343
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
4444
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
4545
<PackageReference Include="Microsoft.OData.Edm" Version="7.15.0" />
46-
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.3.0-preview2" />
46+
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.3.0" />
4747
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
4848
</ItemGroup>
4949

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.IO;
8-
using System.Linq;
98
using System.Net;
109
using System.Net.Http;
1110
using System.Security;
@@ -20,7 +19,6 @@
2019
using Microsoft.OpenApi.OData;
2120
using Microsoft.OpenApi.Readers;
2221
using Microsoft.OpenApi.Services;
23-
using Microsoft.OpenApi.Validations;
2422
using Microsoft.OpenApi.Writers;
2523
using static Microsoft.OpenApi.Hidi.OpenApiSpecVersionHelper;
2624
using System.Threading;
@@ -43,6 +41,7 @@ public static async Task TransformOpenApiDocument(
4341
FileInfo output,
4442
bool cleanoutput,
4543
string? version,
44+
string metadataVersion,
4645
OpenApiFormat? format,
4746
bool terseOutput,
4847
string settingsFile,
@@ -81,7 +80,7 @@ CancellationToken cancellationToken
8180
OpenApiFormat openApiFormat = format ?? (!string.IsNullOrEmpty(openapi) ? GetOpenApiFormat(openapi, logger) : OpenApiFormat.Yaml);
8281
OpenApiSpecVersion openApiVersion = version != null ? TryParseOpenApiSpecVersion(version) : OpenApiSpecVersion.OpenApi3_0;
8382

84-
OpenApiDocument document = await GetOpenApi(openapi, csdl, csdlFilter, settingsFile, inlineExternal, logger, cancellationToken);
83+
OpenApiDocument document = await GetOpenApi(openapi, csdl, csdlFilter, settingsFile, inlineExternal, logger, cancellationToken, metadataVersion);
8584
document = await FilterOpenApiDocument(filterbyoperationids, filterbytags, filterbycollection, document, logger, cancellationToken);
8685
WriteOpenApi(output, terseOutput, inlineLocal, inlineExternal, openApiFormat, openApiVersion, document, logger);
8786
}
@@ -132,11 +131,11 @@ private static void WriteOpenApi(FileInfo output, bool terseOutput, bool inlineL
132131
}
133132

134133
// Get OpenAPI document either from OpenAPI or CSDL
135-
private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csdl, string csdlFilter, string settingsFile, bool inlineExternal, ILogger logger, CancellationToken cancellationToken)
134+
private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csdl, string csdlFilter, string settingsFile, bool inlineExternal, ILogger logger, CancellationToken cancellationToken, string metadataVersion = null)
136135
{
137136
OpenApiDocument document;
138137
Stream stream;
139-
138+
140139
if (!string.IsNullOrEmpty(csdl))
141140
{
142141
var stopwatch = new Stopwatch();
@@ -154,7 +153,7 @@ private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csd
154153
stream = null;
155154
}
156155

157-
document = await ConvertCsdlToOpenApi(filteredStream ?? stream, settingsFile, cancellationToken);
156+
document = await ConvertCsdlToOpenApi(filteredStream ?? stream, metadataVersion, settingsFile, cancellationToken);
158157
stopwatch.Stop();
159158
logger.LogTrace("{timestamp}ms: Generated OpenAPI with {paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
160159
}
@@ -317,14 +316,20 @@ internal static IConfiguration GetConfiguration(string settingsFile)
317316
/// </summary>
318317
/// <param name="csdl">The CSDL stream.</param>
319318
/// <returns>An OpenAPI document.</returns>
320-
public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, string settingsFile = null, CancellationToken token = default)
319+
public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, string metadataVersion = null, string settingsFile = null, CancellationToken token = default)
321320
{
322321
using var reader = new StreamReader(csdl);
323322
var csdlText = await reader.ReadToEndAsync(token);
324323
var edmModel = CsdlReader.Parse(XElement.Parse(csdlText).CreateReader());
325324

326325
var config = GetConfiguration(settingsFile);
327326
var settings = new OpenApiConvertSettings();
327+
328+
if (!string.IsNullOrEmpty(metadataVersion))
329+
{
330+
settings.SemVerVersion = metadataVersion;
331+
}
332+
328333
config.GetSection("OpenApiConvertSettings").Bind(settings);
329334

330335
OpenApiDocument document = edmModel.ConvertToOpenApi(settings);

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ internal static RootCommand CreateRootCommand()
4646
var versionOption = new Option<string?>("--version", "OpenAPI specification version");
4747
versionOption.AddAlias("-v");
4848

49+
var metadataVersionOption = new Option<string?>("--metadata-version", "Graph metadata version to use.");
50+
metadataVersionOption.AddAlias("--mv");
51+
4952
var formatOption = new Option<OpenApiFormat?>("--format", "File format");
5053
formatOption.AddAlias("-f");
51-
54+
5255
var terseOutputOption = new Option<bool>("--terse-output", "Produce terse json output");
5356
terseOutputOption.AddAlias("--to");
5457

@@ -93,6 +96,7 @@ internal static RootCommand CreateRootCommand()
9396
outputOption,
9497
cleanOutputOption,
9598
versionOption,
99+
metadataVersionOption,
96100
formatOption,
97101
terseOutputOption,
98102
settingsFileOption,
@@ -112,6 +116,7 @@ internal static RootCommand CreateRootCommand()
112116
OutputOption = outputOption,
113117
CleanOutputOption = cleanOutputOption,
114118
VersionOption = versionOption,
119+
MetadataVersionOption = metadataVersionOption,
115120
FormatOption = formatOption,
116121
TerseOutputOption = terseOutputOption,
117122
SettingsFileOption = settingsFileOption,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi.Readers</Title>
1212
<PackageId>Microsoft.OpenApi.Readers</PackageId>
13-
<Version>1.6.3</Version>
13+
<Version>1.6.4-preview1</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>

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 1 addition & 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.6.3</Version>
14+
<Version>1.6.4-preview1</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>

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ public OpenApiOperation() {}
116116
/// </summary>
117117
public OpenApiOperation(OpenApiOperation operation)
118118
{
119-
Tags = new List<OpenApiTag>(operation?.Tags);
119+
Tags = operation?.Tags != null ? new List<OpenApiTag>(operation?.Tags) : null;
120120
Summary = operation?.Summary ?? Summary;
121121
Description = operation?.Description ?? Description;
122122
ExternalDocs = operation?.ExternalDocs != null ? new(operation?.ExternalDocs) : null;
123123
OperationId = operation?.OperationId ?? OperationId;
124124
Parameters = operation?.Parameters != null ? new List<OpenApiParameter>(operation.Parameters) : null;
125-
RequestBody = new(operation?.RequestBody);
125+
RequestBody = operation?.RequestBody != null ? new(operation?.RequestBody) : null;
126126
Responses = operation?.Responses != null ? new(operation?.Responses) : null;
127127
Callbacks = operation?.Callbacks != null ? new Dictionary<string, OpenApiCallback>(operation.Callbacks) : null;
128128
Deprecated = operation?.Deprecated ?? Deprecated;

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer)
243243
// style
244244
if (_style.HasValue)
245245
{
246-
writer.WriteProperty(OpenApiConstants.Style, Style.Value.GetDisplayName());
246+
writer.WriteProperty(OpenApiConstants.Style, _style.Value.GetDisplayName());
247247
}
248248

249249
// explode
250-
writer.WriteProperty(OpenApiConstants.Explode, Explode, Style.HasValue && Style.Value == ParameterStyle.Form);
250+
writer.WriteProperty(OpenApiConstants.Explode, _explode, _style.HasValue && _style.Value == ParameterStyle.Form);
251251

252252
// allowReserved
253253
writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false);

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public async Task ReturnConvertedCSDLFile()
3838
}
3939

4040
[Theory]
41-
[InlineData("Todos.Todo.UpdateTodo",null, 1)]
41+
[InlineData("Todos.Todo.UpdateTodo", null, 1)]
4242
[InlineData("Todos.Todo.ListTodo", null, 1)]
43-
[InlineData(null, "Todos.Todo", 4)]
43+
[InlineData(null, "Todos.Todo", 5)]
4444
public async Task ReturnFilteredOpenApiDocBasedOnOperationIdsAndInputCsdlDocument(string operationIds, string tags, int expectedPathCount)
4545
{
4646
// Arrange
@@ -190,7 +190,7 @@ public async Task TransformCommandConvertsOpenApi()
190190
{
191191
var fileinfo = new FileInfo("sample.json");
192192
// create a dummy ILogger instance for testing
193-
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml",null, null, fileinfo, true, null, null,false,null,false,false,null,null,null,new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
193+
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml",null, null, fileinfo, true, null, null, null,false,null,false,false,null,null,null,new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
194194

195195
var output = File.ReadAllText("sample.json");
196196
Assert.NotEmpty(output);
@@ -201,7 +201,7 @@ public async Task TransformCommandConvertsOpenApi()
201201
public async Task TransformCommandConvertsOpenApiWithDefaultOutputname()
202202
{
203203
// create a dummy ILogger instance for testing
204-
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", null, null, null, true, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
204+
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", null, null, null, true, null, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
205205

206206
var output = File.ReadAllText("output.yml");
207207
Assert.NotEmpty(output);
@@ -211,7 +211,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputname()
211211
public async Task TransformCommandConvertsCsdlWithDefaultOutputname()
212212
{
213213
// create a dummy ILogger instance for testing
214-
await OpenApiService.TransformOpenApiDocument(null, "UtilityFiles\\Todo.xml", null, null, true, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
214+
await OpenApiService.TransformOpenApiDocument(null, "UtilityFiles\\Todo.xml", null, null, true, null, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
215215

216216
var output = File.ReadAllText("output.yml");
217217
Assert.NotEmpty(output);
@@ -221,7 +221,7 @@ public async Task TransformCommandConvertsCsdlWithDefaultOutputname()
221221
public async Task TransformCommandConvertsOpenApiWithDefaultOutputnameAndSwitchFormat()
222222
{
223223
// create a dummy ILogger instance for testing
224-
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", null, null, null, true, "3.0", OpenApiFormat.Yaml, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
224+
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", null, null, null, true, "3.0", null, OpenApiFormat.Yaml, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
225225

226226
var output = File.ReadAllText("output.yml");
227227
Assert.NotEmpty(output);
@@ -231,7 +231,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputnameAndSwitchF
231231
public async Task ThrowTransformCommandIfOpenApiAndCsdlAreEmpty()
232232
{
233233
await Assert.ThrowsAsync<ArgumentException>(async () =>
234-
await OpenApiService.TransformOpenApiDocument(null, null, null, null, true, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken()));
234+
await OpenApiService.TransformOpenApiDocument(null, null, null, null, true, null, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken()));
235235

236236
}
237237

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
268268
<PackageReference Include="FluentAssertions" Version="6.10.0">
269269
</PackageReference>
270-
<PackageReference Include="Newtonsoft.Json" Version="13.0.2">
270+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3">
271271
</PackageReference>
272272
<PackageReference Include="SharpYaml" Version="2.1.0">
273273
</PackageReference>

0 commit comments

Comments
 (0)