Skip to content

Ensure @model types have a mapping in razor pages #11844

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
merged 7 commits into from
May 15, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ @model Type2
var documentNode = processor.GetDocumentNode();

// Act
var result = ModelDirective.GetModelType(documentNode);
var result = ModelDirective.GetModelType(documentNode).Content;

// Assert
Assert.Equal("Type1", result);
Expand All @@ -61,7 +61,7 @@ public void ModelDirective_GetModelType_DefaultsToDynamic()
var documentNode = processor.GetDocumentNode();

// Act
var result = ModelDirective.GetModelType(documentNode);
var result = ModelDirective.GetModelType(documentNode).Content;

// Assert
Assert.Equal("dynamic", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ @model Type2
var documentNode = processor.GetDocumentNode();

// Act
var result = ModelDirective.GetModelType(documentNode);
var result = ModelDirective.GetModelType(documentNode).Content;

// Assert
Assert.Equal("Type1", result);
Expand All @@ -61,7 +61,7 @@ public void ModelDirective_GetModelType_DefaultsToDynamic()
var documentNode = processor.GetDocumentNode();

// Act
var result = ModelDirective.GetModelType(documentNode);
var result = ModelDirective.GetModelType(documentNode).Content;

// Assert
Assert.Equal("dynamic", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ public void RazorPagesWithRouteTemplate_Runtime()
// Assert
AssertDocumentNodeMatchesBaseline(compiled.CodeDocument.GetDocumentIntermediateNode());
AssertCSharpDocumentMatchesBaseline(compiled.CodeDocument.GetCSharpDocument());
AssertSourceMappingsMatchBaseline(compiled.CodeDocument);
AssertLinePragmas(compiled.CodeDocument, designTime: false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ @model Type2
var documentNode = processor.GetDocumentNode();

// Act
var result = ModelDirective.GetModelType(documentNode);
var result = ModelDirective.GetModelType(documentNode).Content;

// Assert
Assert.Equal("Type1", result);
Expand All @@ -58,7 +58,7 @@ public void ModelDirective_GetModelType_DefaultsToDynamic()
var documentNode = processor.GetDocumentNode();

// Act
var result = ModelDirective.GetModelType(documentNode);
var result = ModelDirective.GetModelType(documentNode).Content;

// Assert
Assert.Equal("dynamic", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_I
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives Model => ViewData.Model!;
#nullable disable
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives Model => ViewData.Model;
Copy link
Member

Choose a reason for hiding this comment

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

This looks suspicious. Previously the Model property here was nullable-enabled, now it's nullable-disabled, isn't that a regression?

Copy link
Member Author

Choose a reason for hiding this comment

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

The Model property in this case isn't coming from the user, if you look it's the type of name of the file which is generated code, declared outside of the users nullability annotations. So, it's correct that it shouldn't be nullable annotated.

}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,4 @@
Inject -
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives>)PageContext?.ViewData!;\n#nullable disable
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives Model => ViewData.Model!;\n#nullable disable
PropertyDeclaration - - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_IncompleteDirectives - Model - ViewData.Model
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_t
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test Model => ViewData.Model!;
#nullable disable
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test Model => ViewData.Model;
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@
Inject -
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test>)PageContext?.ViewData!;\n#nullable disable
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test Model => ViewData.Model!;\n#nullable disable
PropertyDeclaration - - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test - Model - ViewData.Model
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,15 @@ internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_I
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<MyModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<MyModel>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public MyModel Model => ViewData.Model!;
public
#nullable restore
#line (2,8)-(2,15) "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/InheritsWithViewImports.cshtml"
MyModel

#line default
#line hidden
#nullable disable
Model => ViewData.Model;
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@
Inject -
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<MyModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<MyModel>)PageContext?.ViewData!;\n#nullable disable
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic MyModel Model => ViewData.Model!;\n#nullable disable
PropertyDeclaration - - MyModel - Model - ViewData.Model
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_M
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective Model => ViewData.Model!;
#nullable disable
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective Model => ViewData.Model;
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@
Inject -
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective>)PageContext?.ViewData!;\n#nullable disable
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective Model => ViewData.Model!;\n#nullable disable
PropertyDeclaration - - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_MalformedPageDirective - Model - ViewData.Model
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_P
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace Model => ViewData.Model!;
#nullable disable
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace Model => ViewData.Model;
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@
Inject -
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace>)PageContext?.ViewData!;\n#nullable disable
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace Model => ViewData.Model!;\n#nullable disable
PropertyDeclaration - - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_PageWithNamespace - Model - ViewData.Model
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_R
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPageWithNoLeadingPageDirective> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPageWithNoLeadingPageDirective>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPageWithNoLeadingPageDirective Model => ViewData.Model!;
#nullable disable
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPageWithNoLeadingPageDirective Model => ViewData.Model;
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@
Inject -
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPageWithNoLeadingPageDirective> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPageWithNoLeadingPageDirective>)PageContext?.ViewData!;\n#nullable disable
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPageWithNoLeadingPageDirective Model => ViewData.Model!;\n#nullable disable
PropertyDeclaration - - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPageWithNoLeadingPageDirective - Model - ViewData.Model
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_t
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test Model => ViewData.Model!;
#nullable disable
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test Model => ViewData.Model;
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@
Inject -
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test>)PageContext?.ViewData!;\n#nullable disable
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test Model => ViewData.Model!;\n#nullable disable
PropertyDeclaration - - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_test - Model - ViewData.Model
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_t
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestModel>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public TestModel Model => ViewData.Model!;
public
#nullable restore
#line (3,8)-(3,17) "TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml"
TestModel

