public void GetPropertyValue_ThrowsInvalidOperation_IfEdmObjectIsNull()
        {
            // Arrange
            ResourceContext instanceContext = new ResourceContext();

            // Act & Assert
            ExceptionAssert.Throws <InvalidOperationException>(
                () => instanceContext.GetPropertyValue("SomeProperty"),
                "The property 'EdmObject' of ResourceContext cannot be null.");
        }
        public void GetPropertyValue_ThrowsInvalidOperation_IfPropertyIsNotFound()
        {
            // Arrange
            IEdmEntityTypeReference     entityType = new EdmEntityTypeReference(new EdmEntityType("NS", "Name"), isNullable: false);
            Mock <IEdmStructuredObject> edmObject  = new Mock <IEdmStructuredObject>();

            edmObject.Setup(o => o.GetEdmType()).Returns(entityType);
            ResourceContext instanceContext = new ResourceContext(_serializerContext, entityType, edmObject.Object);

            // Act & Assert
            ExceptionAssert.Throws <InvalidOperationException>(
                () => instanceContext.GetPropertyValue("NotPresentProperty"),
                "The EDM instance of type '[NS.Name Nullable=False]' is missing the property 'NotPresentProperty'.");
        }
Exemple #3
0
        private static object GetKeyValue(IEdmProperty key, ResourceContext resourceContext)
        {
            Contract.Assert(key != null);
            Contract.Assert(resourceContext != null);

            object value = resourceContext.GetPropertyValue(key.Name);

            if (value == null)
            {
                IEdmTypeReference edmType = resourceContext.EdmObject.GetEdmType();
                throw Error.InvalidOperation(SRResources.KeyValueCannotBeNull, key.Name, edmType.Definition);
            }

            return(ConvertValue(value));
        }
        private void WriteComplexAndExpandedNavigationProperty(IEdmProperty edmProperty, SelectExpandClause selectExpandClause,
                                                               ResourceContext resourceContext,
                                                               ODataWriter writer)
        {
            Contract.Assert(edmProperty != null);
            Contract.Assert(resourceContext != null);
            Contract.Assert(writer != null);

            object propertyValue = resourceContext.GetPropertyValue(edmProperty.Name);

            if (propertyValue == null || propertyValue is NullEdmComplexObject)
            {
                if (edmProperty.Type.IsCollection())
                {
                    // A complex or navigation property whose Type attribute specifies a collection, the collection always exists,
                    // it may just be empty.
                    // If a collection of complex or entities can be related, it is represented as a JSON array. An empty
                    // collection of resources (one that contains no resource) is represented as an empty JSON array.
                    writer.WriteStart(new ODataResourceSet
                    {
                        TypeName = edmProperty.Type.FullName()
                    });
                }
                else
                {
                    // If at most one resource can be related, the value is null if no resource is currently related.
                    writer.WriteStart(resource: null);
                }

                writer.WriteEnd();
            }
            else
            {
                // create the serializer context for the complex and expanded item.
                ODataSerializerContext nestedWriteContext = new ODataSerializerContext(resourceContext, selectExpandClause, edmProperty);

                // write object.
                ODataEdmTypeSerializer serializer = SerializerProvider.GetEdmTypeSerializer(resourceContext.Context, edmProperty.Type);
                if (serializer == null)
                {
                    throw new SerializationException(
                              Error.Format(SRResources.TypeCannotBeSerialized, edmProperty.Type.ToTraceString(), typeof(ODataResourceSerializer).Name));
                }

                serializer.WriteObjectInline(propertyValue, edmProperty.Type, writer, nestedWriteContext);
            }
        }
        public void GetPropertyValue_ThrowsInvalidOperation_IfEdmObjectGetEdmTypeReturnsNull()
        {
            // Arrange
            object outObject;
            Mock <IEdmEntityObject> mock = new Mock <IEdmEntityObject>();

            mock.Setup(o => o.TryGetPropertyValue(It.IsAny <string>(), out outObject)).Returns(false).Verifiable();
            mock.Setup(o => o.GetEdmType()).Returns <IEdmTypeReference>(null).Verifiable();
            ResourceContext context = new ResourceContext();

            context.EdmObject = mock.Object;

            // Act & Assert
            ExceptionAssert.Throws <InvalidOperationException>(() => context.GetPropertyValue("SomeProperty"),
                                                               "The EDM type of an IEdmObject cannot be null.", partialMatch: true);
            mock.Verify();
        }
        /// <summary>
        /// Creates the <see cref="ODataProperty"/> to be written for the given entity and the structural property.
        /// </summary>
        /// <param name="structuralProperty">The EDM structural property being written.</param>
        /// <param name="resourceContext">The context for the entity instance being written.</param>
        /// <returns>The <see cref="ODataProperty"/> to write.</returns>
        public virtual ODataProperty CreateStructuralProperty(IEdmStructuralProperty structuralProperty, ResourceContext resourceContext)
        {
            if (structuralProperty == null)
            {
                throw Error.ArgumentNull("structuralProperty");
            }
            if (resourceContext == null)
            {
                throw Error.ArgumentNull("resourceContext");
            }

            ODataSerializerContext writeContext = resourceContext.SerializerContext;

            ODataEdmTypeSerializer serializer = SerializerProvider.GetEdmTypeSerializer(resourceContext.Context, structuralProperty.Type);

            if (serializer == null)
            {
                throw new SerializationException(
                          Error.Format(SRResources.TypeCannotBeSerialized, structuralProperty.Type.FullName(), typeof(ODataOutputFormatter).Name));
            }

            object propertyValue = resourceContext.GetPropertyValue(structuralProperty.Name);

            IEdmTypeReference propertyType = structuralProperty.Type;

            if (propertyValue != null)
            {
                if (!propertyType.IsPrimitive() && !propertyType.IsEnum())
                {
                    IEdmTypeReference actualType = writeContext.GetEdmType(propertyValue, propertyValue.GetType());
                    if (propertyType != null && propertyType != actualType)
                    {
                        propertyType = actualType;
                    }
                }
            }

            return(serializer.CreateProperty(propertyValue, propertyType, structuralProperty.Name, writeContext));
        }
        private static EntityTagHeaderValue CreateETag(ResourceContext resourceContext, IETagHandler handler)
        {
            IEdmModel model = resourceContext.EdmModel;

            IEnumerable <IEdmStructuralProperty> concurrencyProperties;

            if (model != null && resourceContext.NavigationSource != null)
            {
                concurrencyProperties = model.GetConcurrencyProperties(resourceContext.NavigationSource).OrderBy(c => c.Name);
            }
            else
            {
                concurrencyProperties = Enumerable.Empty <IEdmStructuralProperty>();
            }

            IDictionary <string, object> properties = new Dictionary <string, object>();

            foreach (IEdmStructuralProperty etagProperty in concurrencyProperties)
            {
                properties.Add(etagProperty.Name, resourceContext.GetPropertyValue(etagProperty.Name));
            }
            return(handler.CreateETag(properties));
        }
        /// <summary>
        /// Creates the ETag for the given entity.
        /// </summary>
        /// <param name="resourceContext">The context for the resource instance being written.</param>
        /// <returns>The created ETag.</returns>
        public virtual string CreateETag(ResourceContext resourceContext)
        {
            if (resourceContext.Request != null)
            {
                HttpContext httpContext = resourceContext.Request.HttpContext;
                if (httpContext == null)
                {
                    throw Error.InvalidOperation("TODO: " /*SRResources.RequestMustContainConfiguration*/);
                }

                IEdmModel     model     = resourceContext.EdmModel;
                IEdmEntitySet entitySet = resourceContext.NavigationSource as IEdmEntitySet;

                IEnumerable <IEdmStructuralProperty> concurrencyProperties;
                if (model != null && entitySet != null)
                {
                    concurrencyProperties = model.GetConcurrencyProperties(entitySet).OrderBy(c => c.Name);
                }
                else
                {
                    concurrencyProperties = Enumerable.Empty <IEdmStructuralProperty>();
                }

                IDictionary <string, object> properties = new Dictionary <string, object>();
                foreach (IEdmStructuralProperty etagProperty in concurrencyProperties)
                {
                    properties.Add(etagProperty.Name, resourceContext.GetPropertyValue(etagProperty.Name));
                }
                EntityTagHeaderValue etagHeaderValue = httpContext.RequestServices.GetRequiredService <IETagHandler>().CreateETag(properties);
                if (etagHeaderValue != null)
                {
                    return(etagHeaderValue.ToString());
                }
            }

            return(null);
        }