Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentOutOfRangeException thrown if empty stream passed to OpenApiDocument.Load #2130

Closed
martincostello opened this issue Feb 6, 2025 · 1 comment
Milestone

Comments

@martincostello
Copy link
Contributor

Describe the bug

If an empty MemoryStream is passed to OpenApiDocument.Load(), an ArgumentOutOfRangeException is thrown from down in the internal call stack of the method:

System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

  Stack Trace: 
List`1.get_Item(Int32 index)
OpenApiYamlReader.LoadJsonNodesFromYamlDocument(TextReader input)
OpenApiYamlReader.Read(MemoryStream input, OpenApiReaderSettings settings)
OpenApiModelFactory.InternalLoad(MemoryStream input, String format, OpenApiReaderSettings settings)
OpenApiModelFactory.Load(MemoryStream stream, String format, OpenApiReaderSettings settings)
OpenApiDocument.Load(MemoryStream stream, String format, OpenApiReaderSettings settings)

The public surface of the API should perform some basic validation to provide a more useful exception/message to the user, or at a minimum this line of code should check the array contains at least 1 element:

var yamlDocument = yamlStream.Documents[0];

OpenApi File To Reproduce

N/A

Expected behavior

A useful exception is thrown to the user.

Screenshots/Code Snippets

OpenApiDocument.Load(new MemoryStream());

Additional context

Discovered while migrating to Microsoft.OpenApi 2.0.0-preview.5 in domaindrivendev/Swashbuckle.AspNetCore#3252 due to forgetting to update the stream position after copying a FileStream to a MemoryStream.

public static void AddOpenApiFile(this ApiTestRunnerOptions options, string documentName, string filePath)
{
    using var fileStream = File.OpenRead(filePath);
+   using var memoryStream = new MemoryStream();

    fileStream.CopyTo(memoryStream);
+   // Line below was missing
+   //memoryStream.Seek(0, SeekOrigin.Begin);

-   var openApiDocument = new OpenApiStreamReader().Read(fileStream, out var diagnostic);
-   options.OpenApiDocs.Add(documentName, openApiDocument);
+   var result = OpenApiDocument.Load(memoryStream);
+   options.OpenApiDocs.Add(documentName, result.Document);
}
@MaggieKimani1
Copy link
Contributor

MaggieKimani1 commented Feb 6, 2025

@martincostello thank you for pointing out this bug. We'll add validation to allow for graceful degradation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants