Skip to content
代码片段 群组 项目
未验证 提交 e2592015 编辑于 作者: github-actions[bot]'s avatar github-actions[bot] 提交者: GitHub
浏览文件

[release/6.0] Handle shortened JSON file in `dotnet openapi` (#36355)

* Clean backport of e23fd047

* Handle shortened JSON file in `dotnet openapi`
  - #35767
  - an existing JSON file must be truncated
* !fixup! Address nits in changed file
  - take VS suggestions
* Stop skipping `dotnet openapi` tests
  - .NET SDKs and Visual Studio's `msbuild` avoid #32686 already
* !fixup! Address nits in `dotnet openapi` test files
  - take VS suggestions
* Extend `dotnet openapi refresh` tests
  - include regression test for #35767
上级 23878536
No related branches found
No related tags found
无相关合并请求
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text.Json; using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.Evaluation; using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Openapi.Tools; using Microsoft.DotNet.Openapi.Tools;
using Microsoft.DotNet.Openapi.Tools.Internal; using Microsoft.DotNet.Openapi.Tools.Internal;
...@@ -46,9 +41,9 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -46,9 +41,9 @@ namespace Microsoft.DotNet.OpenApi.Commands
ProjectFileOption = Option("-p|--updateProject", "The project file update.", CommandOptionType.SingleValue); ProjectFileOption = Option("-p|--updateProject", "The project file update.", CommandOptionType.SingleValue);
if (Parent is Application) if (Parent is Application application)
{ {
WorkingDirectory = ((Application)Parent).WorkingDirectory; WorkingDirectory = application.WorkingDirectory;
} }
else else
{ {
...@@ -89,10 +84,11 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -89,10 +84,11 @@ namespace Microsoft.DotNet.OpenApi.Commands
private Application GetApplication() private Application GetApplication()
{ {
var parent = Parent; var parent = Parent;
while(!(parent is Application)) while(parent is not Application)
{ {
parent = parent.Parent; parent = parent.Parent;
} }
return (Application)parent; return (Application)parent;
} }
...@@ -126,7 +122,7 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -126,7 +122,7 @@ namespace Microsoft.DotNet.OpenApi.Commands
return new FileInfo(project); return new FileInfo(project);
} }
protected Project LoadProject(FileInfo projectFile) protected static Project LoadProject(FileInfo projectFile)
{ {
var project = ProjectCollection.GlobalProjectCollection.LoadProject( var project = ProjectCollection.GlobalProjectCollection.LoadProject(
projectFile.FullName, projectFile.FullName,
...@@ -136,12 +132,12 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -136,12 +132,12 @@ namespace Microsoft.DotNet.OpenApi.Commands
return project; return project;
} }
internal bool IsProjectFile(string file) internal static bool IsProjectFile(string file)
{ {
return File.Exists(Path.GetFullPath(file)) && file.EndsWith(".csproj", StringComparison.Ordinal); return File.Exists(Path.GetFullPath(file)) && file.EndsWith(".csproj", StringComparison.Ordinal);
} }
internal bool IsUrl(string file) internal static bool IsUrl(string file)
{ {
return Uri.TryCreate(file, UriKind.Absolute, out var _) && file.StartsWith("http", StringComparison.Ordinal); return Uri.TryCreate(file, UriKind.Absolute, out var _) && file.StartsWith("http", StringComparison.Ordinal);
} }
...@@ -167,7 +163,8 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -167,7 +163,8 @@ namespace Microsoft.DotNet.OpenApi.Commands
if (sourceUrl != null) if (sourceUrl != null)
{ {
if (items.Any(i => string.Equals(i.GetMetadataValue(SourceUrlAttrName), sourceUrl))) if (items.Any(
i => string.Equals(i.GetMetadataValue(SourceUrlAttrName), sourceUrl, StringComparison.Ordinal)))
{ {
Warning.Write($"A reference to '{sourceUrl}' already exists in '{project.FullPath}'."); Warning.Write($"A reference to '{sourceUrl}' already exists in '{project.FullPath}'.");
return; return;
...@@ -299,8 +296,8 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -299,8 +296,8 @@ namespace Microsoft.DotNet.OpenApi.Commands
/// <param name="retryCount"></param> /// <param name="retryCount"></param>
private static async Task<IHttpResponseMessageWrapper> RetryRequest( private static async Task<IHttpResponseMessageWrapper> RetryRequest(
Func<Task<IHttpResponseMessageWrapper>> retryBlock, Func<Task<IHttpResponseMessageWrapper>> retryBlock,
CancellationToken cancellationToken = default, int retryCount = 60,
int retryCount = 60) CancellationToken cancellationToken = default)
{ {
for (var retry = 0; retry < retryCount; retry++) for (var retry = 0; retry < retryCount; retry++)
{ {
...@@ -331,7 +328,7 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -331,7 +328,7 @@ namespace Microsoft.DotNet.OpenApi.Commands
{ {
if (exception is HttpRequestException || exception is WebException) if (exception is HttpRequestException || exception is WebException)
{ {
await Task.Delay(1 * 1000); //Wait for a while before retry. await Task.Delay(1 * 1000, cancellationToken); // Wait for a while before retry.
} }
} }
} }
...@@ -340,7 +337,7 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -340,7 +337,7 @@ namespace Microsoft.DotNet.OpenApi.Commands
throw new OperationCanceledException("Failed to connect, retry limit exceeded."); throw new OperationCanceledException("Failed to connect, retry limit exceeded.");
} }
private string GetUniqueFileName(string directory, string fileName, string extension) private static string GetUniqueFileName(string directory, string fileName, string extension)
{ {
var uniqueName = fileName; var uniqueName = fileName;
...@@ -366,7 +363,7 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -366,7 +363,7 @@ namespace Microsoft.DotNet.OpenApi.Commands
return uniqueName + extension; return uniqueName + extension;
} }
private string GetFileNameFromResponse(IHttpResponseMessageWrapper response, string url) private static string GetFileNameFromResponse(IHttpResponseMessageWrapper response, string url)
{ {
var contentDisposition = response.ContentDisposition(); var contentDisposition = response.ContentDisposition();
string result; string result;
...@@ -396,22 +393,12 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -396,22 +393,12 @@ namespace Microsoft.DotNet.OpenApi.Commands
else else
{ {
var parts = uri.Host.Split('.'); var parts = uri.Host.Split('.');
var domain = parts.Length switch
// There's no segment, use the domain name.
string domain;
switch (parts.Length)
{ {
case 1: 1 or 2 => parts.First(), // It's localhost or somewhere in an Intranet if 1; no www if 2.
case 2: 3 => parts[1], // Grab XYZ in www.XYZ.domain.com or similar.
// It's localhost if 1, no www if 2 _ => throw new NotImplementedException("We don't handle the case that the Host has more than three segments"),
domain = parts.First(); };
break;
case 3:
domain = parts[1];
break;
default:
throw new NotImplementedException("We don't handle the case that the Host has more than three segments");
}
result = domain + DefaultExtension; result = domain + DefaultExtension;
} }
...@@ -420,7 +407,7 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -420,7 +407,7 @@ namespace Microsoft.DotNet.OpenApi.Commands
return result; return result;
} }
internal CodeGenerator? GetCodeGenerator(CommandOption codeGeneratorOption) internal static CodeGenerator? GetCodeGenerator(CommandOption codeGeneratorOption)
{ {
CodeGenerator? codeGenerator; CodeGenerator? codeGenerator;
if (codeGeneratorOption.HasValue()) if (codeGeneratorOption.HasValue())
...@@ -435,7 +422,7 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -435,7 +422,7 @@ namespace Microsoft.DotNet.OpenApi.Commands
return codeGenerator; return codeGenerator;
} }
internal void ValidateCodeGenerator(CommandOption codeGeneratorOption) internal static void ValidateCodeGenerator(CommandOption codeGeneratorOption)
{ {
if (codeGeneratorOption.HasValue()) if (codeGeneratorOption.HasValue())
{ {
...@@ -494,7 +481,7 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -494,7 +481,7 @@ namespace Microsoft.DotNet.OpenApi.Commands
private static IDictionary<string, string> GetServicePackages(CodeGenerator? type) private static IDictionary<string, string> GetServicePackages(CodeGenerator? type)
{ {
CodeGenerator generator = type ?? CodeGenerator.NSwagCSharp; var generator = type ?? CodeGenerator.NSwagCSharp;
var name = Enum.GetName(typeof(CodeGenerator), generator); var name = Enum.GetName(typeof(CodeGenerator), generator);
var attributes = typeof(Program).Assembly.GetCustomAttributes<OpenApiDependencyAttribute>(); var attributes = typeof(Program).Assembly.GetCustomAttributes<OpenApiDependencyAttribute>();
...@@ -513,10 +500,8 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -513,10 +500,8 @@ namespace Microsoft.DotNet.OpenApi.Commands
private static byte[] GetHash(Stream stream) private static byte[] GetHash(Stream stream)
{ {
using (var algorithm = SHA256.Create()) using var algorithm = SHA256.Create();
{ return algorithm.ComputeHash(stream);
return algorithm.ComputeHash(stream);
}
} }
private async Task WriteToFileAsync(Stream content, string destinationPath, bool overwrite) private async Task WriteToFileAsync(Stream content, string destinationPath, bool overwrite)
...@@ -572,12 +557,13 @@ namespace Microsoft.DotNet.OpenApi.Commands ...@@ -572,12 +557,13 @@ namespace Microsoft.DotNet.OpenApi.Commands
// Create or overwrite the destination file. // Create or overwrite the destination file.
reachedCopy = true; reachedCopy = true;
using var fileStream = new FileStream(destinationPath, FileMode.OpenOrCreate, FileAccess.Write); using var fileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write);
fileStream.Seek(0, SeekOrigin.Begin); fileStream.Seek(0, SeekOrigin.Begin);
if (content.CanSeek) if (content.CanSeek)
{ {
content.Seek(0, SeekOrigin.Begin); content.Seek(0, SeekOrigin.Begin);
} }
await content.CopyToAsync(fileStream); await content.CopyToAsync(fileStream);
} }
catch (Exception ex) catch (Exception ex)
......
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml; using System.Xml;
using Microsoft.AspNetCore.Testing;
using Microsoft.DotNet.OpenApi.Tests; using Microsoft.DotNet.OpenApi.Tests;
using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
namespace Microsoft.DotNet.OpenApi.Add.Tests namespace Microsoft.DotNet.OpenApi.Add.Tests
...@@ -16,18 +12,18 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -16,18 +12,18 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
{ {
public OpenApiAddFileTests(ITestOutputHelper output) : base(output) { } public OpenApiAddFileTests(ITestOutputHelper output) : base(output) { }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public void OpenApi_Empty_ShowsHelp() public void OpenApi_Empty_ShowsHelp()
{ {
var app = GetApplication(); var app = GetApplication();
var run = app.Execute(new string[] { }); var run = app.Execute(Array.Empty<string>());
AssertNoErrors(run); AssertNoErrors(run);
Assert.Contains("Usage: openapi ", _output.ToString()); Assert.Contains("Usage: openapi ", _output.ToString());
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public void OpenApi_NoProjectExists() public void OpenApi_NoProjectExists()
{ {
var app = GetApplication(); var app = GetApplication();
...@@ -38,7 +34,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -38,7 +34,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
Assert.Equal(1, run); Assert.Equal(1, run);
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public void OpenApi_ExplicitProject_Missing() public void OpenApi_ExplicitProject_Missing()
{ {
var app = GetApplication(); var app = GetApplication();
...@@ -50,7 +46,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -50,7 +46,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
Assert.Equal(1, run); Assert.Equal(1, run);
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public void OpenApi_Add_Empty_ShowsHelp() public void OpenApi_Add_Empty_ShowsHelp()
{ {
var app = GetApplication(); var app = GetApplication();
...@@ -61,7 +57,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -61,7 +57,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
Assert.Contains("Usage: openapi add", _output.ToString()); Assert.Contains("Usage: openapi add", _output.ToString());
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public void OpenApi_Add_File_Empty_ShowsHelp() public void OpenApi_Add_File_Empty_ShowsHelp()
{ {
var app = GetApplication(); var app = GetApplication();
...@@ -72,7 +68,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -72,7 +68,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
Assert.Contains("Usage: openapi ", _output.ToString()); Assert.Contains("Usage: openapi ", _output.ToString());
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_ReuseItemGroup() public async Task OpenApi_Add_ReuseItemGroup()
{ {
var project = CreateBasicProject(withOpenApi: true); var project = CreateBasicProject(withOpenApi: true);
...@@ -101,7 +97,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -101,7 +97,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
Assert.Same(openApiRefs[0].ParentNode, openApiRefs[1].ParentNode); Assert.Same(openApiRefs[0].ParentNode, openApiRefs[1].ParentNode);
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public void OpenApi_Add_File_EquivilentPaths() public void OpenApi_Add_File_EquivilentPaths()
{ {
var project = CreateBasicProject(withOpenApi: true); var project = CreateBasicProject(withOpenApi: true);
...@@ -126,7 +122,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -126,7 +122,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
Assert.Single(openApiRefs); Assert.Single(openApiRefs);
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_NSwagTypeScript() public async Task OpenApi_Add_NSwagTypeScript()
{ {
var project = CreateBasicProject(withOpenApi: true); var project = CreateBasicProject(withOpenApi: true);
...@@ -146,7 +142,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -146,7 +142,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
Assert.Contains($"<OpenApiReference Include=\"{nswagJsonFile}\" CodeGenerator=\"NSwagTypeScript\" />", content); Assert.Contains($"<OpenApiReference Include=\"{nswagJsonFile}\" CodeGenerator=\"NSwagTypeScript\" />", content);
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_FromJson() public async Task OpenApi_Add_FromJson()
{ {
var project = CreateBasicProject(withOpenApi: true); var project = CreateBasicProject(withOpenApi: true);
...@@ -166,7 +162,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -166,7 +162,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
Assert.Contains($"<OpenApiReference Include=\"{nswagJsonFile}\"", content); Assert.Contains($"<OpenApiReference Include=\"{nswagJsonFile}\"", content);
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_File_UseProjectOption() public async Task OpenApi_Add_File_UseProjectOption()
{ {
var project = CreateBasicProject(withOpenApi: true); var project = CreateBasicProject(withOpenApi: true);
...@@ -186,7 +182,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -186,7 +182,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
Assert.Contains($"<OpenApiReference Include=\"{nswagJsonFIle}\"", content); Assert.Contains($"<OpenApiReference Include=\"{nswagJsonFIle}\"", content);
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_MultipleTimes_OnlyOneReference() public async Task OpenApi_Add_MultipleTimes_OnlyOneReference()
{ {
var project = CreateBasicProject(withOpenApi: true); var project = CreateBasicProject(withOpenApi: true);
......
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing;
using Microsoft.DotNet.OpenApi.Tests; using Microsoft.DotNet.OpenApi.Tests;
using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
namespace Microsoft.DotNet.OpenApi.Add.Tests namespace Microsoft.DotNet.OpenApi.Add.Tests
...@@ -16,7 +11,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -16,7 +11,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
{ {
public OpenApiAddURLTests(ITestOutputHelper output) : base(output){ } public OpenApiAddURLTests(ITestOutputHelper output) : base(output){ }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_Url_WithContentDisposition() public async Task OpenApi_Add_Url_WithContentDisposition()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -48,7 +43,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -48,7 +43,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenAPI_Add_Url_NoContentDisposition() public async Task OpenAPI_Add_Url_NoContentDisposition()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -81,7 +76,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -81,7 +76,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenAPI_Add_Url_NoExtension_AssumesJson() public async Task OpenAPI_Add_Url_NoExtension_AssumesJson()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -114,7 +109,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -114,7 +109,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_Url_NoSegment() public async Task OpenApi_Add_Url_NoSegment()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -147,7 +142,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -147,7 +142,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_Url() public async Task OpenApi_Add_Url()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -179,7 +174,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -179,7 +174,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_Url_SameName_UniqueFile() public async Task OpenApi_Add_Url_SameName_UniqueFile()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -239,7 +234,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -239,7 +234,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_Url_NSwagCSharp() public async Task OpenApi_Add_Url_NSwagCSharp()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -271,7 +266,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -271,7 +266,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_Url_NSwagTypeScript() public async Task OpenApi_Add_Url_NSwagTypeScript()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -303,7 +298,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -303,7 +298,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_Url_OutputFile() public async Task OpenApi_Add_Url_OutputFile()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -335,7 +330,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -335,7 +330,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Add_URL_FileAlreadyExists_Fail() public async Task OpenApi_Add_URL_FileAlreadyExists_Fail()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -393,7 +388,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -393,7 +388,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public void OpenApi_Add_URL_MultipleTimes_OnlyOneReference() public void OpenApi_Add_URL_MultipleTimes_OnlyOneReference()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
...@@ -419,7 +414,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests ...@@ -419,7 +414,7 @@ namespace Microsoft.DotNet.OpenApi.Add.Tests
Assert.Single(Regex.Matches(content, escapedApiRef)); Assert.Single(Regex.Matches(content, escapedApiRef));
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenAPi_Add_URL_InvalidUrl() public async Task OpenAPi_Add_URL_InvalidUrl()
{ {
var project = CreateBasicProject(withOpenApi: false); var project = CreateBasicProject(withOpenApi: false);
......
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.OpenApi.Tests; using Microsoft.DotNet.OpenApi.Tests;
using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
namespace Microsoft.DotNet.OpenApi.Refresh.Tests namespace Microsoft.DotNet.OpenApi.Refresh.Tests
...@@ -15,24 +10,24 @@ namespace Microsoft.DotNet.OpenApi.Refresh.Tests ...@@ -15,24 +10,24 @@ namespace Microsoft.DotNet.OpenApi.Refresh.Tests
{ {
public OpenApiRefreshTests(ITestOutputHelper output) : base(output) { } public OpenApiRefreshTests(ITestOutputHelper output) : base(output) { }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Refresh_Basic() public async Task OpenApi_Refresh_Basic()
{ {
CreateBasicProject(withOpenApi: false); CreateBasicProject(withOpenApi: false);
// Add <OpenApiReference/> to the project. Ignore initial filename.json content.
var app = GetApplication(); var app = GetApplication();
var run = app.Execute(new[] { "add", "url", FakeOpenApiUrl }); var run = app.Execute(new[] { "add", "url", FakeOpenApiUrl });
AssertNoErrors(run); AssertNoErrors(run);
// File will grow after the refresh.
var expectedJsonPath = Path.Combine(_tempDir.Root, "filename.json"); var expectedJsonPath = Path.Combine(_tempDir.Root, "filename.json");
var json = await File.ReadAllTextAsync(expectedJsonPath); await File.WriteAllTextAsync(expectedJsonPath, "trash");
json += "trash";
await File.WriteAllTextAsync(expectedJsonPath, json);
var firstWriteTime = File.GetLastWriteTime(expectedJsonPath); var firstWriteTime = File.GetLastWriteTime(expectedJsonPath);
Thread.Sleep(TimeSpan.FromSeconds(1)); await Task.Delay(TimeSpan.FromSeconds(1));
app = GetApplication(); app = GetApplication();
run = app.Execute(new[] { "refresh", FakeOpenApiUrl }); run = app.Execute(new[] { "refresh", FakeOpenApiUrl });
...@@ -41,6 +36,63 @@ namespace Microsoft.DotNet.OpenApi.Refresh.Tests ...@@ -41,6 +36,63 @@ namespace Microsoft.DotNet.OpenApi.Refresh.Tests
var secondWriteTime = File.GetLastWriteTime(expectedJsonPath); var secondWriteTime = File.GetLastWriteTime(expectedJsonPath);
Assert.True(firstWriteTime < secondWriteTime, $"File wasn't updated! {firstWriteTime} {secondWriteTime}"); Assert.True(firstWriteTime < secondWriteTime, $"File wasn't updated! {firstWriteTime} {secondWriteTime}");
Assert.Equal(Content, await File.ReadAllTextAsync(expectedJsonPath), ignoreLineEndingDifferences: true);
}
// Regression test for #35767 scenario.
[Fact]
public async Task OpenApi_Refresh_MuchShorterFile()
{
CreateBasicProject(withOpenApi: false);
// Add <OpenApiReference/> to the project. Ignore initial filename.json content.
var app = GetApplication();
var run = app.Execute(new[] { "add", "url", FakeOpenApiUrl });
AssertNoErrors(run);
// File will shrink after the refresh.
var expectedJsonPath = Path.Combine(_tempDir.Root, "filename.json");
await File.WriteAllTextAsync(expectedJsonPath, PackageUrlContent);
var firstWriteTime = File.GetLastWriteTime(expectedJsonPath);
await Task.Delay(TimeSpan.FromSeconds(1));
app = GetApplication();
run = app.Execute(new[] { "refresh", FakeOpenApiUrl });
AssertNoErrors(run);
var secondWriteTime = File.GetLastWriteTime(expectedJsonPath);
Assert.True(firstWriteTime < secondWriteTime, $"File wasn't updated! {firstWriteTime} {secondWriteTime}");
Assert.Equal(Content, await File.ReadAllTextAsync(expectedJsonPath), ignoreLineEndingDifferences: true);
}
[Fact]
public async Task OpenApi_Refresh_UnchangedFile()
{
CreateBasicProject(withOpenApi: false);
// Add <OpenApiReference/> to the project and write the filename.json file.
var app = GetApplication();
var run = app.Execute(new[] { "add", "url", FakeOpenApiUrl });
AssertNoErrors(run);
var expectedJsonPath = Path.Combine(_tempDir.Root, "filename.json");
var firstWriteTime = File.GetLastWriteTime(expectedJsonPath);
await Task.Delay(TimeSpan.FromSeconds(1));
app = GetApplication();
run = app.Execute(new[] { "refresh", FakeOpenApiUrl });
AssertNoErrors(run);
var secondWriteTime = File.GetLastWriteTime(expectedJsonPath);
Assert.Equal(firstWriteTime, secondWriteTime);
Assert.Equal(Content, await File.ReadAllTextAsync(expectedJsonPath));
} }
} }
} }
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing;
using Microsoft.DotNet.OpenApi.Tests; using Microsoft.DotNet.OpenApi.Tests;
using Microsoft.Extensions.Tools.Internal; using Microsoft.Extensions.Tools.Internal;
using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
namespace Microsoft.DotNet.OpenApi.Remove.Tests namespace Microsoft.DotNet.OpenApi.Remove.Tests
...@@ -15,7 +11,7 @@ namespace Microsoft.DotNet.OpenApi.Remove.Tests ...@@ -15,7 +11,7 @@ namespace Microsoft.DotNet.OpenApi.Remove.Tests
{ {
public OpenApiRemoveTests(ITestOutputHelper output) : base(output) { } public OpenApiRemoveTests(ITestOutputHelper output) : base(output) { }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Remove_File() public async Task OpenApi_Remove_File()
{ {
var nswagJsonFile = "openapi.json"; var nswagJsonFile = "openapi.json";
...@@ -60,7 +56,7 @@ namespace Microsoft.DotNet.OpenApi.Remove.Tests ...@@ -60,7 +56,7 @@ namespace Microsoft.DotNet.OpenApi.Remove.Tests
Assert.False(File.Exists(Path.Combine(_tempDir.Root, nswagJsonFile))); Assert.False(File.Exists(Path.Combine(_tempDir.Root, nswagJsonFile)));
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Remove_ViaUrl() public async Task OpenApi_Remove_ViaUrl()
{ {
_tempDir _tempDir
...@@ -148,7 +144,7 @@ namespace Microsoft.DotNet.OpenApi.Remove.Tests ...@@ -148,7 +144,7 @@ namespace Microsoft.DotNet.OpenApi.Remove.Tests
} }
} }
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")] [Fact]
public async Task OpenApi_Remove_Multiple() public async Task OpenApi_Remove_Multiple()
{ {
var nswagJsonFile = "openapi.json"; var nswagJsonFile = "openapi.json";
......
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text; using System.Text;
using System.Threading.Tasks;
using Microsoft.DotNet.Openapi.Tools; using Microsoft.DotNet.Openapi.Tools;
using Microsoft.Extensions.Tools.Internal; using Microsoft.Extensions.Tools.Internal;
using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
namespace Microsoft.DotNet.OpenApi.Tests namespace Microsoft.DotNet.OpenApi.Tests
...@@ -92,7 +87,7 @@ namespace Microsoft.DotNet.OpenApi.Tests ...@@ -92,7 +87,7 @@ namespace Microsoft.DotNet.OpenApi.Tests
_tempDir.Root, wrapper, _output, _error); _tempDir.Root, wrapper, _output, _error);
} }
private IDictionary<string, Tuple<string, ContentDispositionHeaderValue>> DownloadMock() private static IDictionary<string, Tuple<string, ContentDispositionHeaderValue>> DownloadMock()
{ {
var noExtension = new ContentDispositionHeaderValue("attachment"); var noExtension = new ContentDispositionHeaderValue("attachment");
noExtension.Parameters.Add(new NameValueHeaderValue("filename", "filename")); noExtension.Parameters.Add(new NameValueHeaderValue("filename", "filename"));
...@@ -113,7 +108,7 @@ namespace Microsoft.DotNet.OpenApi.Tests ...@@ -113,7 +108,7 @@ namespace Microsoft.DotNet.OpenApi.Tests
protected void AssertNoErrors(int appExitCode) protected void AssertNoErrors(int appExitCode)
{ {
Assert.True(string.IsNullOrEmpty(_error.ToString()), $"Threw error: {_error.ToString()}"); Assert.True(string.IsNullOrEmpty(_error.ToString()), $"Threw error: {_error}");
Assert.Equal(0, appExitCode); Assert.Equal(0, appExitCode);
} }
...@@ -124,7 +119,7 @@ namespace Microsoft.DotNet.OpenApi.Tests ...@@ -124,7 +119,7 @@ namespace Microsoft.DotNet.OpenApi.Tests
} }
} }
public class TestHttpClientWrapper : IHttpClientWrapper public sealed class TestHttpClientWrapper : IHttpClientWrapper
{ {
private readonly IDictionary<string, Tuple<string, ContentDispositionHeaderValue>> _results; private readonly IDictionary<string, Tuple<string, ContentDispositionHeaderValue>> _results;
...@@ -143,7 +138,7 @@ namespace Microsoft.DotNet.OpenApi.Tests ...@@ -143,7 +138,7 @@ namespace Microsoft.DotNet.OpenApi.Tests
MemoryStream stream = null; MemoryStream stream = null;
if(result != null) if(result != null)
{ {
byte[] byteArray = Encoding.ASCII.GetBytes(result.Item1); var byteArray = Encoding.ASCII.GetBytes(result.Item1);
stream = new MemoryStream(byteArray); stream = new MemoryStream(byteArray);
} }
...@@ -151,7 +146,7 @@ namespace Microsoft.DotNet.OpenApi.Tests ...@@ -151,7 +146,7 @@ namespace Microsoft.DotNet.OpenApi.Tests
} }
} }
public class TestHttpResponseMessageWrapper : IHttpResponseMessageWrapper public sealed class TestHttpResponseMessageWrapper : IHttpResponseMessageWrapper
{ {
public Task<Stream> Stream { get; } public Task<Stream> Stream { get; }
...@@ -159,17 +154,11 @@ namespace Microsoft.DotNet.OpenApi.Tests ...@@ -159,17 +154,11 @@ namespace Microsoft.DotNet.OpenApi.Tests
public bool IsSuccessCode() public bool IsSuccessCode()
{ {
switch(StatusCode) return StatusCode switch
{ {
case HttpStatusCode.OK: HttpStatusCode.OK or HttpStatusCode.Created or HttpStatusCode.NoContent or HttpStatusCode.Accepted => true,
case HttpStatusCode.Created: _ => false,
case HttpStatusCode.NoContent: };
case HttpStatusCode.Accepted:
return true;
case HttpStatusCode.NotFound:
default:
return false;
}
} }
private readonly ContentDispositionHeaderValue _contentDisposition; private readonly ContentDispositionHeaderValue _contentDisposition;
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册