Skip to content

Reading JSON documents unnecessarily creates a buffered copy of the document #1853

@darrelmiller

Description

@darrelmiller

Suggestion to update to something like the following code in OpenApiModelFactory

        public static async Task<ReadResult> LoadAsync(Stream input, string format, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default)
        {
            Utils.CheckArgumentNull(format, nameof(format));
            settings ??= new OpenApiReaderSettings();

            Stream preparedStream;
            if (input is MemoryStream || format == "json")  // Added test for json format
            {
                preparedStream = input;
            }
            else
            {
                // Buffer stream so that OpenApiTextReaderReader can process it synchronously
                // YamlDocument doesn't support async reading.
                preparedStream = new MemoryStream();
                await input.CopyToAsync(preparedStream, 81920, cancellationToken);
                preparedStream.Position = 0;
            }

            // Allow StreamReader to read directly from FileStream or NetworkStream
            using var reader = new StreamReader(preparedStream, default, true, -1, settings.LeaveStreamOpen);  
            return await LoadAsync(reader, format, settings, cancellationToken);
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions