示例#1
0
        /// <summary>
        /// Gets an entity metadata builder for the given entry.
        /// </summary>
        /// <param name="entryState">Entry state to use as reference for information needed by the builder.</param>
        /// <returns>An entity metadata builder.</returns>
        public ODataEntityMetadataBuilder GetEntityMetadataBuilderForReader(IODataJsonLightReaderEntryState entryState)
        {
            Debug.Assert(entryState != null, "entry != null");

            // Only apply the conventional template builder on response. On a request we would only report what's on the wire.
            if (entryState.MetadataBuilder == null)
            {
                ODataEntry entry = entryState.Entry;
                if (this.isResponse)
                {
                    ODataTypeAnnotation typeAnnotation = entry.GetAnnotation <ODataTypeAnnotation>();

                    Debug.Assert(typeAnnotation != null, "The JSON light reader should have already set the ODataTypeAnnotation.");
                    IEdmNavigationSource navigationSource = typeAnnotation.NavigationSource;

                    IEdmEntityType navigationSourceElementType         = this.edmTypeResolver.GetElementType(navigationSource);
                    IODataFeedAndEntryTypeContext typeContext          = ODataFeedAndEntryTypeContext.Create(/*serializationInfo*/ null, navigationSource, navigationSourceElementType, entryState.EntityType, this.model, /*throwIfMissingTypeInfo*/ true);
                    IODataEntryMetadataContext    entryMetadataContext = ODataEntryMetadataContext.Create(entry, typeContext, /*serializationInfo*/ null, (IEdmEntityType)entry.GetEdmType().Definition, this, entryState.SelectedProperties);

                    UrlConvention urlConvention            = UrlConvention.ForUserSettingAndTypeContext(/*keyAsSegment*/ null, typeContext);
                    ODataConventionalUriBuilder uriBuilder = new ODataConventionalUriBuilder(this.ServiceBaseUri, urlConvention);

                    entryState.MetadataBuilder = new ODataConventionalEntityMetadataBuilder(entryMetadataContext, this, uriBuilder);
                }
                else
                {
                    entryState.MetadataBuilder = new NoOpEntityMetadataBuilder(entry);
                }
            }

            return(entryState.MetadataBuilder);
        }
示例#2
0
        private OePropertyAccessor(IEdmProperty edmProperty, Func <Object, Object> accessor)
        {
            _name        = edmProperty.Name;
            _accessor    = accessor;
            _edmProperty = edmProperty;

            if (edmProperty.DeclaringType == PrimitiveTypeHelper.TupleEdmType)
            {
                _typeAnnotation = new ODataTypeAnnotation(edmProperty.Type.ShortQualifiedName());
            }
        }
示例#3
0
        /// <summary>
        /// Gets the EDM type of an OData instance from the <see cref="ODataTypeAnnotation"/> of the instance (if available).
        /// </summary>
        /// <param name="annotatable">The OData instance to get the EDM type for.</param>
        /// <returns>The EDM type of the <paramref name="annotatable"/> if available in the <see cref="ODataTypeAnnotation"/> annotation.</returns>
        internal static IEdmTypeReference GetEdmType(this ODataAnnotatable annotatable)
        {
            if (annotatable == null)
            {
                return(null);
            }

            ODataTypeAnnotation typeAnnotation = annotatable.GetAnnotation <ODataTypeAnnotation>();

            return(typeAnnotation == null ? null : typeAnnotation.Type);
        }
示例#4
0
        /// <summary>
        /// Gets the EDM type of an OData instance from the <see cref="ODataTypeAnnotation"/> of the instance (if available).
        /// </summary>
        /// <param name="annotatable">The OData instance to get the EDM type for.</param>
        /// <returns>The EDM type of the <paramref name="annotatable"/> if available in the <see cref="ODataTypeAnnotation"/> annotation.</returns>
        internal static IEdmTypeReference GetEdmType(this ODataAnnotatable annotatable)
        {
            DebugUtils.CheckNoExternalCallers();

            if (annotatable == null)
            {
                return(null);
            }

            ODataTypeAnnotation typeAnnotation = annotatable.GetAnnotation <ODataTypeAnnotation>();

            return(typeAnnotation == null ? null : typeAnnotation.Type);
        }
        public void AddTypeNameAnnotationAsNeeded_AddsAnnotation_InJsonLightMetadataMode()
        {
            // Arrange
            IEdmPrimitiveTypeReference edmPrimitiveType = typeof(short).GetEdmPrimitiveTypeReference();
            ODataPrimitiveValue        primitive        = new ODataPrimitiveValue((short)1);

            // Act
            ODataPrimitiveSerializer.AddTypeNameAnnotationAsNeeded(primitive, edmPrimitiveType, ODataMetadataLevel.Full);

            // Assert
            ODataTypeAnnotation annotation = primitive.TypeAnnotation;

            Assert.NotNull(annotation); // Guard
            Assert.Equal("Edm.Int16", annotation.TypeName);
        }
示例#6
0
        public void AddTypeNameAnnotationAsNeeded_DoesNotAddAnnotation()
        {
            // Arrange
            ODataEnumValue        enumValue = new ODataEnumValue("value");
            IEdmEnumTypeReference enumType  = new EdmEnumTypeReference(
                new EdmEnumType("TestModel", "EnumType"), isNullable: false);

            // Act
            ODataEnumSerializer.AddTypeNameAnnotationAsNeeded(enumValue, enumType, ODataMetadataLevel.Minimal);

            // Assert
            ODataTypeAnnotation annotation = enumValue.TypeAnnotation;

            Assert.Null(annotation);
        }
