Skip to content

Commit 8d83694

Browse files
committed
Return the generic output of type T as part of the read result; clean up code
1 parent f30add8 commit 8d83694

File tree

6 files changed

+38
-43
lines changed

6 files changed

+38
-43
lines changed

src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.OpenApi.Reader;
1111
using SharpYaml.Serialization;
1212
using System.Linq;
13-
using Microsoft.OpenApi.Models;
1413

1514
namespace Microsoft.OpenApi.Readers
1615
{
@@ -46,9 +45,9 @@ public async Task<ReadResult> ReadAsync(TextReader input,
4645
}
4746

4847
/// <inheritdoc/>
49-
public async Task<ReadFragmentResult> ReadFragmentAsync<T>(TextReader input,
50-
OpenApiSpecVersion version,
51-
OpenApiReaderSettings settings = null) where T : IOpenApiElement
48+
public async Task<ReadFragmentResult<T>> ReadFragmentAsync<T>(TextReader input,
49+
OpenApiSpecVersion version,
50+
OpenApiReaderSettings settings = null) where T : IOpenApiElement
5251
{
5352
JsonNode jsonNode;
5453

@@ -81,13 +80,13 @@ static JsonNode LoadJsonNodesFromYamlDocument(TextReader input)
8180
}
8281

8382
/// <inheritdoc/>
84-
public async Task<ReadResult> ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null, CancellationToken cancellationToken = default)
83+
public async Task<ReadResult> ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, CancellationToken cancellationToken = default)
8584
{
86-
return await OpenApiReaderRegistry.DefaultReader.ReadAsync(jsonNode, settings, OpenApiConstants.Yaml, cancellationToken);
85+
return await OpenApiReaderRegistry.DefaultReader.ReadAsync(jsonNode, settings, cancellationToken);
8786
}
8887

8988
/// <inheritdoc/>
90-
public async Task<ReadFragmentResult> ReadFragmentAsync<T>(JsonNode input, OpenApiSpecVersion version, OpenApiReaderSettings settings = null) where T : IOpenApiElement
89+
public async Task<ReadFragmentResult<T>> ReadFragmentAsync<T>(JsonNode input, OpenApiSpecVersion version, OpenApiReaderSettings settings = null) where T : IOpenApiElement
9190
{
9291
return await OpenApiReaderRegistry.DefaultReader.ReadFragmentAsync<T>(input, version, settings);
9392
}

src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ public interface IOpenApiReader
2929
/// <param name="jsonNode">The JsonNode input.</param>
3030
/// <param name="settings">The Reader settings to be used during parsing.</param>
3131
/// <param name="cancellationToken">Propagates notifications that operations should be cancelled.</param>
32-
/// <param name="format">The OpenAPI format.</param>
3332
/// <returns></returns>
34-
Task<ReadResult> ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null, CancellationToken cancellationToken = default);
33+
Task<ReadResult> ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, CancellationToken cancellationToken = default);
3534

3635
/// <summary>
3736
/// Reads the TextReader input and parses the fragment of an OpenAPI description into an Open API Element.
@@ -40,7 +39,7 @@ public interface IOpenApiReader
4039
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
4140
/// <param name="settings">The OpenApiReader settings.</param>
4241
/// <returns>Instance of newly created IOpenApiElement.</returns>
43-
Task<ReadFragmentResult> ReadFragmentAsync<T>(TextReader input, OpenApiSpecVersion version, OpenApiReaderSettings settings = null) where T: IOpenApiElement;
42+
Task<ReadFragmentResult<T>> ReadFragmentAsync<T>(TextReader input, OpenApiSpecVersion version, OpenApiReaderSettings settings = null) where T: IOpenApiElement;
4443

