This repository was archived by the owner on Jun 24, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathOpenApiStringReader.cs
More file actions
59 lines (52 loc) · 2.03 KB
/
Copy pathOpenApiStringReader.cs
File metadata and controls
59 lines (52 loc) · 2.03 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.IO;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.Interface;
namespace Microsoft.OpenApi.Readers
{
/// <summary>
/// Service class for converting strings into OpenApiDocument instances
/// </summary>
public class OpenApiStringReader : IOpenApiReader<string, OpenApiDiagnostic>
{
private readonly OpenApiReaderSettings _settings;
/// <summary>
/// Constructor tha allows reader to use non-default settings
/// </summary>
/// <param name="settings"></param>
public OpenApiStringReader(OpenApiReaderSettings settings = null)
{
_settings = settings ?? new OpenApiReaderSettings();
}
/// <summary>
/// Reads the string input and parses it into an Open API document.
/// </summary>
public OpenApiDocument Read(string input, out OpenApiDiagnostic diagnostic)
{
using (var memoryStream = new MemoryStream())
{
var writer = new StreamWriter(memoryStream);
writer.Write(input);
writer.Flush();
memoryStream.Position = 0;
return new OpenApiStreamReader(_settings).Read(memoryStream, out diagnostic);
}
}
/// <summary>
/// Reads the string input and parses it into an Open API element.
/// </summary>
public T ReadFragment<T>(string input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic) where T : IOpenApiElement
{
using (var memoryStream = new MemoryStream())
{
var writer = new StreamWriter(memoryStream);
writer.Write(input);
writer.Flush();
memoryStream.Position = 0;
return new OpenApiStreamReader(_settings).ReadFragment<T>(memoryStream, version, out diagnostic);
}
}
}
}