private IEdmStructuredObject EnsureModel(IEdmStructuredObject obj) { if (obj != null) { obj.SetModel(this.EdmModel); } return(obj); }
internal IEdmTypeReference GetEdmType(object instance, Type type) { IEdmTypeReference edmType; IEdmObject edmObject = instance as IEdmObject; if (edmObject != null) { IEdmStructuredObject edmStructuredObject = edmObject as IEdmStructuredObject; if (edmStructuredObject != null) { edmStructuredObject.SetModel(Model); } edmType = edmObject.GetEdmType(); if (edmType == null) { throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName, typeof(IEdmObject).Name); } } else { if (Model == null) { throw Error.InvalidOperation(SRResources.RequestMustHaveModel); } _typeMappingCache = _typeMappingCache ?? Model.GetTypeMappingCache(); edmType = _typeMappingCache.GetEdmType(type, Model); if (edmType == null) { if (instance != null) { edmType = _typeMappingCache.GetEdmType(instance.GetType(), Model); } if (edmType == null) { throw Error.InvalidOperation(SRResources.ClrTypeNotInModel, type); } } else if (instance != null) { IEdmTypeReference actualType = _typeMappingCache.GetEdmType(instance.GetType(), Model); if (actualType != null && actualType != edmType) { edmType = actualType; } } } return(edmType); }
private ODataSerializer GetSerializer(Type type, object value, ODataSerializerProvider serializerProvider) { ODataSerializer serializer; IEdmObject edmObject = value as IEdmObject; if (edmObject != null) { IEdmStructuredObject edmStructuredObject = edmObject as IEdmStructuredObject; if (edmStructuredObject != null) { edmStructuredObject.SetModel(Request.GetModel()); } IEdmTypeReference edmType = edmObject.GetEdmType(); if (edmType == null) { throw new SerializationException(Error.Format(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName, typeof(IEdmObject).Name)); } serializer = serializerProvider.GetEdmTypeSerializer(edmType); if (serializer == null) { string message = Error.Format(SRResources.TypeCannotBeSerialized, edmType.ToTraceString(), typeof(ODataMediaTypeFormatter).Name); throw new SerializationException(message); } } else { var applyClause = Request.ODataProperties().ApplyClause; // get the most appropriate serializer given that we support inheritance. if (applyClause == null) { type = value == null ? type : value.GetType(); } serializer = serializerProvider.GetODataPayloadSerializer(type, Request); if (serializer == null) { string message = Error.Format(SRResources.TypeCannotBeSerialized, type.Name, typeof(ODataMediaTypeFormatter).Name); throw new SerializationException(message); } } return(serializer); }
internal static void WriteToStream( Type type, object value, IEdmModel model, ODataVersion version, Uri baseAddress, MediaTypeHeaderValue contentType, IWebApiUrlHelper internaUrlHelper, IWebApiRequestMessage internalRequest, IWebApiHeaders internalRequestHeaders, Func <IServiceProvider, ODataMessageWrapper> getODataMessageWrapper, Func <IEdmTypeReference, ODataSerializer> getEdmTypeSerializer, Func <Type, ODataSerializer> getODataPayloadSerializer, Func <ODataSerializerContext> getODataSerializerContext) { if (model == null) { throw Error.InvalidOperation(SRResources.RequestMustHaveModel); } IEdmStructuredObject edmStructuredObject = value as IEdmStructuredObject; if (edmStructuredObject != null) { edmStructuredObject.SetModel(model); } ODataSerializer serializer = GetSerializer(type, value, internalRequest, getEdmTypeSerializer, getODataPayloadSerializer); ODataPath path = internalRequest.Context.Path; IEdmNavigationSource targetNavigationSource = path == null ? null : path.NavigationSource; // serialize a response IODataResponseMessage responseMessage = PrepareResponseMessage(internalRequest, internalRequestHeaders, getODataMessageWrapper); ODataMessageWriterSettings writerSettings = internalRequest.WriterSettings; writerSettings.BaseUri = baseAddress; writerSettings.Version = version; writerSettings.Validations = writerSettings.Validations & ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType; string metadataLink = internaUrlHelper.CreateODataLink(MetadataSegment.Instance); if (metadataLink == null) { throw new SerializationException(SRResources.UnableToDetermineMetadataUrl); } //Set this variable if the SelectExpandClause is different from the processed clause on the Query options SelectExpandClause selectExpandDifferentFromQueryOptions = null; if (internalRequest.Context.QueryOptions != null && internalRequest.Context.QueryOptions.SelectExpand != null) { if (internalRequest.Context.QueryOptions.SelectExpand.ProcessedSelectExpandClause != internalRequest.Context.ProcessedSelectExpandClause) { selectExpandDifferentFromQueryOptions = internalRequest.Context.ProcessedSelectExpandClause; } } else if (internalRequest.Context.ProcessedSelectExpandClause != null) { selectExpandDifferentFromQueryOptions = internalRequest.Context.ProcessedSelectExpandClause; } writerSettings.ODataUri = new ODataUri { ServiceRoot = baseAddress, // TODO: 1604 Convert webapi.odata's ODataPath to ODL's ODataPath, or use ODL's ODataPath. SelectAndExpand = internalRequest.Context.ProcessedSelectExpandClause, Apply = internalRequest.Context.ApplyClause, Path = (path == null || IsOperationPath(path)) ? null : path.Path, }; ODataMetadataLevel metadataLevel = ODataMetadataLevel.MinimalMetadata; if (contentType != null) { IEnumerable <KeyValuePair <string, string> > parameters = contentType.Parameters.Select(val => new KeyValuePair <string, string>(val.Name, val.Value)); metadataLevel = ODataMediaTypes.GetMetadataLevel(contentType.MediaType, parameters); } using (ODataMessageWriter messageWriter = new ODataMessageWriter(responseMessage, writerSettings, model)) { ODataSerializerContext writeContext = getODataSerializerContext(); writeContext.NavigationSource = targetNavigationSource; writeContext.Model = model; writeContext.RootElementName = GetRootElementName(path) ?? "root"; writeContext.SkipExpensiveAvailabilityChecks = serializer.ODataPayloadKind == ODataPayloadKind.ResourceSet; writeContext.Path = path; writeContext.MetadataLevel = metadataLevel; writeContext.QueryOptions = internalRequest.Context.QueryOptions; //Set the SelectExpandClause on the context if it was explicitly specified. if (selectExpandDifferentFromQueryOptions != null) { writeContext.SelectExpandClause = selectExpandDifferentFromQueryOptions; } serializer.WriteObject(value, type, messageWriter, writeContext); } }