예제 #1
0
        /// <inheritdoc />
        public virtual ODataSerializer GetODataPayloadSerializer(HttpContext context, Type type)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }

            IServiceProvider provider = context.RequestServices;
            var x = context.Response.StatusCode;

            // handle the special types.
            if (type == typeof(ODataServiceDocument))
            {
                return(provider.GetRequiredService <ODataServiceDocumentSerializer>());
            }
            else if (type == typeof(Uri) || type == typeof(ODataEntityReferenceLink))
            {
                return(provider.GetRequiredService <ODataEntityReferenceLinkSerializer>());
            }
            else if (typeof(IEnumerable <Uri>).IsAssignableFrom(type) || type == typeof(ODataEntityReferenceLinks))
            {
                return(provider.GetRequiredService <ODataEntityReferenceLinksSerializer>());
            }
            else if (type == typeof(ODataError) || type == typeof(SerializableError))
            {
                return(provider.GetRequiredService <ODataErrorSerializer>());
            }
            else if (typeof(IEdmModel).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
            {
                return(provider.GetRequiredService <ODataMetadataSerializer>());
            }

            // if it is not a special type, assume it has a corresponding EdmType.
            IEdmModel         model            = context.ODataFeature().Model;
            ClrTypeCache      typeMappingCache = model.GetTypeMappingCache();
            IEdmTypeReference edmType          = typeMappingCache.GetEdmType(type, model);

            if (edmType != null)
            {
                if (((edmType.IsPrimitive() || edmType.IsEnum()) &&
                     ODataRawValueMediaTypeMapping.IsRawValueRequest(context)) ||
                    ODataCountMediaTypeMapping.IsCountRequest(context))
                {
                    return(provider.GetRequiredService <ODataRawValueSerializer>());
                }
                else
                {
                    return(GetEdmTypeSerializer(context, edmType));
                }
            }
            else
            {
                return(null);
            }
        }
        /// <inheritdoc />
        public override ODataSerializer GetODataPayloadSerializer(IEdmModel model, Type type, HttpRequestMessage request)
        {
            if (model == null)
            {
                throw Error.ArgumentNull("model");
            }
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            // handle the special types.
            if (type == typeof(ODataServiceDocument))
            {
                return(_workspaceSerializer);
            }
            else if (type == typeof(Uri) || type == typeof(ODataEntityReferenceLink))
            {
                return(_entityReferenceLinkSerializer);
            }
            else if (typeof(IEnumerable <Uri>).IsAssignableFrom(type) || type == typeof(ODataEntityReferenceLinks))
            {
                return(_entityReferenceLinksSerializer);
            }
            else if (type == typeof(ODataError) || type == typeof(HttpError))
            {
                return(_errorSerializer);
            }
            else if (typeof(IEdmModel).IsAssignableFrom(type))
            {
                return(_metadataSerializer);
            }

            // if it is not a special type, assume it has a corresponding EdmType.
            ClrTypeCache      typeMappingCache = model.GetTypeMappingCache();
            IEdmTypeReference edmType          = typeMappingCache.GetEdmType(type, model);

            if (edmType != null)
            {
                if (((edmType.IsPrimitive() || edmType.IsEnum()) &&
                     ODataRawValueMediaTypeMapping.IsRawValueRequest(request)) ||
                    ODataCountMediaTypeMapping.IsCountRequest(request))
                {
                    return(_rawValueSerializer);
                }
                else
                {
                    return(GetEdmTypeSerializer(edmType));
                }
            }
            else
            {
                return(null);
            }
        }
        public void TryMatchMediaType_DoesntMatchRequest_WithNonODataRequest(ODataRawValueMediaTypeMapping mapping)
        {
            // Arrange
            var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/");

            // Act
            double mapResult = mapping.TryMatchMediaType(request);

            // Assert
            Assert.Equal(0, mapResult);
        }
 public void TryMatchMediaType_ThrowsArgumentNull_WhenRequestIsNull(ODataRawValueMediaTypeMapping mapping)
 {
     // Arrange, Act & Assert
     ExceptionAssert.ThrowsArgumentNull(() => { mapping.TryMatchMediaType(null); }, "request");
 }