Skip to content

Backport Model Bound Setting Overrides #974

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
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>6.4.0</VersionPrefix>
<VersionPrefix>6.4.1</VersionPrefix>
<AssemblyVersion>6.4.0.0</AssemblyVersion>
<TargetFrameworks>net45;net472</TargetFrameworks>
<RootNamespace>Asp.Versioning</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@

Fix: `EnableQueryAttribute` should override _Model Bound_ settings (Related to [#928](https://github.com/dotnet/aspnet-api-versioning/issues/928))
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>6.4.0</VersionPrefix>
<VersionPrefix>6.4.1</VersionPrefix>
<AssemblyVersion>6.4.0.0</AssemblyVersion>
<TargetFrameworks>net6.0;netcoreapp3.1</TargetFrameworks>
<RootNamespace>Asp.Versioning</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@

Fix: `EnableQueryAttribute` should override _Model Bound_ settings (Related to [#928](https://github.com/dotnet/aspnet-api-versioning/issues/928))
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Asp.Versioning.Conventions;
using Microsoft.OData.ModelBuilder;
using Microsoft.OData.ModelBuilder.Config;
using System.Reflection;
using Xunit;
using static Microsoft.AspNetCore.Http.StatusCodes;
using static Microsoft.AspNetCore.Mvc.ModelBinding.BindingSource;
using static Microsoft.AspNetCore.OData.Query.AllowedArithmeticOperators;
Expand Down Expand Up @@ -469,6 +470,50 @@ public void apply_to_should_use_model_bound_query_attributes()
options => options.ExcludingMissingMembers() );
}

[Fact]
public void apply_should_override_model_bound_settings_with_enable_query_attribute()
{
// arrange
var builder = new ODataConventionModelBuilder().EnableLowerCamelCase();

builder.EntitySet<Customer>( "Customers" );

var validationSettings = new ODataValidationSettings()
{
AllowedQueryOptions = AllowedQueryOptions.None,
AllowedArithmeticOperators = AllowedArithmeticOperators.None,
AllowedLogicalOperators = AllowedLogicalOperators.None,
AllowedFunctions = AllowedFunctions.None,
};
var settings = new TestODataQueryOptionSettings( typeof( Customer ) );
var convention = new ODataValidationSettingsConvention( validationSettings, settings );
var model = builder.GetEdmModel();
var description = NewApiDescription( typeof( CustomersController ), typeof( IEnumerable<Customer> ), model );

// act
convention.ApplyTo( description );

// assert
var parameter = description.ParameterDescriptions.Single();

parameter.Should().BeEquivalentTo(
new
{
Name = "$filter",
Source = Query,
Type = typeof( string ),
DefaultValue = default( object ),
IsRequired = false,
ModelMetadata = new { Description = "Test" },
ParameterDescriptor = new
{
Name = "$filter",
ParameterType = typeof( string ),
},
},
options => options.ExcludingMissingMembers() );
}

[Fact]
public void apply_to_should_process_odataX2Dlike_api_description()
{
Expand Down Expand Up @@ -678,6 +723,13 @@ public class OrdersController : ODataController
public IActionResult Get( ODataQueryOptions<Order> options ) => Ok();
}

public class CustomersController : ODataController
{
[EnableQuery( AllowedQueryOptions = Filter )]
[ProducesResponseType( typeof( IEnumerable<Customer> ), Status200OK )]
public IActionResult Get( ODataQueryOptions<Customer> options ) => Ok();
}

[Select]
[Filter]
[Count]
Expand All @@ -693,6 +745,12 @@ public class Order
public int Quantity { get; set; }
}

[Page( MaxTop = 25, PageSize = 25 )]
public class Customer
{
public int CustomerId { get; set; }
}

private sealed class TestODataQueryOptionSettings : ODataQueryOptionSettings
{
internal TestODataQueryOptionSettings( Type type, bool dollarPrefix = true ) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ internal ODataAttributeVisitor(

internal void Visit( ApiDescription apiDescription )
{
VisitAction( apiDescription.ActionDescriptor );

var modelType = context.ReturnType;

if ( modelType != null )
{
VisitModel( modelType );
}

VisitAction( apiDescription.ActionDescriptor );
}

private void VisitModel( IEdmStructuredType modelType )
Expand Down