4544
/// <summary>
4645
/// Reads the JsonNode input and parses the fragment of an OpenAPI description into an Open API Element.
@@ -49,6 +48,6 @@ public interface IOpenApiReader
4948
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
5049
/// <param name="settings">The OpenApiReader settings.</param>
5150
/// <returns>Instance of newly created IOpenApiElement.</returns>
52-
Task<ReadFragmentResult> ReadFragmentAsync<T>(JsonNode input, OpenApiSpecVersion version, OpenApiReaderSettings settings = null) where T: IOpenApiElement;
51+
Task<ReadFragmentResult<T>> ReadFragmentAsync<T>(JsonNode input, OpenApiSpecVersion version, OpenApiReaderSettings settings = null) where T: IOpenApiElement;
5352
}
5453
}

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,10 @@ public async Task<ReadResult> ReadAsync(TextReader input,
6161
/// </summary>
6262
/// <param name="jsonNode">The JsonNode input.</param>
6363
/// <param name="settings">The Reader settings to be used during parsing.</param>
64-
/// <param name="format">The OpenAPI format.</param>
6564
/// <param name="cancellationToken">Propagates notifications that operations should be cancelled.</param>
6665
/// <returns></returns>
6766
public async Task<ReadResult> ReadAsync(JsonNode jsonNode,
6867
OpenApiReaderSettings settings,
69-
string format = null,
7068
CancellationToken cancellationToken = default)
7169
{
7270
var diagnostic = new OpenApiDiagnostic();
@@ -85,7 +83,7 @@ public async Task<ReadResult> ReadAsync(JsonNode jsonNode,
8583

8684
if (settings.LoadExternalRefs)
8785
{
88-
var diagnosticExternalRefs = await LoadExternalRefsAsync(document, cancellationToken, settings, format);
86+
var diagnosticExternalRefs = await LoadExternalRefsAsync(document, cancellationToken, settings);
8987
// Merge diagnostics of external reference
9088
if (diagnosticExternalRefs != null)
9189
{
@@ -123,9 +121,9 @@ public async Task<ReadResult> ReadAsync(JsonNode jsonNode,
123121
}
124122

125123
/// <inheritdoc/>
126-
public async Task<ReadFragmentResult> ReadFragmentAsync<T>(TextReader input,
127-
OpenApiSpecVersion version,
128-
OpenApiReaderSettings settings = null) where T: IOpenApiElement
124+
public async Task<ReadFragmentResult<T>> ReadFragmentAsync<T>(TextReader input,
125+
OpenApiSpecVersion version,
126+
OpenApiReaderSettings settings = null) where T: IOpenApiElement
129127
{
130128
JsonNode jsonNode;
131129

@@ -145,7 +143,7 @@ public async Task<ReadFragmentResult> ReadFragmentAsync<T>(TextReader input,
145143
}
146144

147145
/// <inheritdoc/>
148-
public async Task<ReadFragmentResult> ReadFragmentAsync<T>(JsonNode input,
146+
public async Task<ReadFragmentResult<T>> ReadFragmentAsync<T>(JsonNode input,
149147
OpenApiSpecVersion version,
150148
OpenApiReaderSettings settings = null) where T : IOpenApiElement
151149
{
@@ -177,9 +175,9 @@ public async Task<ReadFragmentResult> ReadFragmentAsync<T>(JsonNode input,
177175
}
178176
}
179177

180-
return new ReadFragmentResult
178+
return new ReadFragmentResult<T>
181179
{
182-
Element = element,
180+
Element = (T)element,
183181
OpenApiDiagnostic = diagnostic
184182
};
185183
}
@@ -190,7 +188,7 @@ private async Task<JsonNode> LoadJsonNodesAsync(TextReader input)
190188
return JsonNode.Parse(content);
191189
}
192190

193-
private async Task<OpenApiDiagnostic> LoadExternalRefsAsync(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings, string format = null)
191+
private async Task<OpenApiDiagnostic> LoadExternalRefsAsync(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings)
194192
{
195193
// Create workspace for all documents to live in.
196194
var baseUrl = settings.BaseUrl ?? new Uri(OpenApiConstants.BaseRegistryUri);
@@ -199,7 +197,7 @@ private async Task<OpenApiDiagnostic> LoadExternalRefsAsync(OpenApiDocument docu
199197
// Load this root document into the workspace
200198
var streamLoader = new DefaultStreamLoader(settings.BaseUrl);
201199
var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, settings.CustomExternalLoader ?? streamLoader, settings);
202-
return await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, format ?? OpenApiConstants.Json, null, cancellationToken);
200+
return await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, null, cancellationToken);
203201
}
204202
}
205203
}

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -117,9 +117,9 @@ public static async Task<ReadResult> ParseAsync(string input,
117117
/// <param name="version"></param>
118118
/// <param name="settings">The OpenApi reader settings.</param>
119119
/// <returns>An OpenAPI document instance.</returns>
120-
public static async Task<ReadFragmentResult> ParseAsync<T>(string input,
121-
OpenApiSpecVersion version,
122-
OpenApiReaderSettings settings = null) where T : IOpenApiElement
120+
public static async Task<ReadFragmentResult<T>> ParseAsync<T>(string input,
121+
OpenApiSpecVersion version,
122+
OpenApiReaderSettings settings = null) where T : IOpenApiElement
123123
{
124124
var format = input.StartsWith("{") || input.StartsWith("[") ? OpenApiConstants.Json : OpenApiConstants.Yaml;
125125
settings ??= new OpenApiReaderSettings();
@@ -136,10 +136,10 @@ public static async Task<ReadFragmentResult> ParseAsync<T>(string input,
136136
/// <param name="cancellationToken"></param>
137137
/// <returns>Instance of newly created IOpenApiElement.</returns>
138138
/// <returns>The OpenAPI element.</returns>
139-
public static async Task<ReadFragmentResult> LoadAsync<T>(string url,
140-
OpenApiSpecVersion version,
141-
OpenApiReaderSettings settings = null,
142-
CancellationToken cancellationToken = default) where T : IOpenApiElement
139+
public static async Task<ReadFragmentResult<T>> LoadAsync<T>(string url,
140+
OpenApiSpecVersion version,
141+
OpenApiReaderSettings settings = null,
142+
CancellationToken cancellationToken = default) where T : IOpenApiElement
143143
{
144144
var format = await GetFormatAsync(url);
145145
settings ??= new OpenApiReaderSettings();
@@ -156,10 +156,10 @@ public static async Task<ReadFragmentResult> LoadAsync<T>(string url,
156156
/// <param name="settings">The OpenApiReader settings.</param>
157157
/// <returns>Instance of newly created IOpenApiElement.</returns>
158158
/// <returns>The OpenAPI element.</returns>
159-
public static async Task<ReadFragmentResult> LoadAsync<T>(Stream input,
160-
OpenApiSpecVersion version,
161-
string format = null,
162-
OpenApiReaderSettings settings = null) where T : IOpenApiElement
159+
public static async Task<ReadFragmentResult<T>> LoadAsync<T>(Stream input,
160+
OpenApiSpecVersion version,
161+
string format = null,
162+
OpenApiReaderSettings settings = null) where T : IOpenApiElement
163163
{
164164
format ??= InspectStreamFormat(input);
165165
using var reader = new StreamReader(input);
@@ -175,10 +175,10 @@ public static async Task<ReadFragmentResult> LoadAsync<T>(Stream input,
175175
/// <param name="settings">The OpenApiReader settings.</param>
176176
/// <returns>Instance of newly created IOpenApiElement.</returns>
177177
/// <returns>The OpenAPI element.</returns>
178-
public static async Task<ReadFragmentResult> LoadAsync<T>(TextReader input,
179-
OpenApiSpecVersion version,
180-
string format = null,
181-
OpenApiReaderSettings settings = null) where T : IOpenApiElement
178+
public static async Task<ReadFragmentResult<T>> LoadAsync<T>(TextReader input,
179+
OpenApiSpecVersion version,
180+
string format = null,
181+
OpenApiReaderSettings settings = null) where T : IOpenApiElement
182182
{
183183
format ??= InspectTextReaderFormat(input);
184184
return await OpenApiReaderRegistry.GetReader(format).ReadFragmentAsync<T>(input, version, settings);

src/Microsoft.OpenApi/Reader/ReadResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public class ReadResult
2424
/// <summary>
2525
/// Container object used for returning the result of reading an OpenAPI fragment
2626
/// </summary>
27-
public class ReadFragmentResult
27+
public class ReadFragmentResult<T> where T: IOpenApiElement
2828
{
2929
/// <summary>
3030
/// The parsed fragment.
3131
/// </summary>
32-
public IOpenApiElement Element { get; set; }
32+
public T Element { get; set; }
3333

3434
/// <summary>
3535
/// OpenApiDiagnostic contains the Errors reported while parsing

src/Microsoft.OpenApi/Reader/Services/OpenApiWorkspaceLoader.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public OpenApiWorkspaceLoader(OpenApiWorkspace workspace, IStreamLoader loader,
2222

2323
internal async Task<OpenApiDiagnostic> LoadAsync(OpenApiReference reference,
2424
OpenApiDocument document,
25-
string format = null,
2625
OpenApiDiagnostic diagnostic = null,
2726
CancellationToken cancellationToken = default)
2827
{
@@ -46,15 +45,15 @@ internal async Task<OpenApiDiagnostic> LoadAsync(OpenApiReference reference,
4645
if (!_workspace.Contains(item.ExternalResource))
4746
{
4847
var input = await _loader.LoadAsync(new(item.ExternalResource, UriKind.RelativeOrAbsolute));
49-
var result = await OpenApiDocument.LoadAsync(input, format, _readerSettings, cancellationToken);
48+
var result = await OpenApiDocument.LoadAsync(input, _readerSettings, cancellationToken);
5049
// Merge diagnostics
5150
if (result.OpenApiDiagnostic != null)
5251
{
5352
diagnostic.AppendDiagnostic(result.OpenApiDiagnostic, item.ExternalResource);
5453
}
5554
if (result.OpenApiDocument != null)
5655
{
57-
var loadDiagnostic = await LoadAsync(item, result.OpenApiDocument, format, diagnostic, cancellationToken);
56+
var loadDiagnostic = await LoadAsync(item, result.OpenApiDocument, diagnostic, cancellationToken);
5857
diagnostic = loadDiagnostic;
5958
}
6059
}

0 commit comments

Comments
 (0)