Skip to content

Handle shortened JSON file in dotnet openapi #36171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 10, 2021
Merged

Conversation

dougbu
Copy link
Contributor

@dougbu dougbu commented Sep 4, 2021

- #35767
- an existing JSON file must be truncated
@dougbu dougbu added area-commandlinetools Includes: Command line tools, dotnet-dev-certs, dotnet-user-jwts, and OpenAPI feature-openapi labels Sep 4, 2021
@dougbu dougbu requested a review from mkArtakMSFT September 4, 2021 01:04
@dougbu dougbu requested a review from Pilchie as a code owner September 4, 2021 01:04
@dougbu
Copy link
Contributor Author

dougbu commented Sep 4, 2021

@ryanbrandenburg are you available to check this out❔

@mkArtakMSFT please add a reviewer familiar with FileMode differences

@dougbu
Copy link
Contributor Author

dougbu commented Sep 4, 2021

@mkArtakMSFT @bradygaster should we backport this to any of our servicing branches or release/6.0❔ If yes, which❔

@dougbu
Copy link
Contributor Author

dougbu commented Sep 4, 2021

PR needs a test for the #35767 scenario

- .NET SDKs and Visual Studio's `msbuild` avoid #32686
- include regression test for #35767
CancellationToken cancellationToken = default,
int retryCount = 60)
int retryCount = 60,
CancellationToken cancellationToken = default)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit odd. The method is only ever called w/ a single argument.

@@ -572,12 +557,13 @@ private async Task WriteToFileAsync(Stream content, string destinationPath, bool

// Create or overwrite the destination file.
reachedCopy = true;
using var fileStream = new FileStream(destinationPath, FileMode.OpenOrCreate, FileAccess.Write);
using var fileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most important parts of the PR are this and changes in src/Tools/Microsoft.dotnet-openapi/test/OpenApiRefreshTests.cs

@@ -16,18 +12,18 @@ public class OpenApiAddFileTests : OpenApiTestBase
{
public OpenApiAddFileTests(ITestOutputHelper output) : base(output) { }

[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/32686")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will close this issue if tests run smoothly on CI. They're fine in VS locally 😀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good news…

@dougbu
Copy link
Contributor Author

dougbu commented Sep 9, 2021

@mkArtakMSFT could you review or assign a reviewer please❔

@mkArtakMSFT @bradygaster should we backport this to any of our servicing branches or release/6.0❔ If yes, which❔

@dougbu
Copy link
Contributor Author

dougbu commented Sep 9, 2021

/fyi @captainsafia this PR definitely and successfully cleans up the last of #32686 😀

Comment on lines -162 to +161
switch(StatusCode)
return StatusCode switch
{
case HttpStatusCode.OK:
case HttpStatusCode.Created:
case HttpStatusCode.NoContent:
case HttpStatusCode.Accepted:
return true;
case HttpStatusCode.NotFound:
default:
return false;
}
HttpStatusCode.OK or HttpStatusCode.Created or HttpStatusCode.NoContent or HttpStatusCode.Accepted => true,
_ => false,
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I rarely remember how to write a switch expression, they're certainly easy to read 😺

@bradygaster
Copy link
Member

@mkArtakMSFT @bradygaster should we backport this to any of our servicing branches or release/6.0❔ If yes, which❔

How far back would be far-enough back to start causing you headaches? I'd definitely support going back to support 3.1, but 5.0, not so sure if there'd be a strong-enough case.

var json = await File.ReadAllTextAsync(expectedJsonPath);
json += "trash";
await File.WriteAllTextAsync(expectedJsonPath, json);
await File.WriteAllTextAsync(expectedJsonPath, "trash");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean it is safe to override the contents of expectedJsonPath instead of appending?

Copy link
Contributor Author

@dougbu dougbu Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is only read to check its SHA in this test. Put another way, the intent is to demonstrate downloading a longer JSON file than the original. New tests demonstrate identical and shorter downloads.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The dotnet openapi tool does not itself parse OpenAPI files. That comes later, when the changed project is built.)

@dougbu
Copy link
Contributor Author

dougbu commented Sep 9, 2021

@mkArtakMSFT @bradygaster should we backport this to any of our servicing branches or release/6.0❔ If yes, which❔

How far back would be far-enough back to start causing you headaches? I'd definitely support going back to support 3.1, but 5.0, not so sure if there'd be a strong-enough case.

The main differences between branches appear to be slight improvements in syntax and test code. I suspect the only trip-ups will be forced returns to older C# syntax. Note: I would cherry-pick the final command rather than blindly take whatever has changed in 'main'.

I also recommend we take this to release/5.0 and release/6.0 if we decide to update release/3.1.

@dougbu dougbu requested review from rafikiassumani-msft and removed request for mkArtakMSFT September 9, 2021 18:21
@dougbu
Copy link
Contributor Author

dougbu commented Sep 9, 2021

Adding @rafikiassumanimsft and removing @mkArtakMSFT given ownership

default:
return false;
}
HttpStatusCode.OK or HttpStatusCode.Created or HttpStatusCode.NoContent or HttpStatusCode.Accepted => true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You get 100 @pranavkm points

@dougbu dougbu merged commit e23fd04 into main Sep 10, 2021
@dougbu dougbu deleted the dougbu/json.the.shorter.35767 branch September 10, 2021 00:11
@ghost ghost added this to the 7.0-preview1 milestone Sep 10, 2021
@dougbu
Copy link
Contributor Author

dougbu commented Sep 10, 2021

Thanks all❕

@dougbu
Copy link
Contributor Author

dougbu commented Sep 10, 2021

/backport to release/6.0

@github-actions
Copy link
Contributor

Started backporting to release/6.0: https://github.com/dotnet/aspnetcore/actions/runs/1219400869

dougbu added a commit that referenced this pull request Sep 13, 2021
* backport of  #36171
* handle shortened JSON file in `dotnet openapi`
  - #35767
  - an existing JSON file must be truncated
* Extend `dotnet openapi refresh` tests
  - include regression test for #35767
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-commandlinetools Includes: Command line tools, dotnet-dev-certs, dotnet-user-jwts, and OpenAPI feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants