Skip to content

OData 5.0 #669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
16 changes: 15 additions & 1 deletion ApiVersioning.sln
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Common.OData.ApiExplorer",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.OData.Versioning.ApiExplorer.Tests", "test\Microsoft.AspNetCore.OData.Versioning.ApiExplorer.Tests\Microsoft.AspNetCore.OData.Versioning.ApiExplorer.Tests.csproj", "{23BC896B-A4CC-4C82-B98B-CE71239C2EB8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConventionsODataSample", "samples\aspnetcore\ConventionsODataSample\ConventionsODataSample.csproj", "{992B6D9F-F007-441A-9ED9-6A0669993A70}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdvancedODataSample", "samples\aspnetcore\AdvancedODataSample\AdvancedODataSample.csproj", "{DDC53D03-C461-4477-84E2-4C31DD3C6B13}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\Common.OData.ApiExplorer\Common.OData.ApiExplorer.projitems*{0d6519ae-20d2-4c98-97aa-ed3622043936}*SharedItemsImports = 5
Expand Down Expand Up @@ -306,6 +310,14 @@ Global
{23BC896B-A4CC-4C82-B98B-CE71239C2EB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23BC896B-A4CC-4C82-B98B-CE71239C2EB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23BC896B-A4CC-4C82-B98B-CE71239C2EB8}.Release|Any CPU.Build.0 = Release|Any CPU
{992B6D9F-F007-441A-9ED9-6A0669993A70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{992B6D9F-F007-441A-9ED9-6A0669993A70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{992B6D9F-F007-441A-9ED9-6A0669993A70}.Release|Any CPU.ActiveCfg = Release|Any CPU
{992B6D9F-F007-441A-9ED9-6A0669993A70}.Release|Any CPU.Build.0 = Release|Any CPU
{DDC53D03-C461-4477-84E2-4C31DD3C6B13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DDC53D03-C461-4477-84E2-4C31DD3C6B13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDC53D03-C461-4477-84E2-4C31DD3C6B13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDC53D03-C461-4477-84E2-4C31DD3C6B13}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -353,8 +365,10 @@ Global
{0D6519AE-20D2-4C98-97AA-ED3622043936} = {4D5F5F21-0CB7-4B4E-A42F-732BD4AFD0FF}
{C0C766F3-A2D6-461E-ADFF-27496600EA9C} = {4D5F5F21-0CB7-4B4E-A42F-732BD4AFD0FF}
{23BC896B-A4CC-4C82-B98B-CE71239C2EB8} = {0987757E-4D09-4523-B9C9-65B1E8832AA1}
{992B6D9F-F007-441A-9ED9-6A0669993A70} = {900DD210-8500-4D89-A05D-C9526935A719}
{DDC53D03-C461-4477-84E2-4C31DD3C6B13} = {900DD210-8500-4D89-A05D-C9526935A719}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5A38B7FA-17BC-4D3C-977F-7379653DC67C}
EndGlobalSection
EndGlobal
EndGlobal
12 changes: 12 additions & 0 deletions samples/aspnetcore/AdvancedODataSample/AdvancedODataSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Microsoft.Examples</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.OData.Versioning\Microsoft.AspNetCore.OData.Versioning.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Microsoft.Examples.Configuration
{
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Examples.Models;

public class OrderModelConfiguration : IModelConfiguration
{
private static readonly ApiVersion V2 = new ApiVersion( 2, 0 );

private EntityTypeConfiguration<Order> ConfigureCurrent( ODataModelBuilder builder )
{
var order = builder.EntitySet<Order>( "Orders" ).EntityType;

order.HasKey( p => p.Id );

return order;
}

public void Apply( ODataModelBuilder builder, ApiVersion apiVersion, string routePrefix )
{
// note: the EDM for orders is only available in version 2.0
if ( apiVersion == V2 )
{
ConfigureCurrent( builder );
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Microsoft.Examples.Configuration
{
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Examples.Models;

public class PersonModelConfiguration : IModelConfiguration
{
private void ConfigureV1( ODataModelBuilder builder )
{
var person = ConfigureCurrent( builder );
person.Ignore( p => p.Email );
person.Ignore( p => p.Phone );
}

private void ConfigureV2( ODataModelBuilder builder ) => ConfigureCurrent( builder ).Ignore( p => p.Phone );

private EntityTypeConfiguration<Person> ConfigureCurrent( ODataModelBuilder builder )
{
var person = builder.EntitySet<Person>( "People" ).EntityType;

person.HasKey( p => p.Id );

return person;
}

public void Apply( ODataModelBuilder builder, ApiVersion apiVersion, string routePrefix )
{
switch ( apiVersion.MajorVersion )
{
case 1:
ConfigureV1( builder );
break;
case 2:
ConfigureV2( builder );
break;
default:
ConfigureCurrent( builder );
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Microsoft.Examples.Controllers
{
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Query;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Examples.Models;

[ApiVersion( "2.0" )]
[ControllerName( "Orders" )]
public class Orders2Controller : ODataController
{
// GET ~/api/orders?api-version=2.0
[HttpGet]
public IActionResult Get( ODataQueryOptions<Order> options, ApiVersion version ) =>
Ok( new[] { new Order() { Id = 1, Customer = $"Customer v{version}" } } );

// GET ~/api/orders/{id}?api-version=2.0
[HttpGet( "{id}" )]
public IActionResult Get( int id, ODataQueryOptions<Order> options, ApiVersion version ) =>
Ok( new Order() { Id = id, Customer = $"Customer v{version}" } );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Microsoft.Examples.Controllers
{
using Microsoft.AspNetCore.Mvc;
using Microsoft.Examples.Models;

[ApiController]
[ApiVersion( "3.0" )]
[ControllerName( "Orders" )]
[Route( "api/orders" )]
public class Orders3Controller : ControllerBase
{
// GET ~/api/orders?api-version=3.0
[HttpGet]
public IActionResult Get( ApiVersion version ) => Ok( new[] { new Order() { Id = 1, Customer = $"Customer v{version}" } } );

// GET ~/api/orders/{id}?api-version=3.0
[HttpGet( "{id}" )]
public IActionResult Get( int id, ApiVersion version ) => Ok( new Order() { Id = id, Customer = $"Customer v{version}" } );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Microsoft.Examples.Controllers
{
using Microsoft.AspNetCore.Mvc;
using Microsoft.Examples.Models;

// note: since the application is configured with AssumeDefaultVersionWhenUnspecified, this controller
// is implicitly versioned to the DefaultApiVersion, which has the default value 1.0.
[ApiController]
[Route( "api/orders" )]
public class OrdersController : ControllerBase
{
// GET ~/api/orders
// GET ~/api/orders?api-version=1.0
[HttpGet]
public IActionResult Get( ApiVersion version ) => Ok( new[] { new Order() { Id = 1, Customer = $"Customer v{version}" } } );

// GET ~/api/orders/{id}
// GET ~/api/orders/{id}?api-version=1.0
[HttpGet( "{id}" )]
public IActionResult Get( int id, ApiVersion version ) => Ok( new Order() { Id = id, Customer = $"Customer v{version}" } );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Microsoft.Examples.Controllers
{
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Query;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Examples.Models;

[ApiVersion( "3.0" )]
[ControllerName( "People" )]
public class People2Controller : ODataController
{
// GET ~/api/people?api-version=3.0
[HttpGet]
public IActionResult Get( ODataQueryOptions<Person> options, ApiVersion version ) =>
Ok( new[] { new Person() { Id = 1, FirstName = "Bill", LastName = "Mei", Email = "[email protected]", Phone = "555-555-5555" } } );

// GET ~/api/people/{id}?api-version=3.0
[HttpGet( "{id}" )]
public IActionResult Get( int id, ODataQueryOptions<Person> options, ApiVersion version ) =>
Ok( new Person() { Id = id, FirstName = "Bill", LastName = "Mei", Email = "[email protected]", Phone = "555-555-5555" } );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Microsoft.Examples.Controllers
{
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Query;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Examples.Models;

// note: since the application is configured with AssumeDefaultVersionWhenUnspecified, this controller
// is resolved without or without an API version, even though it is explicitly versioned
[ApiVersion( "1.0" )]
[ApiVersion( "2.0" )]
public class PeopleController : ODataController
{
// GET ~/api/people
// GET ~/api/people?api-version=[1.0|2.0]
[HttpGet]
public IActionResult Get( ODataQueryOptions<Person> options, ApiVersion version ) =>
Ok( new[] { new Person() { Id = 1, FirstName = "Bill", LastName = "Mei", Email = "[email protected]", Phone = "555-555-5555" } } );

// GET ~/api/people/{id}
// GET ~/api/people/{id}?api-version=[1.0|2.0]
[HttpGet( "{id}" )]
public IActionResult Get( int id, ODataQueryOptions<Person> options, ApiVersion version ) =>
Ok( new Person() { Id = id, FirstName = "Bill", LastName = "Mei", Email = "[email protected]", Phone = "555-555-5555" } );

// PATCH ~/api/people/{id}?api-version=2.0
[HttpPatch( "{id}" )]
[MapToApiVersion( "2.0" )]
public IActionResult Patch( int id, Delta<Person> delta, ODataQueryOptions<Person> options, ApiVersion version )
{
if ( !ModelState.IsValid )
return BadRequest( ModelState );

var person = new Person() { Id = id, FirstName = "Bill", LastName = "Mei", Email = "[email protected]", Phone = "555-555-5555" };

delta.Patch( person );

return Updated( person );
}
}
}
20 changes: 20 additions & 0 deletions samples/aspnetcore/AdvancedODataSample/Models/Order.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Microsoft.Examples.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

public class Order
{
public int Id { get; set; }

public DateTimeOffset CreatedDate { get; set; } = DateTimeOffset.Now;

public DateTimeOffset EffectiveDate { get; set; } = DateTimeOffset.Now;

[Required]
public string Customer { get; set; }
}
}
23 changes: 23 additions & 0 deletions samples/aspnetcore/AdvancedODataSample/Models/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Microsoft.Examples.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public class Person
{
public int Id { get; set; }

[Required]
[StringLength( 25 )]
public string FirstName { get; set; }

[Required]
[StringLength( 25 )]
public string LastName { get; set; }

public string Email { get; set; }

public string Phone { get; set; }
}
}
16 changes: 16 additions & 0 deletions samples/aspnetcore/AdvancedODataSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Microsoft.Examples
{
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

public static class Program
{
public static void Main( string[] args ) =>
CreateWebHostBuilder( args ).Build().Run();

public static IWebHostBuilder CreateWebHostBuilder( string[] args ) =>
WebHost.CreateDefaultBuilder( args )
.UseStartup<Startup>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:1237/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"AdvancedODataSample": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000/api",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Loading