#line default
#line hidden
#nullable disable
Model => ViewData.Model;
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@
Inject -
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestModel>)PageContext?.ViewData!;\n#nullable disable
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic TestModel Model => ViewData.Model!;\n#nullable disable
PropertyDeclaration - - TestModel - Model - ViewData.Model
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ Source Location: (78:6,5 [14] TestFiles\IntegrationTests\CodeGenerationIntegrati
Generated Location: (2328:46,0 [14] )
|Model?.Address|

Source Location: (36:2,7 [9] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml)
|TestModel|
Generated Location: (4123:81,0 [9] )
|TestModel|

Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ internal sealed class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_t
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestModel?> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestModel?>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public TestModel? Model => ViewData.Model!;
public
#nullable restore
#line (3,8)-(3,18) "TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml"
TestModel?

#line default
#line hidden
#nullable disable
Model => ViewData.Model;
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@
Inject -
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestModel?> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestModel?>)PageContext?.ViewData!;\n#nullable disable
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic TestModel? Model => ViewData.Model!;\n#nullable disable
PropertyDeclaration - - TestModel? - Model - ViewData.Model
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ Source Location: (79:6,5 [14] TestFiles\IntegrationTests\CodeGenerationIntegrati
Generated Location: (2328:46,0 [14] )
|Model?.Address|

Source Location: (36:2,7 [10] TestFiles\IntegrationTests\CodeGenerationIntegrationTest\test.cshtml)
|TestModel?|
Generated Location: (4126:81,0 [10] )
|TestModel?|

Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,15 @@ public class NewModel : PageModel
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<NewModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<NewModel>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public NewModel Model => ViewData.Model!;
public
#nullable restore
#line (3,8)-(3,16) "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPagesWithRouteTemplate.cshtml"
NewModel

#line default
#line hidden
#nullable disable
Model => ViewData.Model;
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@
Inject -
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<NewModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<NewModel>)PageContext?.ViewData!;\n#nullable disable
CSharpCode -
IntermediateToken - - CSharp - #nullable restore\npublic NewModel Model => ViewData.Model!;\n#nullable disable
PropertyDeclaration - - NewModel - Model - ViewData.Model
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Source Location: (36:3,1 [41] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPagesWithRouteTemplate.cshtml)
|using Microsoft.AspNetCore.Mvc.RazorPages|
Generated Location: (1098:16,0 [41] )
|using Microsoft.AspNetCore.Mvc.RazorPages|

Source Location: (6:0,6 [8] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPagesWithRouteTemplate.cshtml)
|"/About"|
Generated Location: (1464:26,0 [8] )
|"/About"|

Source Location: (6:0,6 [8] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPagesWithRouteTemplate.cshtml)
|"/About"|
Generated Location: (1464:26,0 [8] )
|"/About"|

Source Location: (213:12,18 [10] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPagesWithRouteTemplate.cshtml)
|Model.Name|
Generated Location: (2690:48,0 [10] )
|Model.Name|

Source Location: (93:5,12 [97] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPagesWithRouteTemplate.cshtml)
|
public class NewModel : PageModel
{
public string Name { get; set; }
}
|
Generated Location: (2991:59,0 [97] )
|
public class NewModel : PageModel
{
public string Name { get; set; }
}
|

Source Location: (25:2,7 [8] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorPagesWithRouteTemplate.cshtml)
|NewModel|
Generated Location: (4788:95,0 [8] )
|NewModel|

Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ public class Customer
#nullable restore
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPagesWithoutModel> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPagesWithoutModel>)PageContext?.ViewData!;
#nullable disable
#nullable restore
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPagesWithoutModel Model => ViewData.Model!;
#nullable disable
public TestFiles_IntegrationTests_CodeGenerationIntegrationTest_RazorPagesWithoutModel Model => ViewData.Model;
}
}
#pragma warning restore 1591
Loading
Loading