Skip to content

Fix: loading the document requires knowing the format in advance - #1929

Closed
Maggie Kimani (MaggieKimani1) wants to merge 20 commits into
devfrom
mk/inspect-stream-format
Closed

Fix: loading the document requires knowing the format in advance#1929
Maggie Kimani (MaggieKimani1) wants to merge 20 commits into
devfrom
mk/inspect-stream-format

Conversation

@MaggieKimani1

@MaggieKimani1Maggie Kimani (MaggieKimani1) commented Nov 14, 2024

Copy link
Copy Markdown
Contributor

This PR:

  • Adds logic for inspecting the first character of the stream we receive during document loading to know whether its in JSON or YAML. This helps avoid a regression in functionality where users are required to pass the format of the document when loading an OpenAPI document
  • Replaces sync methods with async ones to prevent deadlocks
  • Passes a cancellation token to OpenApiModelFactory.GetStreamAsync to allow for cancellation of tasks/requests
  • Updates tests with new loading pattern

Fixes#1917, #1918

Comment threadsrc/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs Fixed
@MaggieKimani1
Maggie Kimani (MaggieKimani1) marked this pull request as draft November 14, 2024 09:49
@MaggieKimani1
Maggie Kimani (MaggieKimani1) marked this pull request as ready for review November 14, 2024 14:10
Signed-off-by: Vincent Biret <vibiret@microsoft.com>

@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.

Thanks for getting this started.
A couple of remarks

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

Copy link
Copy Markdown

Quality Gate FailedQuality Gate failed

Failed conditions
77.1% Coverage on New Code (required ≥ 80%)
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

try
{
jsonNode = LoadJsonNodesFromYamlDocument(input);
jsonNode = await Task.Run(() => LoadJsonNodesFromYamlDocument(input));

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.

why are we adding the task run here?

@MaggieKimani1Maggie Kimani (MaggieKimani1)Nov 19, 2024

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

The caller is async here

publicstaticasyncTask<ReadFragmentResult<T>>LoadAsync<T>(Streaminput,

And the interface method's equivalent in the JsonReader class is also async

publicasyncTask<ReadFragmentResult<T>>ReadFragmentAsync<T>(TextReaderinput,

If I change this method's signature to be synchronous, I'll also need to update this JSON content to be read synchronously:

varcontent=awaitinput.ReadToEndAsync();

@MaggieKimani1

Copy link
Copy Markdown
ContributorAuthor

Vincent Biret (@baywet) just a head's up, the functionality on inspecting the input stream to determine the format might change based on my discussion with Darrel (@darrelmiller) here #1918 (comment)

@baywet

Copy link
Copy Markdown
Member

Maggie Kimani (@MaggieKimani1) are we going to implement those changes in this PR or in a subsequent one?

@MaggieKimani1

Copy link
Copy Markdown
ContributorAuthor

Maggie Kimani (@MaggieKimani1) are we going to implement those changes in this PR or in a subsequent one?

I can implement it here so as not to lose the other changes on moving to async

Co-authored-by: Vincent Biret <vibiret@microsoft.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v2 - We should NOT do sync over async

3 participants

@MaggieKimani1@baywet@github-advanced-security