示例#7
0
        public void AddTypeNameAnnotationAsNeeded_AddAnnotation_InFullMetadataMode()
        {
            // Arrange
            ODataEnumValue        enumValue = new ODataEnumValue("value");
            IEdmEnumTypeReference enumType  = new EdmEnumTypeReference(
                new EdmEnumType("TestModel", "EnumType"), isNullable: false);

            // Act
            ODataEnumSerializer.AddTypeNameAnnotationAsNeeded(enumValue, enumType, ODataMetadataLevel.Full);

            // Assert
            ODataTypeAnnotation annotation = enumValue.TypeAnnotation;

            Assert.NotNull(annotation);
            Assert.Equal("TestModel.EnumType", annotation.TypeName);
        }
示例#8
0
        private OePropertyAccessor(IEdmProperty edmProperty, Func <Object, Object> accessor, MemberExpression propertyExpression, bool skipToken)
        {
            EdmProperty        = edmProperty;
            _accessor          = accessor;
            PropertyExpression = propertyExpression;
            SkipToken          = skipToken;

            if (edmProperty.DeclaringType == PrimitiveTypeHelper.TupleEdmType)
            {
                String typeName = propertyExpression.Type.IsEnum ? propertyExpression.Type.FullName : edmProperty.Type.ShortQualifiedName();
                TypeAnnotation = new ODataTypeAnnotation(typeName);
            }
            else
            {
                TypeAnnotation = null;
            }
        }
        public void AddTypeNameAnnotationAsNeeded_AddsAnnotationWithNull_InJsonLightNoMetadataMode()
        {
            // Arrange
            string expectedTypeName    = "TypeName";
            ODataCollectionValue value = new ODataCollectionValue
            {
                TypeName = expectedTypeName
            };

            // Act
            ODataCollectionSerializer.AddTypeNameAnnotationAsNeeded(value, ODataMetadataLevel.None);

            // Assert
            ODataTypeAnnotation annotation = value.TypeAnnotation;

            Assert.NotNull(annotation); // Guard
            Assert.Null(annotation.TypeName);
        }
示例#10
0
        /// <summary>
        /// Gets a resource metadata builder for the given resource.
        /// </summary>
        /// <param name="resourceState">Resource state to use as reference for information needed by the builder.</param>
        /// <param name="useKeyAsSegment">true if keys should go in separate segments in auto-generated URIs, false if they should go in parentheses.</param>
        /// <param name="isDelta">true if the payload being read is a delta payload</param>
        /// <returns>A resource metadata builder.</returns>
        public ODataResourceMetadataBuilder GetResourceMetadataBuilderForReader(IODataJsonLightReaderResourceState resourceState, bool useKeyAsSegment, bool isDelta = false)
        {
            Debug.Assert(resourceState != null, "resource != null");

            // Only apply the conventional template builder on response. On a request we would only report what's on the wire.
            if (resourceState.MetadataBuilder == null)
            {
                ODataResourceBase resource = resourceState.Resource;
                if (this.isResponse && !isDelta)
                {
                    ODataTypeAnnotation typeAnnotation = resource.TypeAnnotation;

                    IEdmStructuredType structuredType = null;
                    if (typeAnnotation != null)
                    {
                        if (typeAnnotation.Type != null)
                        {
                            // First try ODataTypeAnnotation.Type (for perf improvement)
                            structuredType = typeAnnotation.Type as IEdmStructuredType;
                        }
                        else if (typeAnnotation.TypeName != null)
                        {
                            // Then try ODataTypeAnnotation.TypeName
                            structuredType = this.model.FindType(typeAnnotation.TypeName) as IEdmStructuredType;
                        }
                    }

                    if (structuredType == null)
                    {
                        // No type name read from the payload. Use resource type from model.
                        structuredType = resourceState.ResourceType;
                    }

                    IEdmNavigationSource      navigationSource            = resourceState.NavigationSource;
                    IEdmEntityType            navigationSourceElementType = this.edmTypeResolver.GetElementType(navigationSource);
                    IODataResourceTypeContext typeContext =
                        ODataResourceTypeContext.Create( /*serializationInfo*/
                            null, navigationSource, navigationSourceElementType, resourceState.ResourceTypeFromMetadata ?? resourceState.ResourceType,
                            /*throwIfMissingTypeInfo*/ true);
                    IODataResourceMetadataContext resourceMetadataContext = ODataResourceMetadataContext.Create(resource, typeContext, /*serializationInfo*/ null, structuredType, this, resourceState.SelectedProperties);

                    ODataConventionalUriBuilder uriBuilder = new ODataConventionalUriBuilder(this.ServiceBaseUri,
                                                                                             useKeyAsSegment ? ODataUrlKeyDelimiter.Slash : ODataUrlKeyDelimiter.Parentheses);

                    if (structuredType.IsODataEntityTypeKind())
                    {
                        resourceState.MetadataBuilder = new ODataConventionalEntityMetadataBuilder(resourceMetadataContext, this, uriBuilder);
                    }
                    else
                    {
                        resourceState.MetadataBuilder = new ODataConventionalResourceMetadataBuilder(resourceMetadataContext, this, uriBuilder);
                    }
                }
                else
                {
                    resourceState.MetadataBuilder = new NoOpResourceMetadataBuilder(resource);
                }
            }

            return(resourceState.MetadataBuilder);
        }