Example #1
0
        public virtual void Apply(ODataComplexValue complexValue, ODataSerializationFeatureContext context)
        {
            Arg.NotNull(complexValue, nameof(complexValue));
            Arg.NotNull(context, nameof(context));

            var model                  = context.SerializerContext.Model;
            var complexType            = GetStructuredType(context.EdmElement);
            var complexTypeAnnotations = model.GetAnnotationValue <HashSet <InstanceAnnotation> >(complexType);

            if (complexTypeAnnotations != null)
            {
                AddAnnotations(context, complexTypeAnnotations, complexValue.InstanceAnnotations);
            }

            foreach (var property in complexValue.Properties)
            {
                var propertyType = complexType.FindProperty(property.Name);

                // likely a dynamic property on open type
                if (propertyType == null)
                {
                    continue;
                }

                var propertyAnnotations = model.GetAnnotationValue <HashSet <InstanceAnnotation> >(propertyType);

                if (propertyAnnotations != null)
                {
                    AddAnnotations(context, propertyAnnotations, property.InstanceAnnotations);
                }
            }
        }
        public override ODataFeed CreateODataFeed(IEnumerable feedInstance, IEdmCollectionTypeReference feedType, ODataSerializerContext writeContext)
        {
            Contract.Assume(writeContext != null);

            var feed      = base.CreateODataFeed(feedInstance, feedType, writeContext);
            var entitySet = writeContext.Model.EntityContainer.FindEntitySet(writeContext.NavigationSource.Name);

            // likely a "contained" entity, which will not have a corresponding entity set
            if (entitySet == null)
            {
                return(feed);
            }

            var context = new ODataSerializationFeatureContext(entitySet, writeContext, ComplexTypeSerializer)
            {
                Instance = feedInstance
            };

            foreach (var feature in SerializationFeatures)
            {
                feature.Apply(feed, context);
            }

            return(feed);
        }
Example #3
0
        private static void AddAnnotations(ODataSerializationFeatureContext context, IEnumerable <InstanceAnnotation> modelAnnotations, ICollection <ODataInstanceAnnotation> instanceAnnotations)
        {
            Contract.Requires(context != null);
            Contract.Requires(modelAnnotations != null);
            Contract.Requires(instanceAnnotations != null);

            var instance = context.Instance;
            var model    = context.SerializerContext.Model;
            var complexTypeSerializer = context.ComplexTypeSerializer;
            var serializerContext     = context.SerializerContext;

            foreach (var modelAnnotation in modelAnnotations)
            {
                var annotation = modelAnnotation.GetValue(instance);

                if (annotation == null)
                {
                    continue;
                }

                var value = modelAnnotation.IsComplex ?
                            GetComplexValue(modelAnnotation, annotation, model, complexTypeSerializer, serializerContext) :
                            GetPrimitiveValue(modelAnnotation, annotation, model);

                if (value == null)
                {
                    continue;
                }

                var instanceAnnotation = new ODataInstanceAnnotation(modelAnnotation.QualifiedName, value);
                instanceAnnotations.Add(instanceAnnotation);
            }
        }
Example #4
0
        public virtual void Apply(ODataFeed feed, ODataSerializationFeatureContext context)
        {
            Arg.NotNull(feed, nameof(feed));
            Arg.NotNull(context, nameof(context));

            var model       = context.SerializerContext.Model;
            var annotations = model.GetAnnotationValue <HashSet <InstanceAnnotation> >(context.EdmElement);

            if (annotations != null)
            {
                AddAnnotations(context, annotations, feed.InstanceAnnotations);
            }
        }
        public virtual void Apply(ODataEntry entry, ODataSerializationFeatureContext context)
        {
            Arg.NotNull(entry, nameof(entry));
            Arg.NotNull(context, nameof(context));

            var entityContext = context.EntityInstanceContext;
            var entityType    = entityContext.EntityType;

            if (!entityType.HasStream)
            {
                return;
            }

            if (entityContext.SerializerContext.MetadataLevel == NoMetadata)
            {
                return;
            }

            var instance = entityContext.TryGetEntityInstance();

            if (instance == null)
            {
                return;
            }

            var annotation = entityContext.EdmModel.GetAnnotationValue <MediaLinkEntryAnnotation>(entityType);

            if (annotation == null)
            {
                return;
            }

            var contentType = annotation.GetContentType(instance);
            var readLink    = annotation.GenerateReadLink?.Invoke(entityContext);
            var editLink    = annotation.GenerateEditLink?.Invoke(entityContext);

            if (string.IsNullOrEmpty(contentType) || (readLink == null && editLink == null))
            {
                return;
            }

            var mediaResource = new ODataStreamReferenceValue()
            {
                ContentType = contentType,
                ReadLink    = readLink,
                EditLink    = editLink,
                ETag        = annotation.GenerateETag?.Invoke(entityContext)
            };

            entry.MediaResource = mediaResource;
        }
