示例#1
0
        /// <summary>
        /// Creates the metadata builder for the given entry. If such a builder is set, asking for payload
        /// metadata properties (like EditLink) of the entry may return a value computed by convention,
        /// depending on the metadata level and whether the user manually set an edit link or not.
        /// </summary>
        /// <param name="entry">The entry to create the metadata builder for.</param>
        /// <param name="typeContext">The context object to answer basic questions regarding the type of the entry or feed.</param>
        /// <param name="serializationInfo">The serialization info for the entry.</param>
        /// <param name="actualEntityType">The entity type of the entry.</param>
        /// <param name="selectedProperties">The selected properties of this scope.</param>
        /// <param name="isResponse">true if the entity metadata builder to create should be for a response payload; false for a request.</param>
        /// <param name="keyAsSegment">true if keys should go in seperate segments in auto-generated URIs, false if they should go in parentheses.
        /// A null value means the user hasn't specified a preference and we should look for an annotation in the entity container, if available.</param>
        /// <param name="odataUri">The OData Uri.</param>
        /// <returns>The created metadata builder.</returns>
        internal override ODataEntityMetadataBuilder CreateEntityMetadataBuilder(
            ODataEntry entry,
            IODataFeedAndEntryTypeContext typeContext,
            ODataFeedAndEntrySerializationInfo serializationInfo,
            IEdmEntityType actualEntityType,
            SelectedPropertiesNode selectedProperties,
            bool isResponse,
            bool?keyAsSegment,
            ODataUri odataUri)
        {
            Debug.Assert(entry != null, "entry != null");
            Debug.Assert(typeContext != null, "typeContext != null");
            Debug.Assert(selectedProperties != null, "selectedProperties != null");

            IODataMetadataContext metadataContext = new ODataMetadataContext(
                isResponse,
                this.model,
                this.NonNullMetadataDocumentUri,
                odataUri);

            UrlConvention urlConvention            = UrlConvention.ForUserSettingAndTypeContext(keyAsSegment, typeContext);
            ODataConventionalUriBuilder uriBuilder = new ODataConventionalUriBuilder(metadataContext.ServiceBaseUri, urlConvention);

            IODataEntryMetadataContext entryMetadataContext = ODataEntryMetadataContext.Create(entry, typeContext, serializationInfo, actualEntityType, metadataContext, selectedProperties);

            return(new ODataConventionalEntityMetadataBuilder(entryMetadataContext, metadataContext, uriBuilder));
        }
示例#2
0
        public void CheckForOperationsRequiringContainerQualificationFallBackToWhetherTypeIsOpen()
        {
            var closedType = new EdmEntityType("NS", "Type1");
            var openType   = new EdmEntityType("NS", "Type2", null, false, true);

            ODataMetadataContext context = new ODataMetadataContext(
                true /*readingResponse*/,
                null,
                new EdmTypeReaderResolver(this.edmModel, null),
                this.edmModel,
                null /*metadataDocumentUri*/,
                null /*requestUri*/);

            context.OperationsBoundToStructuredTypeMustBeContainerQualified(closedType).Should().BeFalse();
            context.OperationsBoundToStructuredTypeMustBeContainerQualified(openType).Should().BeTrue();
        }
示例#3
0
        public void GetEntityMetadataBuilderShouldNotThrowWhenMetadataDocumentUriIsNonNullAndAbsolute()
        {
            ODataMetadataContext context = new ODataMetadataContext(
                true /*readingResponse*/,
                null,
                new EdmTypeReaderResolver(this.edmModel, null),
                this.edmModel,
                new Uri("http://myservice.svc/$metadata", UriKind.Absolute),
                null /*requestUri*/);
            IEdmEntitySet set    = this.edmModel.EntityContainer.FindEntitySet("Products");
            ODataResource entry  = TestUtils.CreateODataEntry(set, new EdmStructuredValue(new EdmEntityTypeReference(set.EntityType(), true), new IEdmPropertyValue[0]), set.EntityType());
            Action        action = () => context.GetResourceMetadataBuilderForReader(new TestJsonLightReaderEntryState {
                Resource = entry, SelectedProperties = new SelectedPropertiesNode("*"), NavigationSource = set
            }, false);

            action.ShouldNotThrow();
        }
