コード例 #1
0
        internal IEdmTypeReference GetEdmType(object instance, Type type)
        {
            IEdmTypeReference edmType;

            IEdmObject edmObject = instance as IEdmObject;

            if (edmObject != null)
            {
                edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName,
                                                 typeof(IEdmObject).Name);
                }
            }
            else
            {
                if (Model == null)
                {
                    throw Error.InvalidOperation(SRResources.RequestMustHaveModel);
                }

                _typeMappingCache = _typeMappingCache ?? Model.GetTypeMappingCache();
                edmType           = _typeMappingCache.GetEdmType(type, Model);
                if (edmType == null)
                {
                    throw Error.InvalidOperation(SRResources.ClrTypeNotInModel, type);
                }
            }

            return(edmType);
        }
コード例 #2
0
        public void IsDeltaResource_ThrowsArgumentNull_Resource()
        {
            // Arrange & Act & Assert
            IEdmObject resource = null;

            ExceptionAssert.ThrowsArgumentNull(() => resource.IsDeltaResource(), "resource");
        }
コード例 #3
0
 /// <summary>
 /// Method to determine whether the current Edm object is a Delta Entry
 /// </summary>
 /// <param name="resource">IEdmObject to be compared</param>
 /// <returns>True or False if type is same as <see cref="EdmDeltaEntityObject"/> or <see cref="EdmDeltaComplexObject"/></returns>
 public static bool IsDeltaResource(this IEdmObject resource)
 {
     if (resource == null)
     {
         throw Error.ArgumentNull("resource");
     }
     return(resource is EdmDeltaEntityObject || resource is EdmDeltaComplexObject);
 }
コード例 #4
0
        public void IsDeltaObject_ThrowsArgumentNull_Type()
        {
            IEdmObject instance = null;

            Assert.ThrowsArgumentNull(
                () => instance.IsDeltaResource(),
                "resource");
        }
コード例 #5
0
        internal IEdmTypeReference GetEdmType(object instance, Type type)
        {
            IEdmTypeReference edmType = null;

            IEdmObject edmObject = instance as IEdmObject;

            if (edmObject != null)
            {
                edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName,
                                                 typeof(IEdmObject).Name);
                }
            }
            else
            {
                if (Model == null)
                {
                    throw Error.InvalidOperation(SRResources.RequestMustHaveModel);
                }

                IEdmModelClrTypeMappingHandler typeMappingHandler = Model.GetAnnotationValue <IEdmModelClrTypeMappingHandler>(Model);
                if (typeMappingHandler != null)
                {
                    edmType = typeMappingHandler.MapClrInstanceToEdmTypeReference(Model, instance);
                }

                if (edmType == null)
                {
                    _typeMappingCache = _typeMappingCache ?? Model.GetTypeMappingCache();
                    edmType           = _typeMappingCache.GetEdmType(type, Model);

                    if (edmType == null)
                    {
                        if (instance != null)
                        {
                            edmType = _typeMappingCache.GetEdmType(instance.GetType(), Model);
                        }

                        if (edmType == null)
                        {
                            throw Error.InvalidOperation(SRResources.ClrTypeNotInModel, type);
                        }
                    }
                    else if (instance != null)
                    {
                        IEdmTypeReference actualType = _typeMappingCache.GetEdmType(instance.GetType(), Model);
                        if (actualType != null && actualType != edmType)
                        {
                            edmType = actualType;
                        }
                    }
                }
            }

            return(edmType);
        }
コード例 #6
0
        internal IEdmTypeReference GetEdmType(object instance, Type type)
        {
            IEdmTypeReference edmType;

            IEdmObject edmObject = instance as IEdmObject;

            if (edmObject != null && instance.GetType().GetProperty("ModelID").GetValue(instance) != null)
            {
                edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName,
                                                 typeof(IEdmObject).Name);
                }
            }
            else
            {
                if (Model == null)
                {
                    throw Error.InvalidOperation(SRResources.RequestMustHaveModel);
                }
                var cacheType = instance.GetType().GetProperty("Items");
                type = cacheType != null?cacheType.GetValue(instance).GetType() : type;

                _typeMappingCache = _typeMappingCache ?? Model.GetTypeMappingCache();
                edmType           = _typeMappingCache.GetEdmType(type, Model);

                if (edmType == null)
                {
                    if (instance != null)
                    {
                        edmType = _typeMappingCache.GetEdmType(instance.GetType(), Model);
                    }

                    if (edmType == null)
                    {
                        throw Error.InvalidOperation(SRResources.ClrTypeNotInModel, type);
                    }
                }
                else if (instance != null)
                {
                    IEdmTypeReference actualType = _typeMappingCache.GetEdmType(instance.GetType(), Model);
                    if (actualType != null && actualType != edmType)
                    {
                        edmType = actualType;
                    }
                }
            }

            return(edmType);
        }
