Skip to content

Commit

Permalink
fix(ApiExplorer): SubstitutedType have invalid property setter
Browse files Browse the repository at this point in the history
The PropertySetter of the SubstitutedType have 0 parameters: they had the same signature of the Getter.

resolves: #1104
  • Loading branch information
AndreaCuneo committed Aug 28, 2024
1 parent 3fc0719 commit 41f8cda
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private static PropertyBuilder AddProperty(
var field = addTo.DefineField( "_" + name, shouldBeAdded, FieldAttributes.Private );
var propertyBuilder = addTo.DefineProperty( name, PropertyAttributes.HasDefault, shouldBeAdded, null );
var getter = addTo.DefineMethod( "get_" + name, propertyMethodAttributes, shouldBeAdded, Type.EmptyTypes );
var setter = addTo.DefineMethod( "set_" + name, propertyMethodAttributes, shouldBeAdded, Type.EmptyTypes );
var setter = addTo.DefineMethod( "set_" + name, propertyMethodAttributes, null, [shouldBeAdded] );
var il = getter.GetILGenerator();

il.Emit( OpCodes.Ldarg_0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,41 @@ public void substitute_should_resolve_types_that_reference_a_model_that_match_th
substitutionType.Should().NotBeOfType<TypeBuilder>();
}

[Fact]
public void substituted_type_should_have_valid_runtime_properties__issue1104()
{
// arrange
var modelBuilder = new ODataConventionModelBuilder();

var address = modelBuilder.EntitySet<Address>( nameof( Address ) ).EntityType;
address.Ignore( x => x.City ); // force substitution
var addressType = typeof( Address );

var context = NewContext( modelBuilder.GetEdmModel() );

// act
var substitutedType = addressType.SubstituteIfNecessary( context );

// assert
substitutedType.Should().NotBe( addressType );
substitutedType.GetRuntimeProperties().Should().HaveCount( 5 )
#if NET452
;
foreach ( var substitutedProperty in substitutedType.GetRuntimeProperties() )
{
substitutedProperty.Should().NotBeNull();
substitutedProperty.GetSetMethod( true ).Should().NotBeNull()
.And.Match( p => p.ReturnType == typeof( void ) )
.And.Match( p => p.GetParameters().Length == 1 );
}
#else
.And.AllSatisfy(prop => prop.GetSetMethod(true).Should()
.NotBeNull()
.And.ReturnVoid()
.And.Match(setter => setter.GetParameters().Length == 1));
#endif
}

public static IEnumerable<object[]> SubstitutionNotRequiredData
{
get
Expand Down

0 comments on commit 41f8cda

Please sign in to comment.