/// <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)); }
public ODataConventionalUriBuilderTests() { this.defaultBaseUri = new Uri("http://odata.org/base/"); this.builder = new ODataConventionalUriBuilder(this.defaultBaseUri, ODataUrlKeyDelimiter.Parentheses); this.model = TestModel.BuildDefaultTestModel(); this.defaultProductInstance = TestModel.BuildDefaultProductValue(TestModel.GetEntityType(this.model, "TestModel.Product")); this.defaultMultipleKeyInstance = TestModel.BuildDefaultMultipleKeyValue(this.model); this.idPropertyList = new Dictionary <string, IEdmValue>() { { "Id", new EdmIntegerConstant(EdmCoreModel.Instance.GetInt32(false), 42) } }; this.typeWithStringKey = new EdmEntityType("Test.Model", "StringKey"); this.typeWithStringKey.AddKeys(this.typeWithStringKey.AddStructuralProperty("Id", EdmPrimitiveTypeKind.String)); }
private static void RunPinningTest(StringBuilder builder, params object[] keyValues) { var compositeKey = keyValues.Select((k, i) => new { Name = "prop" + i, Value = k }).ToArray(); var allKeys = keyValues.Select(k => new[] { new { Name = "prop", Value = k } }).Concat(new[] { compositeKey }); foreach (var key in allKeys) { if (builder.Length > 0) { builder.AppendLine(); } var properties = key.Select(p => new EdmPropertyValue(p.Name, EdmValueUtils.ConvertPrimitiveValue(p.Value, null).Value)).ToList(); var entityType = new EdmEntityType("Fake", "Fake"); entityType.AddKeys(properties.Select(p => new EdmStructuralProperty(entityType, p.Name, p.Value.Type))); var entity = new EdmStructuredValue(new EdmEntityTypeReference(entityType, false), properties); ODataConventionalUriBuilder uriBuilder = new ODataConventionalUriBuilder(new Uri("http://baseuri.org/"), UrlConvention.CreateWithExplicitValue(false)); var entityInstanceUri = uriBuilder.BuildEntityInstanceUri(new Uri("http://baseuri.org/Customers"), new Collection <KeyValuePair <string, object> >(properties.Select(p => new KeyValuePair <string, object>(p.Name, ((IEdmPrimitiveValue)p.Value).ToClrValue())).ToList()), entity.Type.FullName()); builder.Append(entityInstanceUri.OriginalString.Replace("http://baseuri.org/Customers", null).Replace("()", "%28%29")); } builder.AppendLine(); }
/// <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)); } }