Skip to content
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

Refactor readers to reduce surface area #1958

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,34 @@
/// </summary>
public class OpenApiYamlReader : IOpenApiReader
{
private const int copyBufferSize = 4096;

/// <inheritdoc/>
public async Task<ReadResult> ReadAsync(TextReader input,
public async Task<ReadResult> ReadAsync(Stream input,
OpenApiReaderSettings settings = null,
CancellationToken cancellationToken = default)
{
if (input is MemoryStream memoryStream)
{
return Read(memoryStream, settings);
} else {
using var preparedStream = new MemoryStream();
await input.CopyToAsync(preparedStream, copyBufferSize, cancellationToken);
preparedStream.Position = 0;
return Read(preparedStream, settings);
}
}

/// <inheritdoc/>
public ReadResult Read(MemoryStream input,
OpenApiReaderSettings settings = null)
{
JsonNode jsonNode;

// Parse the YAML text in the TextReader into a sequence of JsonNodes
try
{
jsonNode = LoadJsonNodesFromYamlDocument(input);
jsonNode = LoadJsonNodesFromYamlDocument(new StreamReader(input)); // Should we leave the stream open?

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning

Disposable 'StreamReader' is created but not disposed.
}
catch (JsonException ex)
{
Expand All @@ -42,11 +59,11 @@
};
}

return await ReadAsync(jsonNode, settings, cancellationToken: cancellationToken);
return Read(jsonNode, settings);
}

/// <inheritdoc/>
public T ReadFragment<T>(TextReader input,
public T ReadFragment<T>(MemoryStream input,
OpenApiSpecVersion version,
out OpenApiDiagnostic diagnostic,
OpenApiReaderSettings settings = null) where T : IOpenApiElement
Expand All @@ -56,7 +73,7 @@
// Parse the YAML
try
{
jsonNode = LoadJsonNodesFromYamlDocument(input);
jsonNode = LoadJsonNodesFromYamlDocument(new StreamReader(input));

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning

Disposable 'StreamReader' is created but not disposed.
}
catch (JsonException ex)
{
Expand All @@ -81,10 +98,10 @@
return yamlDocument.ToJsonNode();
}

/// <inheritdoc/>
public async Task<ReadResult> ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null, CancellationToken cancellationToken = default)
/// <inheritdoc/>
public ReadResult Read(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null)
{
return await OpenApiReaderRegistry.DefaultReader.ReadAsync(jsonNode, settings, OpenApiConstants.Yaml, cancellationToken);
return OpenApiReaderRegistry.DefaultReader.Read(jsonNode, settings, OpenApiConstants.Yaml);
}

/// <inheritdoc/>
Expand Down
19 changes: 13 additions & 6 deletions src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,40 @@ namespace Microsoft.OpenApi.Interfaces
public interface IOpenApiReader
{
/// <summary>
/// Reads the TextReader input and parses it into an Open API document.
/// Async method to reads the stream and parse it into an Open API document.
/// </summary>
/// <param name="input">The TextReader input.</param>
/// <param name="settings"> The OpenApi reader settings.</param>
/// <param name="cancellationToken">Propagates notification that an operation should be cancelled.</param>
/// <returns></returns>
Task<ReadResult> ReadAsync(TextReader input, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default);
Task<ReadResult> ReadAsync(Stream input, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default);

/// <summary>
/// Provides a synchronous method to read the input memory stream and parse it into an Open API document.
/// </summary>
/// <param name="input"></param>
/// <param name="settings"></param>
/// <returns></returns>
ReadResult Read(MemoryStream input, OpenApiReaderSettings settings = null);

/// <summary>
/// Parses the JsonNode input into an Open API document.
/// </summary>
/// <param name="jsonNode">The JsonNode input.</param>
/// <param name="settings">The Reader settings to be used during parsing.</param>
/// <param name="cancellationToken">Propagates notifications that operations should be cancelled.</param>
/// <param name="format">The OpenAPI format.</param>
/// <returns></returns>
Task<ReadResult> ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null, CancellationToken cancellationToken = default);
ReadResult Read(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null);

/// <summary>
/// Reads the TextReader input and parses the fragment of an OpenAPI description into an Open API Element.
/// Reads the MemoryStream and parses the fragment of an OpenAPI description into an Open API Element.
/// </summary>
/// <param name="input">TextReader containing OpenAPI description to parse.</param>
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
/// <param name="settings">The OpenApiReader settings.</param>
/// <returns>Instance of newly created IOpenApiElement.</returns>
T ReadFragment<T>(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement;
T ReadFragment<T>(MemoryStream input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement;

/// <summary>
/// Reads the JsonNode input and parses the fragment of an OpenAPI description into an Open API Element.
Expand Down
27 changes: 1 addition & 26 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@

writer.WriteStartObject();

// openApi;

Check warning on line 140 in src/Microsoft.OpenApi/Models/OpenApiDocument.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125)
writer.WriteProperty(OpenApiConstants.OpenApi, "3.1.1");

// jsonSchemaDialect
Expand Down Expand Up @@ -553,27 +553,13 @@
/// <param name="format">The OpenAPI format to use during parsing.</param>
/// <param name="settings">The OpenApi reader settings.</param>
/// <returns></returns>
public static ReadResult Load(Stream stream,
public static ReadResult Load(MemoryStream stream,
string format,
OpenApiReaderSettings? settings = null)
{
return OpenApiModelFactory.Load(stream, format, settings);
}

/// <summary>
/// Reads the text reader content and parses it into an Open API document.
/// </summary>
/// <param name="input">TextReader containing OpenAPI description to parse.</param>
/// <param name="format"> The OpenAPI format to use during parsing.</param>
/// <param name="settings">The OpenApi reader settings.</param>
/// <returns></returns>
public static ReadResult Load(TextReader input,
string format,
OpenApiReaderSettings? settings = null)
{
return OpenApiModelFactory.Load(input, format, settings);
}

/// <summary>
/// Parses a local file path or Url into an Open API document.
/// </summary>
Expand All @@ -598,17 +584,6 @@
return await OpenApiModelFactory.LoadAsync(stream, format, settings, cancellationToken);
}

/// <summary>
/// Reads the text reader content and parses it into an Open API document.
/// </summary>
/// <param name="input">TextReader containing OpenAPI description to parse.</param>
/// <param name="format"> The OpenAPI format to use during parsing.</param>
/// <param name="settings">The OpenApi reader settings.</param>
/// <returns></returns>
public static async Task<ReadResult> LoadAsync(TextReader input, string format, OpenApiReaderSettings? settings = null)
{
return await OpenApiModelFactory.LoadAsync(input, format, settings);
}

/// <summary>
/// Parses a string into a <see cref="OpenApiDocument"/> object.
Expand Down
87 changes: 51 additions & 36 deletions src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,47 @@
/// </summary>
public class OpenApiJsonReader : IOpenApiReader
{

/// <summary>
/// Reads the memory stream input and parses it into an Open API document.
/// </summary>
/// <param name="input">TextReader containing OpenAPI description to parse.</param>
/// <param name="settings">The Reader settings to be used during parsing.</param>
/// <returns></returns>
public ReadResult Read(MemoryStream input,
OpenApiReaderSettings settings = null)
{
JsonNode jsonNode;
var diagnostic = new OpenApiDiagnostic();
settings ??= new OpenApiReaderSettings();

// Parse the JSON text in the TextReader into JsonNodes
try
{
jsonNode = JsonNode.Parse(input);
}
catch (JsonException ex)
{
diagnostic.Errors.Add(new OpenApiError($"#line={ex.LineNumber}", $"Please provide the correct format, {ex.Message}"));
return new ReadResult
{
OpenApiDocument = null,
OpenApiDiagnostic = diagnostic
};
}

return Read(jsonNode, settings);
}


/// <summary>
/// Reads the stream input and parses it into an Open API document.
/// Reads the stream input asynchronously and parses it into an Open API document.
/// </summary>
/// <param name="input">TextReader containing OpenAPI description to parse.</param>
/// <param name="settings">The Reader settings to be used during parsing.</param>
/// <param name="cancellationToken">Propagates notifications that operations should be cancelled.</param>
/// <returns></returns>
public async Task<ReadResult> ReadAsync(TextReader input,
public async Task<ReadResult> ReadAsync(Stream input,
OpenApiReaderSettings settings = null,
CancellationToken cancellationToken = default)
{
Expand All @@ -42,7 +75,7 @@
// Parse the JSON text in the TextReader into JsonNodes
try
{
jsonNode = LoadJsonNodes(input);
jsonNode = await JsonNode.ParseAsync(input);;
}
catch (JsonException ex)
{
Expand All @@ -54,7 +87,7 @@
};
}

return await ReadAsync(jsonNode, settings, cancellationToken: cancellationToken);
return Read(jsonNode, settings);
}

/// <summary>
Expand All @@ -63,12 +96,10 @@
/// <param name="jsonNode">The JsonNode input.</param>
/// <param name="settings">The Reader settings to be used during parsing.</param>
/// <param name="format">The OpenAPI format.</param>
/// <param name="cancellationToken">Propagates notifications that operations should be cancelled.</param>
/// <returns></returns>
public async Task<ReadResult> ReadAsync(JsonNode jsonNode,
public ReadResult Read(JsonNode jsonNode,
OpenApiReaderSettings settings,
string format = null,
CancellationToken cancellationToken = default)
string format = null)
{
var diagnostic = new OpenApiDiagnostic();
var context = new ParsingContext(diagnostic)
Expand All @@ -84,16 +115,16 @@
// Parse the OpenAPI Document
document = context.Parse(jsonNode);

if (settings.LoadExternalRefs)
{
var diagnosticExternalRefs = await LoadExternalRefsAsync(document, cancellationToken, settings, format);
// Merge diagnostics of external reference
if (diagnosticExternalRefs != null)
{
diagnostic.Errors.AddRange(diagnosticExternalRefs.Errors);
diagnostic.Warnings.AddRange(diagnosticExternalRefs.Warnings);
}
}
// if (settings.LoadExternalRefs)
// {

Check warning on line 119 in src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125)
// var diagnosticExternalRefs = await LoadExternalRefsAsync(document, cancellationToken, settings, format);
// // Merge diagnostics of external reference
// if (diagnosticExternalRefs != null)
// {
// diagnostic.Errors.AddRange(diagnosticExternalRefs.Errors);
// diagnostic.Warnings.AddRange(diagnosticExternalRefs.Warnings);
// }
// }

document.SetReferenceHostDocument();
}
Expand Down Expand Up @@ -124,7 +155,7 @@
}

/// <inheritdoc/>
public T ReadFragment<T>(TextReader input,
public T ReadFragment<T>(MemoryStream input,
OpenApiSpecVersion version,
out OpenApiDiagnostic diagnostic,
OpenApiReaderSettings settings = null) where T : IOpenApiElement
Expand All @@ -134,7 +165,7 @@
// Parse the JSON
try
{
jsonNode = LoadJsonNodes(input);
jsonNode = JsonNode.Parse(input);
}
catch (JsonException ex)
{
Expand Down Expand Up @@ -183,22 +214,6 @@
return (T)element;
}

private JsonNode LoadJsonNodes(TextReader input)
{
var nodes = JsonNode.Parse(input.ReadToEnd());
return nodes;
}

private async Task<OpenApiDiagnostic> LoadExternalRefsAsync(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings, string format = null)
{
// Create workspace for all documents to live in.
var baseUrl = settings.BaseUrl ?? new Uri(OpenApiConstants.BaseRegistryUri);
var openApiWorkSpace = new OpenApiWorkspace(baseUrl);

// Load this root document into the workspace
var streamLoader = new DefaultStreamLoader(settings.BaseUrl);
var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, settings.CustomExternalLoader ?? streamLoader, settings);
return await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, format ?? OpenApiConstants.Json, null, cancellationToken);
}
}
}
Loading
Loading