-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Verify MVC / Razor Pages support for unions #67005
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
DeagleGross
merged 5 commits into
dotnet:main
from
DeagleGross:dmkorolev/unions/mvc+razor
Jun 8, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
252319c
MVC support and testing
DeagleGross ba87e90
Merge branch 'main' into dmkorolev/unions/mvc+razor
DeagleGross ee5b95c
apply fixes based on runtime changes
DeagleGross f338160
Merge branch 'dmkorolev/unions/mvc+razor' of https://github.com/Deagl…
DeagleGross d90749e
apply test fixes
DeagleGross 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Reflection; | ||
| using System.Text; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.InternalTesting; | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Microsoft.AspNetCore.Mvc.FunctionalTests; | ||
|
|
||
| // Verifies that C# union types flow through the Razor Pages handler pipeline. The | ||
| // underlying JSON formatters are the same SystemTextJsonInputFormatter / | ||
| // SystemTextJsonOutputFormatter that MVC controllers use — this test class exists as a | ||
| // smoke test that confirms Razor Pages model binding for [FromBody] union parameters | ||
| // and IActionResult JsonResult responses behave the same way for unions. | ||
| public class RazorPagesUnionsTest : LoggedTest | ||
| { | ||
| private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => | ||
| builder.UseStartup<RazorPagesWebSite.Startup>(); | ||
|
|
||
| protected override void Initialize(TestContext context, MethodInfo methodInfo, object[] testMethodArguments, ITestOutputHelper testOutputHelper) | ||
| { | ||
| base.Initialize(context, methodInfo, testMethodArguments, testOutputHelper); | ||
| Factory = new MvcTestFixture<RazorPagesWebSite.Startup>(LoggerFactory).WithWebHostBuilder(ConfigureWebHostBuilder); | ||
| Client = Factory.CreateDefaultClient(); | ||
| } | ||
|
|
||
| public override void Dispose() | ||
| { | ||
| Factory.Dispose(); | ||
| base.Dispose(); | ||
| } | ||
|
|
||
| public WebApplicationFactory<RazorPagesWebSite.Startup> Factory { get; private set; } | ||
| public HttpClient Client { get; private set; } | ||
|
|
||
| [Fact] | ||
| public async Task RazorPage_UnionReturnType_SerializesActiveCase() | ||
| { | ||
| var response = await Client.GetAsync("/Unions"); | ||
|
|
||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal("true", await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("true", "true")] | ||
| [InlineData("false", "false")] | ||
| [InlineData("\"hi\"", "\"hi\"")] | ||
| public async Task RazorPage_UnionFromBody_RoundTripsUnambiguousPrimitiveCases(string payload, string expectedBody) | ||
| { | ||
| var content = new StringContent(payload, Encoding.UTF8, "application/json"); | ||
|
|
||
| var response = await Client.PostAsync("/Unions", content); | ||
|
|
||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
| } |
153 changes: 153 additions & 0 deletions
153
src/Mvc/test/Mvc.FunctionalTests/SystemTextJsonInputFormatterTest.Unions.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,153 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
|
|
||
| namespace Microsoft.AspNetCore.Mvc.FunctionalTests; | ||
|
|
||
| public partial class SystemTextJsonInputFormatterTest | ||
| { | ||
| private static StringContent JsonBody(string json) => new(json, Encoding.UTF8, "application/json"); | ||
|
|
||
| [Theory] | ||
| [InlineData("true", "true")] | ||
| [InlineData("false", "false")] | ||
| [InlineData("\"hi\"", "\"hi\"")] | ||
| public async Task Union_Body_UnambiguousPrimitiveCases_RoundTrip(string payload, string expectedBody) | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoBoolString", JsonBody(payload)); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Union_Body_AmbiguousPrimitiveCase_WithoutClassifier_ReturnsBadRequest() | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoIntString", JsonBody("\"hi\"")); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.BadRequest); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("42", "42")] | ||
| [InlineData("\"hi\"", "\"hi\"")] | ||
| public async Task Union_Body_AmbiguousPrimitiveCase_WithClassifier_RoundTrips(string payload, string expectedBody) | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoIntStringWithClassifier", JsonBody(payload)); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Union_Body_AmbiguousNumericUnion_WithoutClassifier_ReturnsBadRequest() | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoIntShort", JsonBody("42")); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.BadRequest); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Union_Body_AmbiguousNumericUnion_WithClassifier_RoundTrips() | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoIntShortWithClassifier", JsonBody("42")); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal("42", await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Union_Body_UnionWithNullableCase_NumberTokenWorksWithoutClassifier() | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoNullableIntString", JsonBody("42")); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal("42", await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Union_Body_UnionWithNullableCase_StringTokenAmbiguousWithoutClassifier_ReturnsBadRequest() | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoNullableIntString", JsonBody("\"hi\"")); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.BadRequest); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("42", "42")] | ||
| [InlineData("\"hi\"", "\"hi\"")] | ||
| [InlineData("null", "null")] | ||
| public async Task Union_Body_UnionWithNullableCase_WithClassifier_RoundTrips(string payload, string expectedBody) | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoNullableIntStringWithClassifier", JsonBody(payload)); | ||
|
|
||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Union_Body_UnionWithNullableCase_NullPayloadWorksWithoutClassifier() | ||
| { | ||
| // Null tokens are short-circuited in the union converter before the classifier | ||
| // runs, so JSON null round-trips even when no classifier is registered. | ||
| var response = await Client.PostAsync("/Unions/EchoNullableIntString", JsonBody("null")); | ||
|
|
||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal("null", await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("{\"name\":\"Whiskers\"}")] | ||
| [InlineData("{\"breed\":\"Labrador\"}")] | ||
| public async Task Union_Body_ObjectCaseUnion_WithoutClassifier_ReturnsBadRequest(string payload) | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoPet", JsonBody(payload)); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.BadRequest); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("{\"name\":\"Whiskers\"}", "{\"name\":\"Whiskers\"}")] | ||
| [InlineData("{\"breed\":\"Labrador\"}", "{\"breed\":\"Labrador\"}")] | ||
| public async Task Union_Body_ObjectCaseUnion_WithClassifier_RoundTrips(string payload, string expectedBody) | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoPetWithClassifier", JsonBody(payload)); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Union_Body_AsPropertyOfWrappingRecord_NumberPayload_RoundTrips() | ||
| { | ||
| const string payload = "{\"correlationId\":\"abc\",\"payload\":42}"; | ||
| var response = await Client.PostAsync("/Unions/EchoEnvelope", JsonBody(payload)); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(payload, await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("{\"correlationId\":\"abc\",\"payload\":null}")] | ||
| [InlineData("{\"correlationId\":\"abc\"}")] | ||
| public async Task Union_Body_AsPropertyOfWrappingRecord_NullOrMissingPayload_RoundTripsAsNull(string payload) | ||
| { | ||
| var response = await Client.PostAsync("/Unions/EchoEnvelope", JsonBody(payload)); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal("{\"correlationId\":\"abc\",\"payload\":null}", await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Union_Body_ExplicitFromBodyAndImplicitBody_BehaveTheSame() | ||
| { | ||
| var explicitResponse = await Client.PostAsync("/Unions/EchoIntString", JsonBody("42")); | ||
| var implicitResponse = await Client.PostAsync("/Unions/EchoIntStringImplicit", JsonBody("42")); | ||
|
|
||
| await explicitResponse.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| await implicitResponse.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal("42", await explicitResponse.Content.ReadAsStringAsync()); | ||
| Assert.Equal("42", await implicitResponse.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Union_Body_NonJsonContentType_Returns415UnsupportedMediaType() | ||
| { | ||
| var content = new StringContent("42", Encoding.UTF8, "text/plain"); | ||
| var response = await Client.PostAsync("/Unions/EchoIntString", content); | ||
|
|
||
| await response.AssertStatusCodeAsync(HttpStatusCode.UnsupportedMediaType); | ||
| } | ||
| } |
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
103 changes: 103 additions & 0 deletions
103
src/Mvc/test/Mvc.FunctionalTests/SystemTextJsonOutputFormatterTest.Unions.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,103 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
|
|
||
| namespace Microsoft.AspNetCore.Mvc.FunctionalTests; | ||
|
|
||
| public partial class SystemTextJsonOutputFormatterTest | ||
| { | ||
| [Theory] | ||
| [InlineData("PrimitiveByteString", "value", "42")] | ||
| [InlineData("PrimitiveByteString", "string", "\"hi\"")] | ||
| [InlineData("PrimitiveShortString", "value", "1234")] | ||
| [InlineData("PrimitiveShortString", "string", "\"hi\"")] | ||
| [InlineData("PrimitiveIntString", "value", "42")] | ||
| [InlineData("PrimitiveIntString", "string", "\"hi\"")] | ||
| [InlineData("PrimitiveLongString", "value", "9999999999")] | ||
| [InlineData("PrimitiveLongString", "string", "\"hi\"")] | ||
| [InlineData("PrimitiveDecimalString", "value", "3.14")] | ||
| [InlineData("PrimitiveDecimalString", "string", "\"hi\"")] | ||
| [InlineData("PrimitiveDoubleString", "value", "2.5")] | ||
| [InlineData("PrimitiveDoubleString", "string", "\"hi\"")] | ||
| [InlineData("PrimitiveBoolString", "value", "true")] | ||
| [InlineData("PrimitiveBoolString", "string", "\"hi\"")] | ||
| [InlineData("PrimitiveGuidInt", "value", "\"00000000-0000-0000-0000-000000000001\"")] | ||
| [InlineData("PrimitiveGuidInt", "int", "42")] | ||
| [InlineData("PrimitiveDateTimeInt", "value", "\"2024-05-28T10:00:00\"")] | ||
| [InlineData("PrimitiveDateTimeInt", "int", "42")] | ||
| [InlineData("PrimitiveCharInt", "value", "\"x\"")] | ||
| [InlineData("PrimitiveCharInt", "int", "42")] | ||
| public async Task Union_ReturnType_PrimitiveCases_SerializeActiveCase(string action, string kind, string expectedBody) | ||
| { | ||
| var response = await Client.GetAsync($"/Unions/{action}/{kind}"); | ||
|
|
||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("AsyncTask")] | ||
| [InlineData("AsyncValueTask")] | ||
| public async Task Union_ReturnType_TaskAndValueTask_SerializeActiveCase(string action) | ||
| { | ||
| var response = await Client.GetAsync($"/Unions/{action}"); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal("42", await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("value", HttpStatusCode.OK, "42")] | ||
| [InlineData("null", HttpStatusCode.NoContent, "")] | ||
| public async Task Union_ReturnType_NullableWrapper_HandlesValueAndNull(string kind, HttpStatusCode expectedStatus, string expectedBody) | ||
| { | ||
| var response = await Client.GetAsync($"/Unions/NullableWrapper/{kind}"); | ||
| await response.AssertStatusCodeAsync(expectedStatus); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("int", "5")] | ||
| [InlineData("string", "\"hi\"")] | ||
| [InlineData("null", "null")] | ||
| public async Task Union_ReturnType_UnionWithNullableCase_SerializesActiveCase(string kind, string expectedBody) | ||
| { | ||
| var response = await Client.GetAsync($"/Unions/UnionWithNullableCase/{kind}"); | ||
|
|
||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("cat", "{\"name\":\"Whiskers\"}")] | ||
| [InlineData("dog", "{\"breed\":\"Labrador\"}")] | ||
| public async Task Union_ReturnType_ObjectCase_SerializesActiveCase(string kind, string expectedBody) | ||
| { | ||
| var response = await Client.GetAsync($"/Unions/ObjectCase/{kind}"); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("int", "42")] | ||
| [InlineData("string", "\"nested\"")] | ||
| [InlineData("bool", "true")] | ||
| public async Task Union_ReturnType_NestedUnion_SerializesInnermostCase(string kind, string expectedBody) | ||
| { | ||
| var response = await Client.GetAsync($"/Unions/Nested/{kind}"); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("int", "{\"correlationId\":\"abc\",\"payload\":42}")] | ||
| [InlineData("string", "{\"correlationId\":\"abc\",\"payload\":\"hi\"}")] | ||
| public async Task Union_ReturnType_AsPropertyOfWrappingRecord_SerializesActiveCase(string kind, string expectedBody) | ||
| { | ||
| var response = await Client.GetAsync($"/Unions/Envelope/{kind}"); | ||
| await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
| Assert.Equal(expectedBody, await response.Content.ReadAsStringAsync()); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.