Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/All.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@
<Project Path="HotChocolate/Adapters/src/Adapters.OpenApi.Core/HotChocolate.Adapters.OpenApi.Core.csproj" />
<Project Path="HotChocolate/Adapters/src/Fusion.Adapters.Mcp/HotChocolate.Fusion.Adapters.Mcp.csproj" />
<Project Path="HotChocolate/Adapters/src/Fusion.Adapters.OpenApi/HotChocolate.Fusion.Adapters.OpenApi.csproj" />
<Project Path="HotChocolate/Adapters/src/Adapters.OpenApi.Packaging/HotChocolate.Adapters.OpenApi.Packaging.csproj" />
</Folder>
<Folder Name="/HotChocolate/Adapters/test/">
<Project Path="HotChocolate/Adapters/test/Adapters.Mcp.Tests/HotChocolate.Adapters.Mcp.Tests.csproj" />
<Project Path="HotChocolate/Adapters/test/Adapters.OpenApi.Tests/HotChocolate.Adapters.OpenApi.Tests.csproj" />
<Project Path="HotChocolate/Adapters/test/Adapters.OpenApi.Packaging.Tests/HotChocolate.Adapters.OpenApi.Packaging.Tests.csproj" />
</Folder>
<Folder Name="/HotChocolate/Fusion-vnext/" />
<Folder Name="/HotChocolate/Fusion-vnext/benchmarks/">
Expand Down
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<PropertyGroup>
<TargetFrameworks>net10.0; net9.0; net8.0</TargetFrameworks>
<ExtendedTargetFrameworks>net10.0; net9.0; net8.0; netstandard2.0</ExtendedTargetFrameworks>
<OpenApiTargetFrameworks>net10.0; net9.0</OpenApiTargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/HotChocolate/Adapters/Adapters.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Folder Name="/src/Adapters.OpenApi/">
<Project Path="src\Adapters.OpenApi.Core\HotChocolate.Adapters.OpenApi.Core.csproj" />
<Project Path="src\Adapters.OpenApi\HotChocolate.Adapters.OpenApi.csproj" />
<Project Path="src\Adapters.OpenApi.Packaging\HotChocolate.Adapters.OpenApi.Packaging.csproj" />
<Project Path="src\Fusion.Adapters.OpenApi\HotChocolate.Fusion.Adapters.OpenApi.csproj" />
</Folder>
<Folder Name="/test/" />
Expand All @@ -16,5 +17,6 @@
</Folder>
<Folder Name="/test/Adapters.OpenApi.Tests/">
<Project Path="test\Adapters.OpenApi.Tests\HotChocolate.Adapters.OpenApi.Tests.csproj" />
<Project Path="test\Adapters.OpenApi.Packaging.Tests\HotChocolate.Adapters.OpenApi.Packaging.Tests.csproj" />
</Folder>
</Solution>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<RootNamespace>HotChocolate.Adapters.OpenApi</RootNamespace>
<PackageId>HotChocolate.Adapters.OpenApi.Core</PackageId>
<TargetFrameworks>net10.0; net9.0</TargetFrameworks>
<TargetFrameworks>$(OpenApiTargetFrameworks)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Immutable;

namespace HotChocolate.Adapters.OpenApi.Packaging;

