Skip to content

Commit 1464e2c

Browse files
committed
Move load methods to be adjacent
1 parent 634cb1c commit 1464e2c

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 42 additions & 1 deletion
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;
@@ -65,6 +65,47 @@ public static ReadResult Load(MemoryStream stream,
6565
return result;
6666
}
6767

68+
/// <summary>
69+
/// Reads the stream input and ensures it is buffered before passing it to the Load method.
70+
/// </summary>
71+
/// <typeparam name="T"></typeparam>
72+
/// <param name="input"></param>
73+
/// <param name="version"></param>
74+
/// <param name="format"></param>
75+
/// <param name="diagnostic"></param>
76+
/// <param name="settings"></param>
77+
/// <returns></returns>
78+
public static T Load<T>(Stream input, OpenApiSpecVersion version, string format, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement
79+
{
80+
if (input is MemoryStream memoryStream)
81+
{
82+
return Load<T>(memoryStream, version, format, out diagnostic, settings);
83+
}
84+
else
85+
{
86+
memoryStream = new MemoryStream();
87+
input.CopyTo(memoryStream);
88+
memoryStream.Position = 0;
89+
return Load<T>(memoryStream, version, format, out diagnostic, settings);
90+
}
91+
}
92+
93+
/// <summary>
94+
/// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element.
95+
/// </summary>
96+
/// <typeparam name="T"></typeparam>
97+
/// <param name="input">Stream containing OpenAPI description to parse.</param>
98+
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
99+
/// <param name="format"></param>
100+
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
101+
/// <param name="settings">The OpenApiReader settings.</param>
102+
/// <returns>Instance of newly created IOpenApiElement.</returns>
103+
/// <returns>The OpenAPI element.</returns>
104+
public static T Load<T>(MemoryStream input, OpenApiSpecVersion version, string format, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement
105+
{
106+
format ??= OpenApiConstants.Json;
107+
return OpenApiReaderRegistry.GetReader(format).ReadFragment<T>(input, version, out diagnostic, settings);
108+
}
68109

69110
/// <summary>
70111
/// Loads the input URL and parses it into an Open API document.

0 commit comments

Comments
 (0)