-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathOpenApiStreamReader.cs
97 lines (88 loc) · 4.09 KB
/
OpenApiStreamReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.Interface;
namespace Microsoft.OpenApi.Readers
{
/// <summary>
/// Service class for converting streams into OpenApiDocument instances
/// </summary>
public class OpenApiStreamReader : IOpenApiReader<Stream, OpenApiDiagnostic>
{
private readonly OpenApiReaderSettings _settings;
/// <summary>
/// Create stream reader with custom settings if desired.
/// </summary>
/// <param name="settings"></param>
public OpenApiStreamReader(OpenApiReaderSettings settings = null)
{
_settings = settings ?? new OpenApiReaderSettings();
if ((_settings.ReferenceResolution == ReferenceResolutionSetting.ResolveAllReferences || _settings.LoadExternalRefs)
&& _settings.BaseUrl == null)
{
throw new ArgumentException("BaseUrl must be provided to resolve external references.");
}
}
/// <summary>
/// Reads the stream input and parses it into an Open API document.
/// </summary>
/// <param name="input">Stream containing OpenAPI description to parse.</param>
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
/// <returns>Instance of newly created OpenApiDocument.</returns>
public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
{
var reader = new StreamReader(input);
var result = new OpenApiTextReaderReader(_settings).Read(reader, out diagnostic);
if (!_settings.LeaveStreamOpen)
{
reader.Dispose();
}
return result;
}
/// <summary>
/// Reads the stream input and parses it into an Open API document.
/// </summary>
/// <param name="input">Stream containing OpenAPI description to parse.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Instance result containing newly created OpenApiDocument and diagnostics object from the process</returns>
public async Task<ReadResult> ReadAsync(Stream input, CancellationToken cancellationToken = default)
{
MemoryStream bufferedStream;
if (input is MemoryStream)
{
bufferedStream = (MemoryStream)input;
}
else
{
// Buffer stream so that OpenApiTextReaderReader can process it synchronously
// YamlDocument doesn't support async reading.
bufferedStream = new MemoryStream();
await input.CopyToAsync(bufferedStream, 81920, cancellationToken);
bufferedStream.Position = 0;
}
using (var reader = new StreamReader(bufferedStream))
{
return await new OpenApiTextReaderReader(_settings).ReadAsync(reader, cancellationToken);
}
}
/// <summary>
/// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element.
/// </summary>
/// <param name="input">Stream 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>
/// <returns>Instance of newly created OpenApiDocument</returns>
public T ReadFragment<T>(Stream input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic) where T : IOpenApiReferenceable
{
using (var reader = new StreamReader(input))
{
return new OpenApiTextReaderReader(_settings).ReadFragment<T>(reader, version, out diagnostic);
}
}
}
}