/// <summary>
/// Contains metadata about an OpenAPI collection archive.
/// </summary>
public record ArchiveMetadata
{
/// <summary>
/// Gets or sets the version of the OpenAPI collection archive format specification.
/// Used to ensure compatibility between different versions of tooling.
/// </summary>
public Version FormatVersion { get; init; } = new("1.0.0");

/// <summary>
/// Gets or sets the names of endpoints contained in this archive.
/// </summary>
public ImmutableArray<string> Endpoints { get; init; } = [];

/// <summary>
/// Gets or sets the names of models contained in this archive.
/// </summary>
public ImmutableArray<string> Models { get; init; } = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
using System.Buffers;
using System.IO.Compression;

namespace HotChocolate.Adapters.OpenApi.Packaging;

internal sealed class ArchiveSession : IDisposable
{
private readonly Dictionary<string, FileEntry> _files = [];
private readonly ZipArchive _archive;
private readonly OpenApiCollectionArchiveReadOptions _readOptions;
private OpenApiCollectionArchiveMode _mode;
private bool _disposed;

public ArchiveSession(
ZipArchive archive,
OpenApiCollectionArchiveMode mode,
OpenApiCollectionArchiveReadOptions readOptions)
{
ArgumentNullException.ThrowIfNull(archive);

_archive = archive;
_mode = mode;
_readOptions = readOptions;
}

public bool HasUncommittedChanges
=> _files.Values.Any(file => file.State is not FileState.Read);

public IEnumerable<string> GetFiles()
{
var tempFiles = _files.Where(file => file.Value.State is not FileState.Deleted).Select(file => file.Key);

if (_mode is OpenApiCollectionArchiveMode.Create)
{
return tempFiles;
}

var files = new HashSet<string>(tempFiles);

foreach (var entry in _archive.Entries)
{
files.Add(entry.FullName);
}

return files;
}

public async Task<bool> ExistsAsync(string path, FileKind kind, CancellationToken cancellationToken)
{
if (_files.TryGetValue(path, out var file))
{
return file.State is not FileState.Deleted;
}

if (_mode is not OpenApiCollectionArchiveMode.Create && _archive.GetEntry(path) is { } entry)
{
file = FileEntry.Read(path);
await ExtractFileAsync(entry, file, GetAllowedSize(kind), cancellationToken);
_files.Add(path, file);
return true;
}

return false;
}

public bool Exists(string path)
{
if (_files.TryGetValue(path, out var file))
{
return file.State is not FileState.Deleted;
}

return _mode is not OpenApiCollectionArchiveMode.Create && _archive.GetEntry(path) is not null;
}

public async Task<Stream> OpenReadAsync(string path, FileKind kind, CancellationToken cancellationToken)
{
if (_files.TryGetValue(path, out var file))
{
if (file.State is FileState.Deleted)
{
throw new FileNotFoundException(path);
}

return File.OpenRead(file.TempPath);
}

if (_mode is not OpenApiCollectionArchiveMode.Create && _archive.GetEntry(path) is { } entry)
{
file = FileEntry.Read(path);
await ExtractFileAsync(entry, file, GetAllowedSize(kind), cancellationToken);
var stream = File.OpenRead(file.TempPath);
_files.Add(path, file);
return stream;
}

throw new FileNotFoundException(path);
}

public Stream OpenWrite(string path)
{
if (_mode is OpenApiCollectionArchiveMode.Read)
{
throw new InvalidOperationException("Cannot write to a read-only archive.");
}

if (_files.TryGetValue(path, out var file))
{
file.MarkMutated();
return File.Open(file.TempPath, FileMode.Create, FileAccess.Write);
}

if (_mode is not OpenApiCollectionArchiveMode.Create && _archive.GetEntry(path) is not null)
{
file = FileEntry.Read(path);
file.MarkMutated();
}

file ??= FileEntry.Created(path);
var stream = File.Open(file.TempPath, FileMode.Create, FileAccess.Write);
_files.Add(path, file);
Comment on lines +117 to +121
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a file entry is created on line 115-116 but already exists in the archive, the entry is not added to _files dictionary before opening the write stream on line 120. This means on line 121, _files.Add(path, file) will try to add an entry that was already used on line 115, but since it was never added to the dictionary, line 121 succeeds. However, the logic flow is confusing. If the intent is to handle replacement, the file should be added to _files after line 116, not after line 120. Otherwise, multiple calls to OpenWrite for the same path between lines 113-117 could create multiple FileEntry instances.

Suggested change
}
file ??= FileEntry.Created(path);
var stream = File.Open(file.TempPath, FileMode.Create, FileAccess.Write);
_files.Add(path, file);
_files.Add(path, file);
}
file ??= FileEntry.Created(path);
var stream = File.Open(file.TempPath, FileMode.Create, FileAccess.Write);
if (!_files.ContainsKey(path))
{
_files.Add(path, file);
}

Copilot uses AI. Check for mistakes.
return stream;
}

public void SetMode(OpenApiCollectionArchiveMode mode)
{
_mode = mode;
}

public async Task CommitAsync(CancellationToken cancellationToken)
{
foreach (var file in _files.Values)
{
#if NET10_0_OR_GREATER
switch (file.State)
{
case FileState.Created:
await _archive.CreateEntryFromFileAsync(
file.TempPath,
file.Path,
cancellationToken: cancellationToken);
break;

case FileState.Replaced:
_archive.GetEntry(file.Path)?.Delete();
await _archive.CreateEntryFromFileAsync(
file.TempPath,
file.Path,
cancellationToken);
break;

case FileState.Deleted:
_archive.GetEntry(file.Path)?.Delete();
break;
}
#else
switch (file.State)
{
case FileState.Created:
_archive.CreateEntryFromFile(file.TempPath, file.Path);
break;

case FileState.Replaced:
_archive.GetEntry(file.Path)?.Delete();
_archive.CreateEntryFromFile(file.TempPath, file.Path);
break;

case FileState.Deleted:
_archive.GetEntry(file.Path)?.Delete();
break;
}

await Task.CompletedTask;
#endif

file.MarkRead();
}
}