コード例 #7
0
        public static ODataEdmTypeSerializer GetEdmTypeSerializer(this ODataSerializerProvider serializerProvider,
                                                                  object instance, HttpRequestMessage request)
        {
            Contract.Assert(serializerProvider != null);
            Contract.Assert(instance != null);

            IEdmObject edmObject = instance as IEdmObject;

            if (edmObject != null)
            {
                return(serializerProvider.GetEdmTypeSerializer(edmObject.GetEdmType()));
            }

            return(serializerProvider.GetODataPayloadSerializer(instance.GetType(), request) as ODataEdmTypeSerializer);
        }
コード例 #8
0
        /// <summary>
        /// Determine and return the EdmType of given type given the SerializerContext.
        /// </summary>
        /// <param name="context">ODataSerializerContext.</param>
        /// <param name="instance">Value of instance.</param>
        /// <param name="type">Type to determine equivalent EdmType from.</param>
        /// <returns>Equivalent edm type to given type</returns>
        public static IEdmTypeReference GetEdmType(this ODataSerializerContext context, object instance, Type type)
        {
            IEdmTypeReference edmType;

            IEdmObject edmObject = instance as IEdmObject;

            if (edmObject != null)
            {
                edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw new InvalidOperationException("Edm type cannot be null");
                }
            }
            else
            {
                if (context.Model == null)
                {
                    throw new InvalidOperationException("Edm model cannot be null");
                }

                ClrTypeCache typeMappingCache = context.Model.GetTypeMappingCache();
                edmType = typeMappingCache.GetEdmType(type, context.Model);

                if (edmType == null)
                {
                    if (instance != null)
                    {
                        edmType = typeMappingCache.GetEdmType(instance.GetType(), context.Model);
                    }

                    if (edmType == null)
                    {
                        throw new InvalidOperationException("Clr type " + type.FullName + " not in model");
                    }
                }
                else if (instance != null)
                {
                    IEdmTypeReference actualType = typeMappingCache.GetEdmType(instance.GetType(), context.Model);
                    if (actualType != null && actualType != edmType)
                    {
                        edmType = actualType;
                    }
                }
            }

            return(edmType);
        }
コード例 #9
0
        internal IEdmTypeReference GetEdmType(object instance, Type type)
        {
            IEdmTypeReference edmType;

            IEdmObject edmObject = instance as IEdmObject;

            if (edmObject != null)
            {
                edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName,
                                                 typeof(IEdmObject).Name);
                }
            }
            else
            {
                if (Model == null)
                {
                    throw Error.InvalidOperation(SRResources.RequestMustHaveModel);
                }

                edmType = Model.GetEdmTypeReference(type);

                if (edmType == null)
                {
                    if (instance != null)
                    {
                        edmType = Model.GetEdmTypeReference(instance.GetType());
                    }

                    if (edmType == null)
                    {
                        throw Error.InvalidOperation(SRResources.ClrTypeNotInModel, type);
                    }
                }
                else if (instance != null)
                {
                    IEdmTypeReference actualType = Model.GetEdmTypeReference(instance.GetType());
                    if (actualType != null && actualType != edmType)
                    {
                        edmType = actualType;
                    }
                }
            }

            return(edmType);
        }
コード例 #10
0
        private ODataSerializer GetSerializer(Type type, object value, ODataSerializerProvider serializerProvider)
        {
            ODataSerializer serializer;

            IEdmObject edmObject = value as IEdmObject;

            if (edmObject != null)
            {
                IEdmStructuredObject edmStructuredObject = edmObject as IEdmStructuredObject;
                if (edmStructuredObject != null)
                {
                    edmStructuredObject.SetModel(Request.GetModel());
                }

                IEdmTypeReference edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw new SerializationException(Error.Format(SRResources.EdmTypeCannotBeNull,
                                                                  edmObject.GetType().FullName, typeof(IEdmObject).Name));
                }

                serializer = serializerProvider.GetEdmTypeSerializer(edmType);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, edmType.ToTraceString(), typeof(ODataMediaTypeFormatter).Name);
                    throw new SerializationException(message);
                }
            }
            else
            {
                var applyClause = Request.ODataProperties().ApplyClause;
                // get the most appropriate serializer given that we support inheritance.
                if (applyClause == null)
                {
                    type = value == null ? type : value.GetType();
                }

                serializer = serializerProvider.GetODataPayloadSerializer(type, Request);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, type.Name, typeof(ODataMediaTypeFormatter).Name);
                    throw new SerializationException(message);
                }
            }

            return(serializer);
        }
