-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathIgnoreDataMemberAttributeEdmPropertyConvention.cs
More file actions
60 lines (54 loc) · 2.52 KB
/
IgnoreDataMemberAttributeEdmPropertyConvention.cs
File metadata and controls
60 lines (54 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Runtime.Serialization;
using Microsoft.AspNet.OData.Common;
namespace Microsoft.AspNet.OData.Builder.Conventions.Attributes
{
/// <summary>
/// Removes properties that have <see cref="IgnoreDataMemberAttribute"/> from their edm type.
/// </summary>
internal class IgnoreDataMemberAttributeEdmPropertyConvention : AttributeEdmPropertyConvention<PropertyConfiguration>
{
public IgnoreDataMemberAttributeEdmPropertyConvention()
: base(attribute => attribute.GetType() == typeof(IgnoreDataMemberAttribute), allowMultiple: false)
{
}
/// <summary>
/// Removes the property from the edm type.
/// </summary>
/// <param name="edmProperty">The property being removed.</param>
/// <param name="structuralTypeConfiguration">The edm type from which the property is being removed.</param>
/// <param name="attribute">The <see cref="Attribute"/> found on this type.</param>
/// <param name="model">The ODataConventionModelBuilder used to build the model.</param>
public override void Apply(PropertyConfiguration edmProperty,
StructuralTypeConfiguration structuralTypeConfiguration,
Attribute attribute,
ODataConventionModelBuilder model)
{
if (structuralTypeConfiguration == null)
{
throw Error.ArgumentNull("structuralTypeConfiguration");
}
if (edmProperty == null)
{
throw Error.ArgumentNull("edmProperty");
}
if (!edmProperty.AddedExplicitly)
{
bool isTypeDataContract = TypeHelper.AsMemberInfo(structuralTypeConfiguration.ClrType).GetCustomAttributes(typeof(DataContractAttribute), inherit: true).Any();
bool isPropertyDataMember = edmProperty.PropertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), inherit: true).Any();
if (isTypeDataContract && isPropertyDataMember)
{
// both Datamember and IgnoreDataMember. DataMember wins as this a DataContract
return;
}
else
{
structuralTypeConfiguration.RemoveProperty(edmProperty.PropertyInfo.PropertyInfo);
}
}
}
}
}