OpenAPI.NET icon indicating copy to clipboard operation
OpenAPI.NET copied to clipboard

Initialization of StreamReader in OpenApiStreamReader throws an ArgumentNullException

Open srev-saxo opened this issue 1 year ago • 2 comments

Describe the bug Initialization of StreamReader in OpenApiStreamReader throws an ArgumentNullException in version 1.6.14.

To Reproduce The behavior can be reproduced by calling OpenApiStreamReader.ReadAsync() from within a .NET source generator. The exception is thrown when the source generator is run.

The bug seems to be related to this commit https://github.com/microsoft/OpenAPI.NET/pull/1585/commits/9a47cb2198e2c433f7c4842fa1421c3cb90950c3 where the StreamReader constructor is changed. Version 1.6.13 did not have the same behavior.

To verify, the same behavior can be reproduced by simply loading a file from within a .NET source generator passing a null-value encoding to the StreamReader in the same manner as is done inside OpenApiStreamReader.ReadAsync():

var reader = new StreamReader(inputStream, encoding: default, detectEncodingFromByteOrderMarks: true, bufferSize: -1, leaveOpen: false);

Additional context OAS documents are loaded from an external stream from within a .NET source generator. The exception is thrown when the compiler invokes the source generator, making the source generator fail. The Visual Studio version is 17.9.3 The source generator targets netstandard2.0 The project that uses the source generator targets .net8

srev-saxo avatar Apr 18 '24 08:04 srev-saxo

Another data point is that my net8.0-windows unit test worked where the net48 unit test failed after upgrading from 1.6.13 to 1.6.14.

Kim-SSi avatar May 08 '24 02:05 Kim-SSi

On .NET Framework, StreamReader's constructor will throw an exception if encoding is null or if bufferSize <= 0... this call does both.

stephentoub avatar May 18 '24 03:05 stephentoub