Skip to content

Commit 69a2d2f

Browse files
authored
Merge pull request #2 from cristipufu/feature/odata_v7
Add support for OData v7
2 parents bfc6183 + da44055 commit 69a2d2f

17 files changed

+176
-41
lines changed

Nest.OData.sln

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,39 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nest.OData", "src\Nest.ODat
1111
EndProject
1212
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{571760D1-8307-49AB-B92F-1E9FC0DAC612}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nest.OData.Tests.Common", "test\Nest.OData.Tests.Common\Nest.OData.Tests.Common.csproj", "{53C81A82-81EE-4D80-BE1F-AA58A4D83EB4}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nest.OData.Tests.Common", "test\Nest.OData.Tests.Common\Nest.OData.Tests.Common.csproj", "{53C81A82-81EE-4D80-BE1F-AA58A4D83EB4}"
1515
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nest.OData.Tests", "test\Nest.OData.Tests\Nest.OData.Tests.csproj", "{EAD9FF3E-7C82-44B5-9F29-5655AC7D2C24}"
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nest.OData.Tests", "test\Nest.OData.Tests\Nest.OData.Tests.csproj", "{EAD9FF3E-7C82-44B5-9F29-5655AC7D2C24}"
1717
EndProject
1818
Global
1919
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2020
Debug|Any CPU = Debug|Any CPU
21+
Release_V7|Any CPU = Release_V7|Any CPU
2122
Release|Any CPU = Release|Any CPU
2223
EndGlobalSection
2324
GlobalSection(ProjectConfigurationPlatforms) = postSolution
2425
{46498294-B6F7-4702-929B-87046E4CB82F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2526
{46498294-B6F7-4702-929B-87046E4CB82F}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{46498294-B6F7-4702-929B-87046E4CB82F}.Release_V7|Any CPU.ActiveCfg = Release_V7|Any CPU
28+
{46498294-B6F7-4702-929B-87046E4CB82F}.Release_V7|Any CPU.Build.0 = Release_V7|Any CPU
2629
{46498294-B6F7-4702-929B-87046E4CB82F}.Release|Any CPU.ActiveCfg = Release|Any CPU
2730
{46498294-B6F7-4702-929B-87046E4CB82F}.Release|Any CPU.Build.0 = Release|Any CPU
2831
{D82E359A-D77E-43A9-B3C5-0389C9AF9F33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2932
{D82E359A-D77E-43A9-B3C5-0389C9AF9F33}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{D82E359A-D77E-43A9-B3C5-0389C9AF9F33}.Release_V7|Any CPU.ActiveCfg = Release_V7|Any CPU
34+
{D82E359A-D77E-43A9-B3C5-0389C9AF9F33}.Release_V7|Any CPU.Build.0 = Release_V7|Any CPU
3035
{D82E359A-D77E-43A9-B3C5-0389C9AF9F33}.Release|Any CPU.ActiveCfg = Release|Any CPU
3136
{D82E359A-D77E-43A9-B3C5-0389C9AF9F33}.Release|Any CPU.Build.0 = Release|Any CPU
3237
{53C81A82-81EE-4D80-BE1F-AA58A4D83EB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3338
{53C81A82-81EE-4D80-BE1F-AA58A4D83EB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{53C81A82-81EE-4D80-BE1F-AA58A4D83EB4}.Release_V7|Any CPU.ActiveCfg = Release_V7|Any CPU
40+
{53C81A82-81EE-4D80-BE1F-AA58A4D83EB4}.Release_V7|Any CPU.Build.0 = Release_V7|Any CPU
3441
{53C81A82-81EE-4D80-BE1F-AA58A4D83EB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
3542
{53C81A82-81EE-4D80-BE1F-AA58A4D83EB4}.Release|Any CPU.Build.0 = Release|Any CPU
3643
{EAD9FF3E-7C82-44B5-9F29-5655AC7D2C24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3744
{EAD9FF3E-7C82-44B5-9F29-5655AC7D2C24}.Debug|Any CPU.Build.0 = Debug|Any CPU
45+
{EAD9FF3E-7C82-44B5-9F29-5655AC7D2C24}.Release_V7|Any CPU.ActiveCfg = Release_V7|Any CPU
46+
{EAD9FF3E-7C82-44B5-9F29-5655AC7D2C24}.Release_V7|Any CPU.Build.0 = Release_V7|Any CPU
3847
{EAD9FF3E-7C82-44B5-9F29-5655AC7D2C24}.Release|Any CPU.ActiveCfg = Release|Any CPU
3948
{EAD9FF3E-7C82-44B5-9F29-5655AC7D2C24}.Release|Any CPU.Build.0 = Release|Any CPU
4049
EndGlobalSection

src/Nest.OData/Nest.OData.csproj

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7+
<Configurations>Debug;Release;Release_V7</Configurations>
78
</PropertyGroup>
89

9-
<ItemGroup>
10+
<PropertyGroup Condition="'$(Configuration)'=='Release_V7'">
11+
<DefineConstants>$(DefineConstants);USE_ODATA_V7</DefineConstants>
12+
</PropertyGroup>
13+
14+
<ItemGroup Condition="'$(Configuration)'=='Release_V7'">
15+
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.12" />
16+
</ItemGroup>
17+
18+
<ItemGroup Condition="'$(Configuration)'=='Debug' Or '$(Configuration)'=='Release'">
1019
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.5" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
1123
<PackageReference Include="NEST" Version="7.17.5" />
1224
</ItemGroup>
1325

src/Nest.OData/ODataAggregationExtensions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using Microsoft.AspNetCore.OData.Query;
1+
#if USE_ODATA_V7
2+
using Microsoft.AspNet.OData.Query;
3+
#else
4+
using Microsoft.AspNetCore.OData.Query;
5+
#endif
26
using Microsoft.OData.UriParser.Aggregation;
37

48
#nullable disable

src/Nest.OData/ODataExtensions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using Microsoft.AspNetCore.OData.Query;
1+
#if USE_ODATA_V7
2+
using Microsoft.AspNet.OData.Query;
3+
#else
4+
using Microsoft.AspNetCore.OData.Query;
5+
#endif
26

37
namespace Nest.OData
48
{

src/Nest.OData/ODataFilterExtensions.cs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
using Microsoft.AspNetCore.OData.Query;
1+
#if USE_ODATA_V7
2+
using Microsoft.AspNet.OData.Query;
3+
using Microsoft.OData;
4+
#else
5+
using Microsoft.AspNetCore.OData.Query;
6+
using Microsoft.OData;
7+
#endif
28
using Microsoft.OData.Edm;
39
using Microsoft.OData.UriParser;
410

@@ -76,7 +82,8 @@ private static QueryContainer TranslateAllNode(AllNode node)
7682

7783
var query = new BoolQuery
7884
{
79-
MustNot = [
85+
MustNot =
86+
[
8087
!TranslateExpression(node.Body, new ODataExpressionContext
8188
{
8289
PathPrefix = fullyQualifiedFieldName,
@@ -325,9 +332,9 @@ private static object ExtractValue(QueryNode node)
325332
{
326333
if (node is ConstantNode constantNode)
327334
{
328-
if (constantNode.TypeReference is IEdmEnumTypeReference)
335+
if (constantNode.TypeReference?.Definition?.TypeKind is EdmTypeKind.Enum)
329336
{
330-
return constantNode.Value?.ToString();
337+
return (constantNode.Value as ODataEnumValue).Value?.ToString();
331338
}
332339

333340
return constantNode.Value;

src/Nest.OData/ODataOrderByExtensions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using Microsoft.AspNetCore.OData.Query;
1+
#if USE_ODATA_V7
2+
using Microsoft.AspNet.OData.Query;
3+
#else
4+
using Microsoft.AspNetCore.OData.Query;
5+
#endif
26
using Microsoft.OData.UriParser;
37

48
namespace Nest.OData

src/Nest.OData/ODataPaginationExtensions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using Microsoft.AspNetCore.OData.Query;
1+
#if USE_ODATA_V7
2+
using Microsoft.AspNet.OData.Query;
3+
#else
4+
using Microsoft.AspNetCore.OData.Query;
5+
#endif
26

37
namespace Nest.OData
48
{

src/Nest.OData/ODataSelectExpandExtensions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using Microsoft.AspNetCore.OData.Query;
1+
#if USE_ODATA_V7
2+
using Microsoft.AspNet.OData.Query;
3+
#else
4+
using Microsoft.AspNetCore.OData.Query;
5+
#endif
26
using Microsoft.OData.UriParser;
37

48
namespace Nest.OData

test/Nest.OData.Sample/Controllers/ProductsController.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using Microsoft.AspNetCore.Mvc;
2+
#if USE_ODATA_V7
3+
using Microsoft.AspNet.OData.Query;
4+
using Microsoft.AspNet.OData;
5+
#else
26
using Microsoft.AspNetCore.OData.Query;
3-
using Microsoft.AspNetCore.OData.Routing.Controllers;
7+
#endif
48
using Nest.OData.Tests.Common;
59

610
namespace Nest.OData.Sample.Controllers
711
{
8-
public class ProductsController : ODataController
12+
public class ProductsController : ControllerBase
913
{
1014
private readonly IList<Product> products;
1115
private static readonly string[] value = ["Leonard G. Lobel", "Eric D. Boyd"];
@@ -26,7 +30,8 @@ public ProductsController()
2630
CreatedDate = new DateTimeOffset(2001, 4, 15, 16, 24, 8, TimeSpan.FromHours(-8)),
2731
UpdatedDate = new DateTimeOffset(2011, 2, 15, 16, 24, 8, TimeSpan.FromHours(-8)),
2832
ProductDetail = new ProductDetail { Id = i, Info = "Info" + i },
29-
ProductOrders = [
33+
ProductOrders =
34+
[
3035
new Order
3136
{
3237
Id = i,
@@ -60,9 +65,10 @@ public ProductsController()
6065
}
6166

6267
[HttpGet]
63-
public IActionResult Get(ODataQueryOptions<Product> queryOptions)
68+
[EnableQuery]
69+
public ActionResult<IEnumerable<Product>> Get(ODataQueryOptions<Product> queryOptions)
6470
{
65-
var _ = queryOptions.ToElasticQuery();
71+
queryOptions.ToElasticQuery();
6672

6773
return Ok(products);
6874
}

test/Nest.OData.Sample/Nest.OData.Sample.csproj

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<Configurations>Debug;Release;Release_V7</Configurations>
78
</PropertyGroup>
89

9-
<ItemGroup>
10+
<PropertyGroup Condition="'$(Configuration)'=='Release_V7'">
11+
<DefineConstants>$(DefineConstants);USE_ODATA_V7</DefineConstants>
12+
</PropertyGroup>
13+
14+
<ItemGroup Condition="'$(Configuration)'=='Release_V7'">
15+
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.12" />
16+
<PackageReference Include="Microsoft.AspNetCore.OData.Versioning.ApiExplorer" Version="4.1.1" />
17+
</ItemGroup>
18+
19+
<ItemGroup Condition="'$(Configuration)'=='Debug' Or '$(Configuration)'=='Release'">
1020
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.5" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
1124
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
1225
</ItemGroup>
1326

test/Nest.OData.Sample/Program.cs

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1+
#if USE_ODATA_V7
2+
using Microsoft.AspNet.OData.Extensions;
3+
#else
14
using Microsoft.AspNetCore.OData;
5+
#endif
26
using Nest.OData.Sample.SwashbuckleFilters;
37
using Nest.OData.Tests.Common;
48

59
var builder = WebApplication.CreateBuilder(args);
610

11+
#if USE_ODATA_V7
12+
builder.Services.AddControllers();
13+
builder.Services.AddOData().EnableApiVersioning();
14+
builder.Services.AddApiVersioning(options =>
15+
{
16+
options.AssumeDefaultVersionWhenUnspecified = true;
17+
});
18+
builder.Services.AddODataApiExplorer(o => o.GroupNameFormat = "'v'VVVV");
19+
builder.Services.AddEndpointsApiExplorer();
20+
#else
721
builder.Services.AddControllers().AddOData(
822
opt => opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(5).AddRouteComponents("odata", EdmModelBuilder.GetEdmModel())
923
);
10-
1124
builder.Services.AddEndpointsApiExplorer();
25+
#endif
1226
builder.Services.AddSwaggerGen(c =>
1327
{
1428
c.OperationFilter<ODataOperationFilter>();
@@ -17,16 +31,23 @@
1731

1832
var app = builder.Build();
1933

20-
if (app.Environment.IsDevelopment())
21-
{
22-
app.UseSwagger();
23-
app.UseSwaggerUI();
24-
}
34+
app.UseSwagger();
35+
app.UseSwaggerUI();
2536

2637
app.UseHttpsRedirection();
2738

28-
app.UseAuthorization();
29-
39+
#if USE_ODATA_V7
40+
app.UseRouting();
41+
#pragma warning disable ASP0014 // Suggest using top level route registrations
42+
app.UseEndpoints(endpoints =>
43+
{
44+
endpoints.MapControllers();
45+
endpoints.Select().Filter().OrderBy().Count().MaxTop(10);
46+
endpoints.MapODataRoute("odata", "odata", EdmModelBuilder.GetEdmModel());
47+
});
48+
#pragma warning restore ASP0014 // Suggest using top level route registrations
49+
#else
3050
app.MapControllers();
51+
#endif
3152

32-
app.Run();
53+
app.Run();

test/Nest.OData.Sample/SwashbuckleFilters/ODataOperationFilter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class ODataOperationFilter : IOperationFilter
77
{
88
public void Apply(OpenApiOperation operation, OperationFilterContext context)
99
{
10-
operation.Parameters ??= [];
10+
operation.Parameters ??= new List<OpenApiParameter>();
1111

1212
var odataParams = new List<OpenApiParameter>
1313
{

test/Nest.OData.Sample/SwashbuckleFilters/RemoveODataQueryOptionsFilter.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using Microsoft.AspNetCore.OData.Query;
1+
#if USE_ODATA_V7
2+
using Microsoft.AspNet.OData.Query;
3+
#else
4+
using Microsoft.AspNetCore.OData.Query;
5+
#endif
26
using Microsoft.OpenApi.Models;
37
using Swashbuckle.AspNetCore.SwaggerGen;
48

test/Nest.OData.Tests.Common/EdmModelBuilder.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using Microsoft.OData.Edm;
2+
#if USE_ODATA_V7
3+
using Microsoft.AspNet.OData.Builder;
4+
#else
25
using Microsoft.OData.ModelBuilder;
6+
#endif
37

48
namespace Nest.OData.Tests.Common
59
{
@@ -12,10 +16,7 @@ public static IEdmModel GetEdmModel()
1216
builder.EntitySet<Supplier>("Suppliers");
1317
builder.EntitySet<Order>("Orders");
1418

15-
var model = builder.GetEdmModel();
16-
model.MarkAsImmutable();
17-
18-
return model;
19+
return builder.GetEdmModel();
1920
}
2021
}
2122
}

test/Nest.OData.Tests.Common/Nest.OData.Tests.Common.csproj

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7+
<Configurations>Debug;Release;Release_V7</Configurations>
78
</PropertyGroup>
89

9-
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.5" />
10+
<PropertyGroup Condition="'$(Configuration)'=='Release_V7'">
11+
<DefineConstants>$(DefineConstants);USE_ODATA_V7</DefineConstants>
12+
</PropertyGroup>
13+
14+
<ItemGroup Condition="'$(Configuration)'=='Release_V7'">
15+
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.12" />
16+
</ItemGroup>
17+
18+
<ItemGroup Condition="'$(Configuration)'=='Debug' Or '$(Configuration)'=='Release'">
19+
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.5" />
1120
</ItemGroup>
1221

1322
</Project>

0 commit comments

Comments
 (0)