Skip to content

Refactor readers to reduce surface area - #1975

Merged
Vincent Biret (baywet) merged 55 commits into
devfrom
mk/fix-json-reader
Dec 20, 2024
Merged

Refactor readers to reduce surface area#1975
Vincent Biret (baywet) merged 55 commits into
devfrom
mk/fix-json-reader

Conversation

@MaggieKimani1

@MaggieKimani1Maggie Kimani (MaggieKimani1) commented Nov 28, 2024

Copy link
Copy Markdown
Contributor

Fixes#1954
fixes#1951
Fixes#1964
Fixes#1917
fixes#1918
closes#1958
closes#1929

Comment threadsrc/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs Fixed
Comment threadsrc/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs Fixed
Comment threadsrc/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs Fixed
Comment threadsrc/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs Fixed
@MaggieKimani1
Maggie Kimani (MaggieKimani1) marked this pull request as ready for review December 2, 2024 11:19
{
throw new InvalidOperationException($"Could not download the file at {url}", ex);
// YAML or other non-JSON format; copy remaining input to a new stream.
preparedStream = new MemoryStream();

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable

Disposable 'MemoryStream' is created but not disposed.
{
throw new InvalidOperationException($"Could not open the file at {url}", ex);
// Buffer stream for non-JSON formats (e.g., YAML) since they require synchronous reading
preparedStream = new MemoryStream();

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable

Disposable 'MemoryStream' is created but not disposed.
Comment threadsrc/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs Outdated
Comment threadsrc/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs Outdated

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot reviewed 15 out of 30 changed files in this pull request and generated 2 comments.

Files not reviewed (15)
  • test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs: Evaluated as low risk
  • src/Microsoft.OpenApi.Hidi/OpenApiService.cs: Evaluated as low risk
  • src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs: Evaluated as low risk
  • src/Microsoft.OpenApi/Models/OpenApiDocument.cs: Evaluated as low risk
  • test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs: Evaluated as low risk
  • test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs: Evaluated as low risk
  • src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs: Evaluated as low risk
  • test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs: Evaluated as low risk
  • test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs: Evaluated as low risk
  • test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs: Evaluated as low risk
  • test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs: Evaluated as low risk
  • test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs: Evaluated as low risk
  • test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs: Evaluated as low risk
  • test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs: Evaluated as low risk
  • test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/UnsupportedSpecVersionTests.cs: Evaluated as low risk
Comments suppressed due to low confidence (3)

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs:332

  • The method PrepareStreamForReadingAsync might read the entire stream into memory, which could be inefficient for very large streams. Consider adding a check to handle large streams more efficiently.
private static async Task<(Stream, string)> PrepareStreamForReadingAsync(Stream input, string format, CancellationToken token = default)

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs:79

  • The method ReadAsync(JsonNode, OpenApiReaderSettings, string, CancellationToken) has been removed. Replace it with Read(JsonNode, OpenApiReaderSettings, string).
return await ReadAsync(jsonNode, settings, cancellationToken: cancellationToken);

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs:145

  • Ensure that the external references loading functionality is still covered elsewhere in the codebase, as the LoadExternalRefsAsync method has been removed.
return Read(jsonNode, settings);

Comment threadsrc/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs
Comment threadsrc/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate FailedQuality Gate failed

Failed conditions
78.3% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@baywetVincent Biret (baywet) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for making the changes!

@baywetVincent Biret (baywet) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(clicked the wrong button 🤦)

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants

@MaggieKimani1@baywet@github-advanced-security@darrelmiller