コード例 #11
0
        public static ODataEdmTypeSerializer GetEdmTypeSerializer(this ODataSerializerProvider serializerProvider, IEdmModel model, object instance)
        {
            Contract.Assert(serializerProvider != null);
            Contract.Assert(model != null);
            Contract.Assert(instance != null);

            Contract.Assert(instance != null);

            IEdmObject edmObject = instance as IEdmObject;

            if (edmObject != null)
            {
                return(serializerProvider.GetEdmTypeSerializer(edmObject.GetEdmType()));
            }

            return(serializerProvider.GetODataPayloadSerializer(model, instance.GetType()) as ODataEdmTypeSerializer);
        }
コード例 #12
0
        private IEdmTypeReference GetEdmType(object instance, Type type)
        {
            IEdmTypeReference edmType = null;

            IEdmObject edmObject = instance as IEdmObject;

            if (edmObject != null)
            {
                edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw new InvalidCastException(nameof(instance));
                }
            }

            return(edmType);
        }
コード例 #13
0
        private bool TryValidateRequest(IEdmObject entity, ODataPath path, Func <ODataPath, bool> canExecute, out HttpResponseMessage errorResponse)
        {
            if (entity == null)
            {
                errorResponse = this.Request.CreateBadRequestResponse();
                return(false);
            }

            if (!canExecute(path))
            {
                errorResponse = this.Request.CreateMethodNotAllowedResponse();
                return(false);
            }

            errorResponse = null;
            return(true);
        }
コード例 #14
0
        internal static IODataSerializer GetSerializer(Type type, object value, HttpRequest request,
                                                       IODataSerializerProvider serializerProvider)
        {
            IODataSerializer serializer;

            IEdmObject edmObject = value as IEdmObject;

            if (edmObject != null)
            {
                IEdmTypeReference edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw new SerializationException(Error.Format(SRResources.EdmTypeCannotBeNull,
                                                                  edmObject.GetType().FullName, typeof(IEdmObject).Name));
                }

                serializer = serializerProvider.GetEdmTypeSerializer(edmType);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, edmType.ToTraceString());
                    throw new SerializationException(message);
                }
            }
            else
            {
                var applyClause = request.ODataFeature().ApplyClause;

                // get the most appropriate serializer given that we support inheritance.
                if (applyClause == null)
                {
                    type = value == null ? type : value.GetType();
                }

                serializer = serializerProvider.GetODataPayloadSerializer(type, request);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, type.Name);
                    throw new SerializationException(message);
                }
            }

            return(serializer);
        }
コード例 #15
0
ファイル: ODataJsonSerializer.cs プロジェクト: bigfont/WebApi
        private ODataSerializer GetSerializer(Type type, object value, IEdmModel model, ODataSerializerProvider serializerProvider)
        {
            ODataSerializer serializer;

            IEdmObject edmObject = value as IEdmObject;

            if (edmObject != null)
            {
                IEdmTypeReference edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw new SerializationException(Error.Format(SRResources.EdmTypeCannotBeNull,
                                                                  edmObject.GetType().FullName, typeof(IEdmObject).Name));
                }

                serializer = serializerProvider.GetEdmTypeSerializer(edmType);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, edmType.ToTraceString(), typeof(ODataJsonSerializer).Name);
                    throw new SerializationException(message);
                }
            }
            else
            {
                // TODO: Support $apply
                // Currently don't support $apply
                //var applyClause = Request.ODataProperties().ApplyClause;
                //// get the most appropriate serializer given that we support inheritance.
                //if (applyClause == null)
                //{
                //	type = value == null ? type : value.GetType();
                //}

                serializer = serializerProvider.GetODataPayloadSerializer(model, type, Request);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, type.Name, typeof(ODataJsonSerializer).Name);
                    throw new SerializationException(message);
                }
            }

            return(serializer);
        }