private static async Task ExtractFileAsync(
ZipArchiveEntry zipEntry,
FileEntry fileEntry,
int maxAllowedSize,
CancellationToken cancellationToken)
{
var buffer = ArrayPool<byte>.Shared.Rent(4096);
var consumed = 0;

await using var readStream = zipEntry.Open();
await using var writeStream = File.Open(fileEntry.TempPath, FileMode.Create, FileAccess.Write);

int read;
while ((read = await readStream.ReadAsync(buffer, cancellationToken)) > 0)
{
consumed += read;

if (consumed > maxAllowedSize)
{
throw new InvalidOperationException(
$"File is too large and exceeds the allowed size of {maxAllowedSize}.");
}

await writeStream.WriteAsync(buffer.AsMemory(0, read), cancellationToken);
}
Comment on lines +188 to +204
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rented buffer from ArrayPool<byte>.Shared.Rent(4096) is never returned to the pool. This causes a memory leak. Add a try-finally block to ensure the buffer is returned using ArrayPool<byte>.Shared.Return(buffer) before the method exits, even if an exception is thrown.

Suggested change
await using var readStream = zipEntry.Open();
await using var writeStream = File.Open(fileEntry.TempPath, FileMode.Create, FileAccess.Write);
int read;
while ((read = await readStream.ReadAsync(buffer, cancellationToken)) > 0)
{
consumed += read;
if (consumed > maxAllowedSize)
{
throw new InvalidOperationException(
$"File is too large and exceeds the allowed size of {maxAllowedSize}.");
}
await writeStream.WriteAsync(buffer.AsMemory(0, read), cancellationToken);
}
try
{
await using var readStream = zipEntry.Open();
await using var writeStream = File.Open(fileEntry.TempPath, FileMode.Create, FileAccess.Write);
int read;
while ((read = await readStream.ReadAsync(buffer, cancellationToken)) > 0)
{
consumed += read;
if (consumed > maxAllowedSize)
{
throw new InvalidOperationException(
$"File is too large and exceeds the allowed size of {maxAllowedSize}.");
}
await writeStream.WriteAsync(buffer.AsMemory(0, read), cancellationToken);
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}

Copilot uses AI. Check for mistakes.
}

private int GetAllowedSize(FileKind kind)
=> kind switch
{
FileKind.Operation or FileKind.Fragment
=> _readOptions.MaxAllowedOperationSize,
FileKind.Settings or FileKind.Metadata
=> _readOptions.MaxAllowedSettingsSize,
_ => throw new ArgumentOutOfRangeException(nameof(kind), kind, null)
};

public void Dispose()
{
if (_disposed)
{
return;
}

foreach (var file in _files.Values)
{
if (file.State is not FileState.Deleted && File.Exists(file.TempPath))
{
try
{
File.Delete(file.TempPath);
}
catch
{
// ignore
}
}
}

_disposed = true;
}

private class FileEntry
{
private FileEntry(string path, string tempPath, FileState state)
{
Path = path;
TempPath = tempPath;
State = state;
}

public string Path { get; }

public string TempPath { get; }

public FileState State { get; private set; }

public void MarkMutated()
{
if (State is FileState.Read or FileState.Deleted)
{
State = FileState.Replaced;
}
}

public void MarkRead()
{
State = FileState.Read;
}

public static FileEntry Created(string path)
=> new(path, GetRandomTempFileName(), FileState.Created);

public static FileEntry Read(string path)
=> new(path, GetRandomTempFileName(), FileState.Read);

private static string GetRandomTempFileName()
{
var tempDir = System.IO.Path.GetTempPath();
var fileName = System.IO.Path.GetRandomFileName();
return System.IO.Path.Combine(tempDir, fileName);
}
}

private enum FileState
{
Read,
Created,
Replaced,
Deleted
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace HotChocolate.Adapters.OpenApi.Packaging;

internal enum FileKind
{
Unknown,
Operation,
Settings,
Fragment,
Metadata
}
Loading
Loading