File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed
src/ModelBinding/Metadata
test/ModelBinding/Metadata Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -225,6 +225,14 @@ public static ModelAttributes GetAttributesForParameter(ParameterInfo parameterI
225225 // To avoid this, we call `GetCustomAttributes` directly
226226 // to avoid examining the inheritance hierarchy.
227227 // See https://source.dot.net/#System.Private.CoreLib/src/System/Attribute.CoreCLR.cs,677
228- return type . GetCustomAttributes < ModelMetadataTypeAttribute > ( inherit : false ) . SingleOrDefault ( ) ? . MetadataType ;
228+ var modelMetadataTypeAttributes = type . GetCustomAttributes < ModelMetadataTypeAttribute > ( inherit : false ) ;
229+ try
230+ {
231+ return modelMetadataTypeAttributes ? . SingleOrDefault ( ) ? . MetadataType ;
232+ }
233+ catch ( InvalidOperationException e )
234+ {
235+ throw new InvalidOperationException ( "Only one ModelMetadataType attribute is permitted per type." , e ) ;
236+ }
229237 }
230238}
Original file line number Diff line number Diff line change @@ -287,6 +287,18 @@ public void GetAttributesForProperty_WithModelType_IncludesTypeAttributes()
287287 attribute => Assert . IsType < ClassValidator > ( attribute ) ) ;
288288 }
289289
290+ [ Fact ]
291+ public void GetAttributeForProperty_WithModelType_HandlesMultipleAttributesOnType ( )
292+ {
293+ // Arrange
294+ var modelType = typeof ( InvalidBaseViewModel ) ;
295+ var property = modelType . GetRuntimeProperties ( ) . FirstOrDefault ( p => p . Name == nameof ( BaseModel . RouteValue ) ) ;
296+
297+ // Assert
298+ var exception = Assert . Throws < InvalidOperationException > ( ( ) => ModelAttributes . GetAttributesForProperty ( modelType , property ) ) ;
299+ Assert . Equal ( "Only one ModelMetadataType attribute is permitted per type." , exception . Message ) ;
300+ }
301+
290302 [ ClassValidator ]
291303 private class BaseModel
292304 {
@@ -335,6 +347,10 @@ private class BaseViewModel
335347 public string RouteValue { get ; set ; }
336348 }
337349
350+ [ ModelMetadataType < BaseModel > ]
351+ [ ModelMetadataType ( typeof ( BaseModel ) ) ]
352+ private class InvalidBaseViewModel : BaseViewModel { }
353+
338354 [ ModelMetadataType < DerivedModel > ]
339355 private class DerivedViewModel : BaseViewModel
340356 {
You can’t perform that action at this time.
0 commit comments