コード例 #16
0
        public void Read_ReturnsEdmComplexObjectCollection_TypelessMode()
        {
            // Arrange
            HttpContent content = new StringContent("{ 'value': [ { 'City' : 'Redmond' } ] }");

            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            IODataRequestMessage request = new ODataMessageWrapper(content.ReadAsStreamAsync().Result, content.Headers);
            ODataMessageReader   reader  = new ODataMessageReader(request, new ODataMessageReaderSettings(), _model);
            var deserializer             = new ODataCollectionDeserializer(_addressCollectionType, new DefaultODataDeserializerProvider());

            // Act
            var result = deserializer.Read(reader, new ODataDeserializerContext {
                Model = _model, ResourceType = typeof(IEdmObject)
            });

            // Assert
            IEdmObject edmObject = Assert.IsType <EdmComplexObjectCollection>(result);

            Assert.Equal(_addressCollectionType, edmObject.GetEdmType());
        }
コード例 #17
0
        // Analyzes incoming type, then uses Serializer Provider through functions to return appropriate serializer
        private static ODataSerializer GetSerializer(Type type, object value, HttpRequest internalRequest, Func <IEdmTypeReference, ODataSerializer> getEdmTypeSerializer, Func <Type, ODataSerializer> getODataPayloadSerializer)
        {
            ODataSerializer serializer;

            IEdmObject edmObject = value as IEdmObject;

            if (edmObject != null)
            {
                IEdmTypeReference edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw new SerializationException("Edm type " + edmObject.GetType().FullName + " cannot be null");
                }

                serializer = getEdmTypeSerializer(edmType);
                if (serializer == null)
                {
                    string message = "Type " + edmType.ToTraceString() + " cannot be serialized";
                    throw new SerializationException(message);
                }
            }
            else
            {
                var applyClause = internalRequest.ODataFeature()?.ApplyClause;

                // get the most appropriate serializer given that we support inheritance.
                if (applyClause == null)
                {
                    type = value == null ? type : value.GetType();
                }

                serializer = getODataPayloadSerializer(type);
                if (serializer == null)
                {
                    string message = "Type " + type.Name + " cannot be serialized";
                    throw new SerializationException(message);
                }
            }

            return(serializer);
        }
コード例 #18
0
        private static ODataSerializer GetSerializer(Type type, object value, IWebApiRequestMessage internalRequest, Func <IEdmTypeReference, ODataSerializer> getEdmTypeSerializer, Func <Type, ODataSerializer> getODataPayloadSerializer)
        {
            ODataSerializer serializer;

            IEdmObject edmObject = value as IEdmObject;

            if (edmObject != null)
            {
                IEdmTypeReference edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw new SerializationException(Error.Format(SRResources.EdmTypeCannotBeNull,
                                                                  edmObject.GetType().FullName, typeof(IEdmObject).Name));
                }

                serializer = getEdmTypeSerializer(edmType);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, edmType.ToTraceString());
                    throw new SerializationException(message);
                }
            }
            else
            {
                var applyClause = internalRequest.Context.ApplyClause;
                // get the most appropriate serializer given that we support inheritance.
                if (applyClause == null)
                {
                    type = value == null ? type : value.GetType();
                }

                serializer = getODataPayloadSerializer(type);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, type.Name);
                    throw new SerializationException(message);
                }
            }

            return(serializer);
        }
コード例 #19
0
        private ODataSerializer GetSerializer(Type type, object value, IEdmModel model, IODataSerializerProvider serializerProvider)
        {
            ODataSerializer serializer;

            IEdmObject edmObject = value as IEdmObject;

            if (edmObject != null)
            {
                IEdmTypeReference edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw new SerializationException("Microsoft.OData.Edm.Error.Format(SRResources.EdmTypeCannotBeNull,edmObject.GetType().FullName, typeof(IEdmObject).Name");
                }

                serializer = serializerProvider.GetEdmTypeSerializer(Request.HttpContext, edmType);
                if (serializer == null)
                {
                    string message = "Microsoft.OData.Edm.Error.Format(SRResources.TypeCannotBeSerialized, edmType.ToTraceString(), typeof(ODataJsonSerializer).Name)";
                    throw new SerializationException(message);
                }
            }
            else
            {
                //var applyClause = Request.ODataFeature().ApplyClause;
                //// get the most appropriate serializer given that we support inheritance.
                //if (applyClause == null)
                //{
                //    type = value?.GetType() ?? type;
                //}

                serializer = serializerProvider.GetODataPayloadSerializer(Request.HttpContext, type);
                if (serializer == null)
                {
                    string message = "Microsoft.OData.Edm.Error.Format(SRResources.TypeCannotBeSerialized, type.Name, typeof(ODataJsonSerializer).Name)";
                    throw new SerializationException(message);
                }
            }

            return(serializer);
        }