Example #6
0
        /// <summary>
        /// Creates and returns an OData complex type value using the specified object graph, tpye, and context.
        /// </summary>
        /// <param name="graph">The object graph to create a complex value for.</param>
        /// <param name="complexType">The type of complex type.</param>
        /// <param name="writeContext">The current <see cref="ODataSerializerContext">serializer context</see>.</param>
        /// <returns>The created <see cref="ODataComplexValue">complex value</see>.</returns>
        public override ODataComplexValue CreateODataComplexValue(object graph, IEdmComplexTypeReference complexType, ODataSerializerContext writeContext)
        {
            var complexValue = base.CreateODataComplexValue(graph, complexType, writeContext);
            var context      = new ODataSerializationFeatureContext(complexType, writeContext, ComplexTypeSerializer)
            {
                Instance = graph
            };

            foreach (var feature in SerializationFeatures)
            {
                feature.Apply(complexValue, context);
            }

            return(complexValue);
        }
Example #7
0
        public override ODataEntry CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext)
        {
            Contract.Assume(entityInstanceContext != null);

            var entry   = base.CreateEntry(selectExpandNode, entityInstanceContext);
            var context = new ODataSerializationFeatureContext(entityInstanceContext.EntityType, entityInstanceContext.SerializerContext, ComplexTypeSerializer)
            {
                Instance = entityInstanceContext.TryGetEntityInstance(),
                EntityInstanceContext = entityInstanceContext,
                SelectExpandNode      = selectExpandNode
            };

            foreach (var feature in SerializationFeatures)
            {
                feature.Apply(entry, context);
            }

            return(entry);
        }
Example #8
0
        public virtual void Apply(ODataEntry entry, ODataSerializationFeatureContext context)
        {
            Arg.NotNull(entry, nameof(entry));
            Arg.NotNull(context, nameof(context));

            // note: currently no way to add annotations inside a projection (e.g. $select)
            if (context.Instance == null)
            {
                return;
            }

            var model            = context.SerializerContext.Model;
            var entityType       = GetStructuredType(context.EdmElement);
            var entryAnnotations = model.GetAnnotationValue <HashSet <InstanceAnnotation> >(entityType);

            if (entryAnnotations != null)
            {
                AddAnnotations(context, entryAnnotations, entry.InstanceAnnotations);
            }

            foreach (var property in entry.Properties)
            {
                var propertyType = entityType.FindProperty(property.Name);

                // likely a dynamic property on open type
                if (propertyType == null)
                {
                    continue;
                }

                var propertyAnnotations = model.GetAnnotationValue <HashSet <InstanceAnnotation> >(propertyType);

                if (propertyAnnotations != null)
                {
                    AddAnnotations(context, propertyAnnotations, property.InstanceAnnotations);
                }
            }
        }
 void IODataSerializationFeature.Apply(ODataComplexValue complexValue, ODataSerializationFeatureContext context)
 {
 }
 void IODataSerializationFeature.Apply(ODataFeed feed, ODataSerializationFeatureContext context)
 {
 }
 void IODataSerializationFeature.Apply(ODataFeed feed, ODataSerializationFeatureContext context)
 {
     Contract.Requires <ArgumentNullException>(feed != null, nameof(feed));
     Contract.Requires <ArgumentNullException>(context != null, nameof(context));
 }
 void IODataSerializationFeature.Apply(ODataComplexValue complexValue, ODataSerializationFeatureContext context)
 {
     Contract.Requires <ArgumentNullException>(complexValue != null, nameof(complexValue));
     Contract.Requires <ArgumentNullException>(context != null, nameof(context));
 }
 void IODataSerializationFeature.Apply(ODataEntry entry, ODataSerializationFeatureContext context)
 {
     Contract.Requires <ArgumentNullException>(entry != null, nameof(entry));
     Contract.Requires <ArgumentNullException>(context != null, nameof(context));
 }