Skip to content

Commit 26c1c94

Browse files
LuukN2commonsensesoftware
authored andcommitted
Add support for generating types for action parameters that have collection parameters.
1 parent 3091cb2 commit 26c1c94

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

src/Common.OData.ApiExplorer/AspNet.OData/ClassProperty.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ internal ClassProperty( IEnumerable<Assembly> assemblies, IEdmOperationParameter
3030
Contract.Requires( parameter != null );
3131

3232
Name = parameter.Name;
33-
type = parameter.Type.Definition.GetClrType( assemblies );
33+
34+
if ( parameter.Type.IsCollection() )
35+
{
36+
var collectionType = parameter.Type.AsCollection();
37+
var elementType = collectionType.ElementType().Definition.GetClrType( assemblies );
38+
type = typeof( IEnumerable<> ).MakeGenericType( elementType );
39+
}
40+
else
41+
{
42+
type = parameter.Type.Definition.GetClrType( assemblies );
43+
}
44+
3445
Attributes = AttributesFromOperationParameter( parameter );
3546
}
3647

test/Microsoft.AspNet.OData.Versioning.ApiExplorer.Tests/AspNet.OData/DefaultModelTypeBuilderTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,33 @@ public void substitute_should_generate_type_for_action_parameters()
242242
substitutionType.Should().HaveProperty<bool>( "callbackRequired" );
243243
}
244244

245+
[Fact]
246+
public void substitute_should_generate_type_for_action_parameters_with_collection_parameters()
247+
{
248+
// arrange
249+
var modelBuilder = new ODataConventionModelBuilder();
250+
var contact = modelBuilder.EntitySet<Contact>( "Contacts" ).EntityType;
251+
var action = contact.Action( "PlanMeeting" );
252+
253+
action.Parameter<DateTime>( "when" );
254+
action.CollectionParameter<Contact>( "attendees" );
255+
action.CollectionParameter<string>( "topics" );
256+
257+
var context = NewContext( modelBuilder.GetEdmModel() );
258+
var model = context.Model;
259+
var qualifiedName = $"{model.EntityContainer.Namespace}.{action.Name}";
260+
var operation = (IEdmAction) model.FindDeclaredOperations( qualifiedName ).Single();
261+
262+
// act
263+
var substitutionType = context.ModelTypeBuilder.NewActionParameters( operation, ApiVersion.Default );
264+
265+
// assert
266+
substitutionType.GetRuntimeProperties().Should().HaveCount( 3 );
267+
substitutionType.Should().HaveProperty<DateTimeOffset>( "when" );
268+
substitutionType.Should().HaveProperty<IEnumerable<Contact>>( "attendees" );
269+
substitutionType.Should().HaveProperty<IEnumerable<string>>( "topics" );
270+
}
271+
245272
public static IEnumerable<object[]> SubstitutionNotRequiredData
246273
{
247274
get

test/Microsoft.AspNet.OData.Versioning.ApiExplorer.Tests/AspNet.OData/Employer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
41
namespace Microsoft.AspNet.OData
52
{
3+
using System;
4+
using System.Collections.Generic;
5+
66
public class Employer
77
{
88
public int EmployerId { get; set; }

test/Microsoft.AspNetCore.OData.Versioning.ApiExplorer.Tests/AspNet.OData/DefaultModelTypeBuilderTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,33 @@ public void substitute_should_generate_type_for_action_parameters()
242242
substitutionType.Should().HaveProperty<bool>( "callbackRequired" );
243243
}
244244

245+
[Fact]
246+
public void substitute_should_generate_type_for_action_parameters_with_collection_parameters()
247+
{
248+
// arrange
249+
var modelBuilder = new ODataConventionModelBuilder();
250+
var contact = modelBuilder.EntitySet<Contact>( "Contacts" ).EntityType;
251+
var action = contact.Action( "PlanMeeting" );
252+
253+
action.Parameter<DateTime>( "when" );
254+
action.CollectionParameter<Contact>( "attendees" );
255+
action.CollectionParameter<string>( "topics" );
256+
257+
var context = NewContext( modelBuilder.GetEdmModel() );
258+
var model = context.Model;
259+
var qualifiedName = $"{model.EntityContainer.Namespace}.{action.Name}";
260+
var operation = (IEdmAction) model.FindDeclaredOperations( qualifiedName ).Single();
261+
262+
// act
263+
var substitutionType = context.ModelTypeBuilder.NewActionParameters( operation, ApiVersion.Default );
264+
265+
// assert
266+
substitutionType.GetRuntimeProperties().Should().HaveCount( 3 );
267+
substitutionType.Should().HaveProperty<DateTimeOffset>( "when" );
268+
substitutionType.Should().HaveProperty<IEnumerable<Contact>>( "attendees" );
269+
substitutionType.Should().HaveProperty<IEnumerable<string>>( "topics" );
270+
}
271+
245272
public static IEnumerable<object[]> SubstitutionNotRequiredData
246273
{
247274
get

test/Microsoft.AspNetCore.OData.Versioning.ApiExplorer.Tests/AspNet.OData/Employer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
41
namespace Microsoft.AspNet.OData
52
{
3+
using System;
4+
using System.Collections.Generic;
5+
66
public class Employer
77
{
88
public int EmployerId { get; set; }

0 commit comments

Comments
 (0)