|
1 | | -// Copyright (c) Microsoft Corporation. All rights reserved. |
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
4 | 4 | using System; |
@@ -65,6 +65,47 @@ public static ReadResult Load(MemoryStream stream, |
65 | 65 | return result; |
66 | 66 | } |
67 | 67 |
|
| 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 | + } |
68 | 109 |
|
69 | 110 | /// <summary> |
70 | 111 | /// Loads the input URL and parses it into an Open API document. |
|
0 commit comments