コード例 #20
0
ファイル: ODataOutputFormatter.cs プロジェクト: maxr96/odata
        private ODataSerializer GetSerializer(Type type, object value, OutputFormatterWriteContext context)
        {
            ODataSerializer serializer;

            IODataSerializerProvider serviceProvider = context.HttpContext.RequestServices.GetRequiredService <IODataSerializerProvider>();

            IEdmObject edmObject = value as IEdmObject;

            if (edmObject != null)
            {
                IEdmTypeReference edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw new SerializationException(Error.Format(SRResources.EdmTypeCannotBeNull,
                                                                  edmObject.GetType().FullName, typeof(IEdmObject).Name));
                }

                serializer = serviceProvider.GetEdmTypeSerializer(context.HttpContext, edmType);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, edmType.ToTraceString(), typeof(ODataOutputFormatter).Name);
                    throw new SerializationException(message);
                }
            }
            else
            {
                // get the most appropriate serializer given that we support inheritance.
                type       = value == null ? type : value.GetType();
                serializer = serviceProvider.GetODataPayloadSerializer(context.HttpContext, type);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, type.Name, typeof(ODataOutputFormatter).Name);
                    throw new SerializationException(message);
                }
            }

            return(serializer);
        }
コード例 #21
0
        private static ODataSerializer GetSerializer(Type type, object value, IEdmModel model, ODataSerializerProvider serializerProvider)
        {
            ODataSerializer serializer;

            IEdmObject edmObject = value as IEdmObject;

            if (edmObject != null)
            {
                IEdmTypeReference edmType = edmObject.GetEdmType();
                if (edmType == null)
                {
                    throw new SerializationException(
                              Error.Format(SRResources.EdmTypeCannotBeNull, type.FullName, typeof(IEdmObject).Name));
                }

                serializer = serializerProvider.GetEdmTypeSerializer(edmType);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, edmType.ToTraceString(), typeof(ODataMediaTypeFormatter).Name);
                    throw new SerializationException(message);
                }
            }
            else
            {
                // get the most appropriate serializer given that we support inheritance.
                type       = value == null ? type : value.GetType();
                serializer = serializerProvider.GetODataPayloadSerializer(model, type);
                if (serializer == null)
                {
                    string message = Error.Format(SRResources.TypeCannotBeSerialized, type.Name, typeof(ODataMediaTypeFormatter).Name);
                    throw new SerializationException(message);
                }
            }

            return(serializer);
        }
コード例 #22
0
        private static IEdmEntityTypeReference GetTypeReference(IEdmModel model, IEdmEntityType edmType, object value)
        {
            if (model == null || edmType == null || value == null)
            {
                return(null);
            }

            IEdmObject edmObject = value as IEdmEntityObject;

            if (edmObject != null)
            {
                IEdmTypeReference edmTypeReference = edmObject.GetEdmType();
                return(edmTypeReference.AsEntity());
            }

            IEdmTypeReference reference = EdmLibHelpers.GetEdmTypeReference(model, value.GetType());

            if (reference != null && reference.Definition.IsOrInheritsFrom(edmType))
            {
                return((IEdmEntityTypeReference)reference);
            }

            return(null);
        }
コード例 #23
0
        /// <summary>
        /// Method to determine whether the current Edm object is a Delta resource
        /// </summary>
        /// <param name="resource">IEdmObject to be compared</param>
        /// <returns>True or False if type is same as <see cref="EdmDeltaResourceObject"/> or <see cref="EdmDeltaComplexObject"/></returns>
        public static bool IsDeltaResource(this IEdmObject resource)
        {
            if (resource == null)
            {
                throw Error.ArgumentNull(nameof(resource));
            }

            TypedEdmEntityObject obj = resource as TypedEdmEntityObject;

            if (obj != null)
            {
                if (obj.Instance is IDeltaSetItem)
                {
                    return(true);
                }
            }

            if (resource is IDeltaSetItem)
            {
                return(true);
            }

            return(resource is EdmDeltaResourceObject || resource is EdmDeltaComplexObject);
        }