Skip to content

Commit

Permalink
fix(WebApplicationDemo): workaround api-versioning bug on substitute …
Browse files Browse the repository at this point in the history
…types setters

see: dotnet/aspnet-api-versioning#1104
  • Loading branch information
AndreaCuneo committed Sep 11, 2024
1 parent 031ac4b commit 01e1f0e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Asp.Versioning.OData;
using Asp.Versioning;
using Microsoft.OData.ModelBuilder;

using WebApplicationDemo.Dto;

namespace WebApplicationDemo.Configuration
Expand All @@ -9,14 +10,15 @@ public class PersonModelConfiguration : IModelConfiguration
{
public void Apply(ODataModelBuilder builder, ApiVersion apiVersion, string? routePrefix)
{
var person = builder.EntitySet<Person>("People").EntityType;

person.HasKey(p => p.Id);
person.Select().OrderBy("firstName", "lastName").Filter();

if(apiVersion < ApiVersions.V2)
{
person.Ignore(p => p.Phone);
var person = builder.EntitySet<Person.V1>("People").EntityType;
person.HasKey(p => p.Id);
person.Select().OrderBy("firstName", "lastName").Filter();
} else {
var person = builder.EntitySet<Person.V2>("People").EntityType;
person.HasKey(p => p.Id);
person.Select().OrderBy("firstName", "lastName").Filter();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Samples/WebApplicationDemo/Controllers/V1/PeopleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace WebApplicationDemo.Controllers.V1
[ApiVersion(1.0)]
public class PeopleController : ODataController
{
private static List<Person> _people = new List<Person>()
private static List<Person.V1> _people = new ()
{
new()
{
Expand All @@ -44,14 +44,14 @@ public class PeopleController : ODataController

[HttpGet]
[EnableQuery(AllowedQueryOptions = All)]
public IQueryable<Person> Get()
public IQueryable<Person.V1> Get()
{
return _people.AsQueryable();
}

[HttpGet]
[EnableQuery]
public SingleResult<Person> Get(int key)
public SingleResult<Person.V1> Get(int key)
{
return SingleResult.Create(_people.Where(p => p.Id == key).AsQueryable());
}
Expand Down
6 changes: 3 additions & 3 deletions Samples/WebApplicationDemo/Controllers/V2/PeopleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace WebApplicationDemo.Controllers.V2
[ApiVersion(2.0)]
public class PeopleController : ODataController
{
private static List<Person> _people = new List<Person>()
private static List<Person.V2> _people = new()
{
new()
{
Expand All @@ -44,15 +44,15 @@ public class PeopleController : ODataController

[HttpGet]
[EnableQuery(AllowedQueryOptions = All)]
public IQueryable<Person> Get()
public IQueryable<Person.V2> Get()
{
return _people.AsQueryable();
}


[HttpGet]
[EnableQuery]
public SingleResult<Person> Get(int key)
public SingleResult<Person.V2> Get(int key)
{
return SingleResult.Create(_people.Where(p => p.Id == key).AsQueryable());
}
Expand Down
74 changes: 44 additions & 30 deletions Samples/WebApplicationDemo/Dto/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,57 @@

namespace WebApplicationDemo.Dto
{
/// <summary>
/// Represents a person.
/// </summary>
public class Person
{
public static class Person {

/// <summary>
/// Gets or sets the unique identifier for a person.
/// Represents a person.
/// </summary>
/// <value>The person's unique identifier.</value>
public int Id { get; set; }
public class V1
{
/// <summary>
/// Gets or sets the unique identifier for a person.
/// </summary>
/// <value>The person's unique identifier.</value>
public int Id { get; set; }

/// <summary>
/// Gets or sets the first name of a person.
/// </summary>
/// <value>The person's first name.</value>
[Required]
[StringLength(25)]
public string? FirstName { get; set; }
/// <summary>
/// Gets or sets the first name of a person.
/// </summary>
/// <value>The person's first name.</value>
[Required]
[StringLength(25)]
public string? FirstName { get; set; }

/// <summary>
/// Gets or sets the last name of a person.
/// </summary>
/// <value>The person's last name.</value>
[Required]
[StringLength(25)]
public string? LastName { get; set; }
/// <summary>
/// Gets or sets the last name of a person.
/// </summary>
/// <value>The person's last name.</value>
[Required]
[StringLength(25)]
public string? LastName { get; set; }

/// <summary>
/// Gets or sets the email address for a person.
/// </summary>
/// <value>The person's email address.</value>
public string? Email { get; set; }
/// <summary>
/// Gets or sets the email address for a person.
/// </summary>
/// <value>The person's email address.</value>
public string? Email { get; set; }

}


/// <summary>
/// Gets or sets the telephone number for a person.
/// Represents a person.
/// </summary>
/// <value>The person's telephone number.</value>
public string? Phone { get; set; }
public class V2 : V1
{

/// <summary>
/// Gets or sets the telephone number for a person.
/// </summary>
/// <value>The person's telephone number.</value>
public string? Phone { get; set; }
}

}

}

0 comments on commit 01e1f0e

Please sign in to comment.