Example #1
0
        public void ModelBuilder_DerivedComplexTypeHavingKeys_SuccedsIfToldToBeComplex()
        {
            MockType baseComplexType = new MockType("BaseComplexType");

            MockType derivedComplexType =
                new MockType("DerivedComplexType")
                .Property(typeof(int), "DerivedComplexTypeId")
                .BaseType(baseComplexType);

            MockType entityType =
                new MockType("EntityType")
                .Property(typeof(int), "ID")
                .Property(baseComplexType.Object, "ComplexProperty");

            MockAssembly assembly = new MockAssembly(baseComplexType, derivedComplexType, entityType);

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.Services.Replace(typeof(IAssembliesResolver), new TestAssemblyResolver(assembly));
            var builder = new ODataConventionModelBuilder(configuration);

            builder.AddEntitySet("entities", builder.AddEntity(entityType));
            builder.AddComplexType(baseComplexType);

            IEdmModel model = builder.GetEdmModel();

            Assert.Equal(3, model.SchemaElements.Count());
            Assert.NotNull(model.FindType("DefaultNamespace.EntityType"));
            Assert.NotNull(model.FindType("DefaultNamespace.BaseComplexType"));
        }
        public void ModelBuilder_SupportsComplexCollectionWhenToldElementTypeIsComplex(Type complexCollectionPropertyType)
        {
            var  modelBuilder = new ODataConventionModelBuilder();
            Type entityType   = CreateDynamicType(
                new DynamicType
            {
                TypeName   = "SampleType",
                Properties =
                {
                    new DynamicProperty {
                        Name = "ID", Type = typeof(int)
                    },
                    new DynamicProperty {
                        Name = "Property1", Type = complexCollectionPropertyType
                    }
                }
            });

            modelBuilder.AddEntity(entityType);
            modelBuilder.AddComplexType(typeof(Version));
            IEdmModel      model  = modelBuilder.GetEdmModel();
            IEdmEntityType entity = model.GetEdmType(entityType) as IEdmEntityType;

            Assert.NotNull(entity);
            Assert.Equal(2, entity.DeclaredProperties.Count());

            IEdmStructuralProperty property1 = entity.DeclaredProperties.OfType <IEdmStructuralProperty>().SingleOrDefault(p => p.Name == "Property1");

            Assert.NotNull(property1);
            Assert.Equal(EdmTypeKind.Collection, property1.Type.Definition.TypeKind);
            Assert.Equal(EdmTypeKind.Complex, (property1.Type.Definition as IEdmCollectionType).ElementType.Definition.TypeKind);
        }
    /// <summary>
    /// Registers an ExtendableEnum type with the <see cref="ODataConventionModelBuilder" />.
    /// </summary>
    /// <param name="builder">The <see cref="ODataConventionModelBuilder"/> with which to register the ExtendableEnum.</param>
    /// <param name="enumerationType">The actual <see cref="Type"/> of the class that inherits from <see cref="ExtendableEnumBase{TEnumeration, TValue}" /> that is to be registered.</param>
    /// <returns>The <see cref="ComplexTypeConfiguration"/> that the ExtendableEnum gets registered as.</returns>
    public static ComplexTypeConfiguration AddExtendableEnum(this ODataConventionModelBuilder builder, Type enumerationType)
    {
        if (builder is null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        var result = builder.AddComplexType(enumerationType);

        AddProperty(result, "Value");
        return(result);
    }
Example #4
0
        private static IEdmModel GetModel()
        {
            var builder = new ODataConventionModelBuilder();

            var objects = builder.EntitySet <Object>("Objects");
            var objectsWithManualExpanding = builder.EntitySet <Object>("ObjectsWithManualExpanding");
            var relationships = builder.EntitySet <Relationship>("Relationships");

            var objectEntity       = objects.EntityType;
            var relationshipEntity = relationships.EntityType;
            var objectWithManualExpandingEntity = objectsWithManualExpanding.EntityType;

            builder.EntitySet <AttributeValue>("AttributeValues");

            objectEntity.Expand(nameof(Object.AttributeValues));
            relationshipEntity.Expand(nameof(Relationship.AttributeValues));
            objectWithManualExpandingEntity.Expand(nameof(Object.AttributeValues));

            builder.AddComplexType(typeof(ChoiceValue));
            builder.AddComplexType(typeof(HyperlinkValue));

            return(builder.GetEdmModel());
        }
Example #5
0
        public void ComplexType_Containing_EntityCollection_Throws()
        {
            MockType entityType = new MockType("EntityType");

            MockType complexType =
                new MockType("ComplexTypeWithEntityCollection")
                .Property(entityType.AsCollection(), "CollectionProperty");

            var modelBuilder = new ODataConventionModelBuilder();

            modelBuilder.AddEntity(entityType);
            modelBuilder.AddComplexType(complexType);

            Assert.Throws <InvalidOperationException>(
                () => modelBuilder.GetEdmModel(),
                "The complex type 'DefaultNamespace.ComplexTypeWithEntityCollection' refers to the entity type 'DefaultNamespace.EntityType' through the property 'CollectionProperty'.");
        }
Example #6
0
 public override IEdmModel GetModel(Type elementClrType, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)
 {
     return(actionDescriptor.Properties.GetOrAdd(ModelKeyPrefix + elementClrType.FullName, _ =>
     {
         var builder = new ODataConventionModelBuilder(actionDescriptor.Configuration, true);
         if (EnableLowerCamelCase)
         {
             builder.EnableLowerCamelCase();
         }
         foreach (var type in KnownComplexTypes)
         {
             builder.AddComplexType(type);
         }
         var config = builder.AddEntityType(elementClrType);
         builder.AddEntitySet(elementClrType.Name, config);
         return builder.GetEdmModel();
     }) as IEdmModel);
 }
Example #7
0
        public void ODataConventionModelBuilder_IgnoresIndexerProperties()
        {
            MockType type =
                new MockType("ComplexType")
                .Property <int>("Item");

            MockPropertyInfo pi = type.GetProperty("Item");

            pi.Setup(p => p.GetIndexParameters()).Returns(new[] { new Mock <ParameterInfo>().Object }); // make it indexer

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.AddComplexType(type);

            IEdmModel       model       = builder.GetEdmModel();
            IEdmComplexType complexType = model.AssertHasComplexType(type);

            Assert.Empty(complexType.Properties());
        }
Example #8
0
        public void ComplexType_Containing_ComplexCollection_works()
        {
            Type complexType =
                new MockType("ComplexTypeWithComplexCollection")
                .Property <Version[]>("CollectionProperty");

            var modelBuilder = new ODataConventionModelBuilder();

            modelBuilder.AddComplexType(complexType);

            var model = modelBuilder.GetEdmModel();

            IEdmComplexType complexEdmType = model.AssertHasComplexType(complexType);

            model.AssertHasComplexType(typeof(Version));
            var collectionProperty = complexEdmType.DeclaredProperties.Where(p => p.Name == "CollectionProperty").SingleOrDefault();

            Assert.NotNull(collectionProperty);
            Assert.True(collectionProperty.Type.IsCollection());
            Assert.Equal(collectionProperty.Type.AsCollection().ElementType().FullName(), "System.Version");
        }
        public void ODataConventionModelBuilder_IgnoresIndexerProperties()
        {
            MockType type =
                new MockType("ComplexType")
                .Property<int>("Item");

            MockPropertyInfo pi = type.GetProperty("Item");
            pi.Setup(p => p.GetIndexParameters()).Returns(new[] { new Mock<ParameterInfo>().Object }); // make it indexer

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.AddComplexType(type);

            IEdmModel model = builder.GetEdmModel();
            IEdmComplexType complexType = model.AssertHasComplexType(type);
            Assert.Empty(complexType.Properties());
        }
        public void ComplexType_Containing_ComplexCollection_works()
        {
            Type complexType =
                new MockType("ComplexTypeWithComplexCollection")
                .Property<Version[]>("CollectionProperty");

            var modelBuilder = new ODataConventionModelBuilder();
            modelBuilder.AddComplexType(complexType);

            var model = modelBuilder.GetEdmModel();

            IEdmComplexType complexEdmType = model.AssertHasComplexType(complexType);
            model.AssertHasComplexType(typeof(Version));
            var collectionProperty = complexEdmType.DeclaredProperties.Where(p => p.Name == "CollectionProperty").SingleOrDefault();
            Assert.NotNull(collectionProperty);
            Assert.True(collectionProperty.Type.IsCollection());
            Assert.Equal(collectionProperty.Type.AsCollection().ElementType().FullName(), "System.Version");
        }
        public void ComplexType_Containing_EntityCollection_Throws()
        {
            MockType entityType = new MockType("EntityType");

            MockType complexType =
                new MockType("ComplexTypeWithEntityCollection")
                .Property(entityType.AsCollection(), "CollectionProperty");

            var modelBuilder = new ODataConventionModelBuilder();
            modelBuilder.AddEntity(entityType);
            modelBuilder.AddComplexType(complexType);

            Assert.Throws<InvalidOperationException>(
                () => modelBuilder.GetEdmModel(),
                "The complex type 'DefaultNamespace.ComplexTypeWithEntityCollection' refers to the entity type 'DefaultNamespace.EntityType' through the property 'CollectionProperty'.");
        }
        public void ModelBuilder_DerivedComplexTypeHavingKeys_SuccedsIfToldToBeComplex()
        {
            MockType baseComplexType = new MockType("BaseComplexType");

            MockType derivedComplexType =
                new MockType("DerivedComplexType")
                .Property(typeof(int), "DerivedComplexTypeId")
                .BaseType(baseComplexType);

            MockType entityType =
                new MockType("EntityType")
                .Property(typeof(int), "ID")
                .Property(baseComplexType.Object, "ComplexProperty");

            MockAssembly assembly = new MockAssembly(baseComplexType, derivedComplexType, entityType);

            HttpConfiguration configuration = new HttpConfiguration();
            configuration.Services.Replace(typeof(IAssembliesResolver), new TestAssemblyResolver(assembly));
            var builder = new ODataConventionModelBuilder(configuration);

            builder.AddEntitySet("entities", builder.AddEntity(entityType));
            builder.AddComplexType(baseComplexType);

            IEdmModel model = builder.GetEdmModel();
            Assert.Equal(3, model.SchemaElements.Count());
            Assert.NotNull(model.FindType("DefaultNamespace.EntityType"));
            Assert.NotNull(model.FindType("DefaultNamespace.BaseComplexType"));
        }
        public void ModelBuilder_SupportsComplexCollectionWhenToldElementTypeIsComplex(Type complexCollectionPropertyType)
        {
            var modelBuilder = new ODataConventionModelBuilder();
            Type entityType =
                new MockType("SampleType")
                .Property<int>("ID")
                .Property(complexCollectionPropertyType, "Property1");

            modelBuilder.AddEntity(entityType);
            modelBuilder.AddComplexType(typeof(Version));
            IEdmModel model = modelBuilder.GetEdmModel();
            IEdmEntityType entity = model.GetEdmType(entityType) as IEdmEntityType;

            Assert.NotNull(entity);
            Assert.Equal(2, entity.DeclaredProperties.Count());

            IEdmStructuralProperty property1 = entity.DeclaredProperties.OfType<IEdmStructuralProperty>().SingleOrDefault(p => p.Name == "Property1");
            Assert.NotNull(property1);
            Assert.Equal(EdmTypeKind.Collection, property1.Type.Definition.TypeKind);
            Assert.Equal(EdmTypeKind.Complex, (property1.Type.Definition as IEdmCollectionType).ElementType.Definition.TypeKind);
        }