Skip to content

Commit 01e1f0e

Browse files
committed
fix(WebApplicationDemo): workaround api-versioning bug on substitute types setters
see: dotnet/aspnet-api-versioning#1104
1 parent 031ac4b commit 01e1f0e

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
lines changed

Samples/WebApplicationDemo/Configuration/PersonModelConfiguration.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Asp.Versioning.OData;
22
using Asp.Versioning;
33
using Microsoft.OData.ModelBuilder;
4+
45
using WebApplicationDemo.Dto;
56

67
namespace WebApplicationDemo.Configuration
@@ -9,14 +10,15 @@ public class PersonModelConfiguration : IModelConfiguration
910
{
1011
public void Apply(ODataModelBuilder builder, ApiVersion apiVersion, string? routePrefix)
1112
{
12-
var person = builder.EntitySet<Person>("People").EntityType;
13-
14-
person.HasKey(p => p.Id);
15-
person.Select().OrderBy("firstName", "lastName").Filter();
16-
1713
if(apiVersion < ApiVersions.V2)
1814
{
19-
person.Ignore(p => p.Phone);
15+
var person = builder.EntitySet<Person.V1>("People").EntityType;
16+
person.HasKey(p => p.Id);
17+
person.Select().OrderBy("firstName", "lastName").Filter();
18+
} else {
19+
var person = builder.EntitySet<Person.V2>("People").EntityType;
20+
person.HasKey(p => p.Id);
21+
person.Select().OrderBy("firstName", "lastName").Filter();
2022
}
2123
}
2224
}

Samples/WebApplicationDemo/Controllers/V1/PeopleController.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace WebApplicationDemo.Controllers.V1
1717
[ApiVersion(1.0)]
1818
public class PeopleController : ODataController
1919
{
20-
private static List<Person> _people = new List<Person>()
20+
private static List<Person.V1> _people = new ()
2121
{
2222
new()
2323
{
@@ -44,14 +44,14 @@ public class PeopleController : ODataController
4444

4545
[HttpGet]
4646
[EnableQuery(AllowedQueryOptions = All)]
47-
public IQueryable<Person> Get()
47+
public IQueryable<Person.V1> Get()
4848
{
4949
return _people.AsQueryable();
5050
}
5151

5252
[HttpGet]
5353
[EnableQuery]
54-
public SingleResult<Person> Get(int key)
54+
public SingleResult<Person.V1> Get(int key)
5555
{
5656
return SingleResult.Create(_people.Where(p => p.Id == key).AsQueryable());
5757
}

Samples/WebApplicationDemo/Controllers/V2/PeopleController.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace WebApplicationDemo.Controllers.V2
1717
[ApiVersion(2.0)]
1818
public class PeopleController : ODataController
1919
{
20-
private static List<Person> _people = new List<Person>()
20+
private static List<Person.V2> _people = new()
2121
{
2222
new()
2323
{
@@ -44,15 +44,15 @@ public class PeopleController : ODataController
4444

4545
[HttpGet]
4646
[EnableQuery(AllowedQueryOptions = All)]
47-
public IQueryable<Person> Get()
47+
public IQueryable<Person.V2> Get()
4848
{
4949
return _people.AsQueryable();
5050
}
5151

5252

5353
[HttpGet]
5454
[EnableQuery]
55-
public SingleResult<Person> Get(int key)
55+
public SingleResult<Person.V2> Get(int key)
5656
{
5757
return SingleResult.Create(_people.Where(p => p.Id == key).AsQueryable());
5858
}

Samples/WebApplicationDemo/Dto/Person.cs

+44-30
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,57 @@
22

33
namespace WebApplicationDemo.Dto
44
{
5-
/// <summary>
6-
/// Represents a person.
7-
/// </summary>
8-
public class Person
9-
{
5+
public static class Person {
6+
107
/// <summary>
11-
/// Gets or sets the unique identifier for a person.
8+
/// Represents a person.
129
/// </summary>
13-
/// <value>The person's unique identifier.</value>
14-
public int Id { get; set; }
10+
public class V1
11+
{
12+
/// <summary>
13+
/// Gets or sets the unique identifier for a person.
14+
/// </summary>
15+
/// <value>The person's unique identifier.</value>
16+
public int Id { get; set; }
1517

16-
/// <summary>
17-
/// Gets or sets the first name of a person.
18-
/// </summary>
19-
/// <value>The person's first name.</value>
20-
[Required]
21-
[StringLength(25)]
22-
public string? FirstName { get; set; }
18+
/// <summary>
19+
/// Gets or sets the first name of a person.
20+
/// </summary>
21+
/// <value>The person's first name.</value>
22+
[Required]
23+
[StringLength(25)]
24+
public string? FirstName { get; set; }
2325

24-
/// <summary>
25-
/// Gets or sets the last name of a person.
26-
/// </summary>
27-
/// <value>The person's last name.</value>
28-
[Required]
29-
[StringLength(25)]
30-
public string? LastName { get; set; }
26+
/// <summary>
27+
/// Gets or sets the last name of a person.
28+
/// </summary>
29+
/// <value>The person's last name.</value>
30+
[Required]
31+
[StringLength(25)]
32+
public string? LastName { get; set; }
3133

32-
/// <summary>
33-
/// Gets or sets the email address for a person.
34-
/// </summary>
35-
/// <value>The person's email address.</value>
36-
public string? Email { get; set; }
34+
/// <summary>
35+
/// Gets or sets the email address for a person.
36+
/// </summary>
37+
/// <value>The person's email address.</value>
38+
public string? Email { get; set; }
39+
40+
}
3741

42+
3843
/// <summary>
39-
/// Gets or sets the telephone number for a person.
44+
/// Represents a person.
4045
/// </summary>
41-
/// <value>The person's telephone number.</value>
42-
public string? Phone { get; set; }
46+
public class V2 : V1
47+
{
48+
49+
/// <summary>
50+
/// Gets or sets the telephone number for a person.
51+
/// </summary>
52+
/// <value>The person's telephone number.</value>
53+
public string? Phone { get; set; }
54+
}
55+
4356
}
57+
4458
}

0 commit comments

Comments
 (0)