Skip to content

Support for properties renamed with [DataMember] in OData models #512

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

Closed
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
Expand Up @@ -96,14 +96,26 @@ static Type GenerateTypeIfNeeded( IEdmStructuredType structuredType, BuilderCont
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

var properties = new List<ClassProperty>();
var structuralProperties = structuredType.Properties().ToDictionary( p => p.Name, StringComparer.OrdinalIgnoreCase );
var structuralProperties = new Dictionary<string, IEdmProperty>( StringComparer.OrdinalIgnoreCase );
var mappedClrProperties = new Dictionary<PropertyInfo, IEdmProperty>();
var clrTypeMatchesEdmType = true;
var hasUnfinishedTypes = false;
var dependentProperties = new List<PropertyDependency>();

foreach ( var property in structuredType.Properties() )
{
structuralProperties.Add( property.Name, property );
var clrProperty = edmModel.GetAnnotationValue<ClrPropertyInfoAnnotation>( property )?.ClrPropertyInfo;
if ( clrProperty != null )
{
mappedClrProperties.Add( clrProperty, property );
}
}

foreach ( var property in clrType.GetProperties( bindingFlags ) )
{
if ( !structuralProperties.TryGetValue( property.Name, out var structuralProperty ) )
if ( !structuralProperties.TryGetValue( property.Name, out var structuralProperty ) &&
!mappedClrProperties.TryGetValue( property, out structuralProperty ) )
{
clrTypeMatchesEdmType = false;
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
namespace Microsoft.AspNet.OData

namespace Microsoft.AspNet.OData
{
using System.Collections.Generic;
using System.Runtime.Serialization;

[DataContract]
public class Contact
{
[DataMember]
public int ContactId { get; set; }

[DataMember( Name = "first_name" )]
public string FirstName { get; set; }

[DataMember]
public string LastName { get; set; }

[DataMember]
public string Email { get; set; }

[DataMember]
public string Phone { get; set; }

[DataMember]
public List<Address> Addresses { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Microsoft.AspNet.OData
{
using FluentAssertions;
using FluentAssertions.Common;
using System.Runtime.Serialization;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -408,6 +410,25 @@ public void substitute_should_resolve_types_that_reference_a_model_that_match_th
substitutionType.Should().NotBeOfType<TypeBuilder>();
}

[Fact]
public void substituted_type_should_have_renamed_with_attribute_properties_from_original_type()
{
// arrange
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Contact>( "Contacts" );

var context = NewContext( modelBuilder.GetEdmModel() );
var originalType = typeof( Contact );

// act
var substitutedType = originalType.SubstituteIfNecessary( context );

// assert
substitutedType.Should().HaveProperty<string>( nameof( Contact.FirstName ) );
substitutedType.GetRuntimeProperty( nameof( Contact.FirstName ) ).Should().NotBeNull();
substitutedType.GetRuntimeProperty( nameof( Contact.FirstName ) ).HasAttribute<DataMemberAttribute>();
}

public static IEnumerable<object[]> SubstitutionNotRequiredData
{
get
Expand Down