Repository navigation
[OpenAPI] Avoid double PipeReader.AdvanceTo when reading @body #9961
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
Merged
tobias-tengler
merged 12 commits into
ChilliCream:main
from
hemed:fix/openapi-adapter-double-advanceto
Jun 22, 2026
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
28b9bd3
fix(adapters-openapi): avoid double PipeReader.AdvanceTo when reading…
hemed ad19ee6
test(adapters-openapi): add Kestrel regression tests for @body endpoints
hemed 53b0d0c
chore(adapters-openapi): trim comments in body-read fix
hemed c7f855f
chore(adapters-openapi): trim comments in Kestrel regression tests
hemed db942d5
chore(adapters-openapi): single-line guard clauses in body read
hemed b89f9ad
chore(adapters-openapi): inline guard clauses
hemed 8f685a9
chore(adapters-openapi): two-line guard clauses
hemed 5aa4013
refactor(adapters-openapi): keep do/while, advance only while data pe…
hemed 422f1ed
style(adapters-openapi): blank line before do/while condition
hemed af25323
Merge branch 'main' into fix/openapi-adapter-double-advanceto
hemed 44d8bc4
fix(adapters-openapi): advance reader in finally + add braces
hemed c7dc2ee
test(adapters-openapi): drop id from PUT body (supplied by route)
hemed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...ate/Adapters/test/Adapters.OpenApi.Tests/Endpoints/KestrelHttpEndpointIntegrationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| using System.Net; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Hosting.Server.Features; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace HotChocolate.Adapters.OpenApi; | ||
|
|
||
| // Regression tests for @body endpoints. They must run on a real Kestrel host: | ||
| // TestServer's in-memory body PipeReader tolerates the double AdvanceTo bug, | ||
| // only Kestrel enforces the contract and returns 500. | ||
| public sealed class KestrelHttpEndpointIntegrationTests : OpenApiTestBase | ||
| { | ||
| protected override void ConfigureStorage( | ||
| IServiceCollection services, | ||
| IOpenApiDefinitionStorage storage, | ||
| OpenApiDiagnosticEventListener? eventListener) | ||
| { | ||
| services.AddGraphQLServer() | ||
| .AddOpenApi() | ||
| .AddOpenApiDefinitionStorage(storage) | ||
| .AddBasicServer(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Http_Post_Body_Succeeds_On_Kestrel() | ||
| { | ||
| var cancellationToken = TestContext.Current.CancellationToken; | ||
| var storage = CreateBasicTestDefinitionStorage(); | ||
| var host = await StartKestrelHostAsync(storage, cancellationToken); | ||
|
|
||
| try | ||
| { | ||
| using var client = new HttpClient { BaseAddress = GetBaseAddress(host) }; | ||
| var content = new StringContent( | ||
| """{ "id": "6", "name": "Test", "email": "test@example.com" }""", | ||
| Encoding.UTF8, | ||
| "application/json"); | ||
|
|
||
| var response = await client.PostAsync("/users", content, cancellationToken); | ||
|
|
||
| Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
|
|
||
| var body = await response.Content.ReadAsStringAsync(cancellationToken); | ||
| using var json = JsonDocument.Parse(body); | ||
| Assert.Equal(JsonValueKind.Object, json.RootElement.ValueKind); | ||
| Assert.True(json.RootElement.TryGetProperty("name", out _)); | ||
| } | ||
| finally | ||
| { | ||
| await host.StopAsync(cancellationToken); | ||
| host.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Http_Put_Body_Succeeds_On_Kestrel() | ||
| { | ||
| var cancellationToken = TestContext.Current.CancellationToken; | ||
| var storage = CreateBasicTestDefinitionStorage(); | ||
| var host = await StartKestrelHostAsync(storage, cancellationToken); | ||
|
|
||
| try | ||
| { | ||
| using var client = new HttpClient { BaseAddress = GetBaseAddress(host) }; | ||
| var content = new StringContent( | ||
| """{ "id": "6", "name": "Test", "email": "test@example.com" }""", | ||
| Encoding.UTF8, | ||
| "application/json"); | ||
|
|
||
| var response = await client.PutAsync("/users/6", content, cancellationToken); | ||
|
|
||
| Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
| } | ||
| finally | ||
| { | ||
| await host.StopAsync(cancellationToken); | ||
| host.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| private async Task<IWebHost> StartKestrelHostAsync( | ||
| IOpenApiDefinitionStorage storage, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var host = new WebHostBuilder() | ||
| .UseKestrel() | ||
| .UseUrls("http://127.0.0.1:0") | ||
| .ConfigureServices(services => | ||
| { | ||
| services.AddRouting(); | ||
|
|
||
| services.AddGraphQLServer() | ||
| .AddOpenApi() | ||
| .AddOpenApiDefinitionStorage(storage) | ||
| .AddBasicServer(); | ||
| }) | ||
| .Configure(app => | ||
| { | ||
| app.UseRouting(); | ||
| app.UseEndpoints(endpoints => | ||
| { | ||
| endpoints.MapOpenApiEndpoints(); | ||
| endpoints.MapGraphQL(); | ||
| }); | ||
| }) | ||
| .Build(); | ||
|
|
||
| await host.StartAsync(cancellationToken); | ||
| return host; | ||
| } | ||
|
|
||
| private static Uri GetBaseAddress(IWebHost host) | ||
| { | ||
| var addresses = host.ServerFeatures.Get<IServerAddressesFeature>()?.Addresses | ||
| ?? throw new InvalidOperationException("Kestrel did not expose any server addresses."); | ||
|
|
||
| var address = addresses.FirstOrDefault() | ||
| ?? throw new InvalidOperationException("Kestrel did not bind to any address."); | ||
|
|
||
| return new Uri(address); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.