示例#4
0
        public void GetEntityMetadataBuilderShouldThrowWhenMetadataDocumentUriIsNull()
        {
            ODataMetadataContext context = new ODataMetadataContext(
                true /*readingResponse*/,
                null,
                new EdmTypeReaderResolver(this.edmModel, null),
                this.edmModel,
                null /*metadataDocumentUri*/,
                null /*requestUri*/);
            IEdmEntitySet set    = this.edmModel.EntityContainer.FindEntitySet("Products");
            ODataResource entry  = TestUtils.CreateODataEntry(set, new EdmStructuredValue(new EdmEntityTypeReference(set.EntityType(), true), new IEdmPropertyValue[0]), set.EntityType());
            Action        action = () => context.GetResourceMetadataBuilderForReader(new TestJsonLightReaderEntryState {
                Resource = entry, SelectedProperties = SelectedPropertiesNode.EntireSubtree, NavigationSource = set
            }, false);

            action.ShouldThrow <ODataException>().WithMessage(Strings.ODataJsonLightResourceMetadataContext_MetadataAnnotationMustBeInPayload("odata.context"));
        }
        private static ODataEntry CreateEntryWithKeyAsSegmentConvention(bool addAnnotation, bool?useKeyAsSegment)
        {
            var model     = new EdmModel();
            var container = new EdmEntityContainer("Fake", "Container");

            model.AddElement(container);
            if (addAnnotation)
            {
                model.AddVocabularyAnnotation(new EdmAnnotation(container, UrlConventionsConstants.ConventionTerm, UrlConventionsConstants.KeyAsSegmentAnnotationValue));
            }

            EdmEntityType entityType = new EdmEntityType("Fake", "FakeType");

            entityType.AddKeys(entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
            model.AddElement(entityType);

            var entitySet = new EdmEntitySet(container, "FakeSet", entityType);

            container.AddElement(entitySet);

            var metadataContext = new ODataMetadataContext(
                true,
                ODataReaderBehavior.DefaultBehavior.OperationsBoundToEntityTypeMustBeContainerQualified,
                new EdmTypeReaderResolver(model, ODataReaderBehavior.DefaultBehavior),
                model,
                new Uri("http://temp.org/$metadata"),
                null /*requestUri*/);

            var thing = new ODataEntry {
                Properties = new[] { new ODataProperty {
                                         Name = "Id", Value = 1
                                     } }
            };

            thing.SetAnnotation(new ODataTypeAnnotation(entitySet, entityType));
            thing.MetadataBuilder = metadataContext.GetEntityMetadataBuilderForReader(new TestJsonLightReaderEntryState {
                Entry = thing, SelectedProperties = new SelectedPropertiesNode("*")
            }, useKeyAsSegment);
            return(thing);
        }
示例#6
0
        /// <summary>
        /// Creates the metadata builder for the given resource. If such a builder is set, asking for payload
        /// metadata properties (like EditLink) of the resource may return a value computed by convention,
        /// depending on the metadata level and whether the user manually set an edit link or not.
        /// </summary>
        /// <param name="resource">The resource to create the metadata builder for.</param>
        /// <param name="typeContext">The context object to answer basic questions regarding the type of the resource or resource set.</param>
        /// <param name="serializationInfo">The serialization info for the resource.</param>
        /// <param name="actualResourceType">The structured type of the resource.</param>
        /// <param name="selectedProperties">The selected properties of this scope.</param>
        /// <param name="isResponse">true if the resource metadata builder to create should be for a response payload; false for a request.</param>
        /// <param name="keyAsSegment">true if keys should go in separate segments in auto-generated URIs, false if they should go in parentheses.</param>
        /// <param name="odataUri">The OData Uri.</param>
        /// <param name="settings">OData message writer settings.</param>
        /// <returns>The created metadata builder.</returns>
        internal override ODataResourceMetadataBuilder CreateResourceMetadataBuilder(
            ODataResourceBase resource,
            IODataResourceTypeContext typeContext,
            ODataResourceSerializationInfo serializationInfo,
            IEdmStructuredType actualResourceType,
            SelectedPropertiesNode selectedProperties,
            bool isResponse,
            bool keyAsSegment,
            ODataUri odataUri,
            ODataMessageWriterSettings settings)
        {
            Debug.Assert(resource != null, "resource != null");
            Debug.Assert(typeContext != null, "typeContext != null");
            Debug.Assert(selectedProperties != null, "selectedProperties != null");

            IODataMetadataContext metadataContext = new ODataMetadataContext(
                isResponse,
                this.model,
                this.NonNullMetadataDocumentUri,
                odataUri);

            ODataConventionalUriBuilder uriBuilder = new ODataConventionalUriBuilder(metadataContext.ServiceBaseUri,
                                                                                     keyAsSegment ? ODataUrlKeyDelimiter.Slash : ODataUrlKeyDelimiter.Parentheses);

            IODataResourceMetadataContext resourceMetadataContext = ODataResourceMetadataContext.Create(resource, typeContext, serializationInfo, actualResourceType, metadataContext, selectedProperties, settings != null ? settings.MetadataSelector : null);

            // Create ODataConventionalEntityMetadataBuilder if actualResourceType is entity type or typeContext.NavigationSourceKind is not none (complex type would be none) for no model scenario.
            if (actualResourceType != null && actualResourceType.TypeKind == EdmTypeKind.Entity ||
                actualResourceType == null && typeContext.NavigationSourceKind != EdmNavigationSourceKind.None)
            {
                return(new ODataConventionalEntityMetadataBuilder(resourceMetadataContext, metadataContext, uriBuilder));
            }
            else
            {
                return(new ODataConventionalResourceMetadataBuilder(resourceMetadataContext, metadataContext, uriBuilder));
            }
        }
示例#7
0
        private static ODataResource CreateEntryWithKeyAsSegmentConvention(bool useKeyAsSegment)
        {
            var model     = new EdmModel();
            var container = new EdmEntityContainer("Fake", "Container");

            model.AddElement(container);

            EdmEntityType entityType = new EdmEntityType("Fake", "FakeType");

            entityType.AddKeys(entityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
            model.AddElement(entityType);

            var entitySet = new EdmEntitySet(container, "FakeSet", entityType);

            container.AddElement(entitySet);

            var metadataContext = new ODataMetadataContext(
                true,
                null,
                new EdmTypeReaderResolver(model, null),
                model,
                new Uri("http://temp.org/$metadata"),
                null /*requestUri*/);

            var thing = new ODataResource {
                Properties = new[] { new ODataProperty {
                                         Name = "Id", Value = 1
                                     } }
            };

            thing.TypeAnnotation  = new ODataTypeAnnotation(entityType.FullTypeName());
            thing.MetadataBuilder = metadataContext.GetResourceMetadataBuilderForReader(new TestJsonLightReaderEntryState {
                Resource = thing, SelectedProperties = new SelectedPropertiesNode("*", null, null), NavigationSource = entitySet
            }, useKeyAsSegment);
            return(thing);
        }