Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/aspnetwebstack.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorraghuramn <ranadimi@microsoft.com>2012-09-06 21:36:52 +0400
committerraghuramn <ranadimi@microsoft.com>2012-10-16 21:32:51 +0400
commit41852a3a8fa8142698c04f7fc192b6b21f5c2ca6 (patch)
tree4e17a447a721a7eac7b9f463b995f247d30409a6 /src
parentcf176acb971f2c64f6d41a25c8777c6c8d7928c4 (diff)
Issue 459: [DataMember(IsRequired=true)] doesn't make property as required
in ODataConventionModelBuilder 1) Added support for IsRequired property on DataMember. 2) Also, fixing the issue where the RequiredAttributeConvention overwrites the already existing user configuration. 3) Also, fixing a similar issue where a navigation property added by user is overwritten by convention modelbuilder.
Diffstat (limited to 'src')
-rw-r--r--src/System.Web.Http.OData/OData/Builder/Conventions/Attributes/DataMemberAttributeEdmPropertyConvention.cs39
-rw-r--r--src/System.Web.Http.OData/OData/Builder/Conventions/Attributes/RequiredAttributeEdmPropertyConvention.cs5
-rw-r--r--src/System.Web.Http.OData/OData/Builder/ODataConventionModelBuilder.cs17
-rw-r--r--src/System.Web.Http.OData/OData/Builder/StructuralPropertyConfiguration.cs23
-rw-r--r--src/System.Web.Http.OData/System.Web.Http.OData.csproj1
5 files changed, 73 insertions, 12 deletions
diff --git a/src/System.Web.Http.OData/OData/Builder/Conventions/Attributes/DataMemberAttributeEdmPropertyConvention.cs b/src/System.Web.Http.OData/OData/Builder/Conventions/Attributes/DataMemberAttributeEdmPropertyConvention.cs
new file mode 100644
index 00000000..90b0aa34
--- /dev/null
+++ b/src/System.Web.Http.OData/OData/Builder/Conventions/Attributes/DataMemberAttributeEdmPropertyConvention.cs
@@ -0,0 +1,39 @@
+// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
+
+using System.Linq;
+using System.Runtime.Serialization;
+
+namespace System.Web.Http.OData.Builder.Conventions.Attributes
+{
+ /// <summary>
+ /// Configures properties that have <see cref="DataMemberAttribute"/> as optional or required on their edm type.
+ /// </summary>
+ public class DataMemberAttributeEdmPropertyConvention : AttributeEdmPropertyConvention<StructuralPropertyConfiguration>
+ {
+ public DataMemberAttributeEdmPropertyConvention()
+ : base(attribute => attribute.GetType() == typeof(DataMemberAttribute), allowMultiple: false)
+ {
+ }
+
+ public override void Apply(StructuralPropertyConfiguration edmProperty, IStructuralTypeConfiguration structuralTypeConfiguration, Attribute attribute)
+ {
+ if (structuralTypeConfiguration == null)
+ {
+ throw Error.ArgumentNull("structuralTypeConfiguration");
+ }
+
+ if (edmProperty == null)
+ {
+ throw Error.ArgumentNull("edmProperty");
+ }
+
+ bool isTypeDataContract = structuralTypeConfiguration.ClrType.GetCustomAttributes(typeof(DataContractAttribute), inherit: true).Any();
+ DataMemberAttribute dataMember = attribute as DataMemberAttribute;
+
+ if (isTypeDataContract && dataMember != null && !edmProperty.IsOptionalPropertyExplicitlySet)
+ {
+ edmProperty.OptionalProperty = !dataMember.IsRequired;
+ }
+ }
+ }
+}
diff --git a/src/System.Web.Http.OData/OData/Builder/Conventions/Attributes/RequiredAttributeEdmPropertyConvention.cs b/src/System.Web.Http.OData/OData/Builder/Conventions/Attributes/RequiredAttributeEdmPropertyConvention.cs
index e727527f..79c30ee7 100644
--- a/src/System.Web.Http.OData/OData/Builder/Conventions/Attributes/RequiredAttributeEdmPropertyConvention.cs
+++ b/src/System.Web.Http.OData/OData/Builder/Conventions/Attributes/RequiredAttributeEdmPropertyConvention.cs
@@ -27,7 +27,10 @@ namespace System.Web.Http.OData.Builder.Conventions.Attributes
throw Error.ArgumentNull("edmProperty");
}
- edmProperty.OptionalProperty = false;
+ if (!edmProperty.IsOptionalPropertyExplicitlySet)
+ {
+ edmProperty.OptionalProperty = false;
+ }
}
}
}
diff --git a/src/System.Web.Http.OData/OData/Builder/ODataConventionModelBuilder.cs b/src/System.Web.Http.OData/OData/Builder/ODataConventionModelBuilder.cs
index 900e4d32..bc294122 100644
--- a/src/System.Web.Http.OData/OData/Builder/ODataConventionModelBuilder.cs
+++ b/src/System.Web.Http.OData/OData/Builder/ODataConventionModelBuilder.cs
@@ -27,6 +27,7 @@ namespace System.Web.Http.OData.Builder
new DataContractAttributeEdmTypeConvention(),
new NotMappedAttributeConvention(), // NotMappedAttributeConvention has to run before EntityKeyConvention
new EntityKeyConvention(),
+ new DataMemberAttributeEdmPropertyConvention(),
new RequiredAttributeEdmPropertyConvention(),
new KeyAttributeEdmPropertyConvention(),
new IgnoreDataMemberAttributeEdmPropertyConvention(),
@@ -366,13 +367,17 @@ namespace System.Web.Http.OData.Builder
}
else
{
- if (!isCollection)
+ // don't add this property if the user has already added it.
+ if (!entity.NavigationProperties.Where(p => p.Name == property.Name).Any())
{
- entity.AddNavigationProperty(property, EdmMultiplicity.ZeroOrOne);
- }
- else
- {
- entity.AddNavigationProperty(property, EdmMultiplicity.Many);
+ if (!isCollection)
+ {
+ entity.AddNavigationProperty(property, EdmMultiplicity.ZeroOrOne);
+ }
+ else
+ {
+ entity.AddNavigationProperty(property, EdmMultiplicity.Many);
+ }
}
}
}
diff --git a/src/System.Web.Http.OData/OData/Builder/StructuralPropertyConfiguration.cs b/src/System.Web.Http.OData/OData/Builder/StructuralPropertyConfiguration.cs
index 9c6bce09..4cb141f0 100644
--- a/src/System.Web.Http.OData/OData/Builder/StructuralPropertyConfiguration.cs
+++ b/src/System.Web.Http.OData/OData/Builder/StructuralPropertyConfiguration.cs
@@ -9,16 +9,29 @@ namespace System.Web.Http.OData.Builder
/// </summary>
public abstract class StructuralPropertyConfiguration : PropertyConfiguration
{
+ private bool _optionalProperty;
+
protected StructuralPropertyConfiguration(PropertyInfo property, IStructuralTypeConfiguration declaringType)
: base(property, declaringType)
{
- OptionalProperty = IsNullable(property.PropertyType);
+ _optionalProperty = IsNullable(property.PropertyType);
+ }
+
+ public bool OptionalProperty
+ {
+ get
+ {
+ return _optionalProperty;
+ }
+
+ set
+ {
+ _optionalProperty = value;
+ IsOptionalPropertyExplicitlySet = true;
+ }
}
- /// <summary>
- /// Gets or sets a value indicating whether this property is optional or required.
- /// </summary>
- public bool OptionalProperty { get; set; }
+ internal bool IsOptionalPropertyExplicitlySet { get; set; }
private static bool IsNullable(Type type)
{
diff --git a/src/System.Web.Http.OData/System.Web.Http.OData.csproj b/src/System.Web.Http.OData/System.Web.Http.OData.csproj
index 914e199d..2b65ff29 100644
--- a/src/System.Web.Http.OData/System.Web.Http.OData.csproj
+++ b/src/System.Web.Http.OData/System.Web.Http.OData.csproj
@@ -110,6 +110,7 @@
<Compile Include="OData\Builder\Conventions\ActionLinkGenerationConvention.cs" />
<Compile Include="OData\Builder\Conventions\IProcedureConvention.cs" />
<Compile Include="OData\Builder\Conventions\AssociationSetDiscoveryConvention.cs" />
+ <Compile Include="OData\Builder\Conventions\Attributes\DataMemberAttributeEdmPropertyConvention.cs" />
<Compile Include="OData\ODataActionParameters.cs" />
<Compile Include="OData\Builder\ActionConfiguration.cs" />
<Compile Include="OData\Builder\BindingParameterConfiguration.cs" />