/// <summary> /// Creates a new <see cref="Scope"/> for the specified <paramref name="state"/> with the provided /// <paramref name="name"/> and <paramref name="value"/> and pushes it on the stack of scopes. /// </summary> /// <param name="state">The <see cref="ODataParameterReaderState"/> to use for the new scope.</param> /// <param name="name">The parameter name to attach with the state in the new scope.</param> /// <param name="value">The parameter value to attach with the state in the new scope.</param> protected internal void EnterScope(ODataParameterReaderState state, string name, object value) { if (state == ODataParameterReaderState.Value) { if (value != null && !EdmLibraryExtensions.IsPrimitiveType(value.GetType()) && !(value is ODataEnumValue)) { throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataParameterReaderCore_ValueMustBePrimitiveOrNull)); } } Debug.Assert(this.scopes.Count == 0 || this.State != ODataParameterReaderState.Exception || state == ODataParameterReaderState.Exception, "If the reader is already in Exception state, we shouldn't call EnterScope() except for another Exception."); // We only need to enter the exception state once. if (this.scopes.Count == 0 || this.State != ODataParameterReaderState.Exception) { Debug.Assert( state == ODataParameterReaderState.Exception || state == ODataParameterReaderState.Start && this.scopes.Count == 0 || state == ODataParameterReaderState.Completed && this.scopes.Count == 0 || this.scopes.Count == 1 && this.scopes.Peek().State == ODataParameterReaderState.Start, "Unexpected state in the scopes stack."); // Make sure there aren't any missing parameter in the payload. if (state == ODataParameterReaderState.Completed) { List <string> missingParameters = new List <string>(); // Note that the binding parameter will be specified on the Uri rather than the payload, skip the binding parameter. foreach (IEdmOperationParameter parameter in this.Operation.Parameters.Skip(this.Operation.IsBound ? 1 : 0)) { // only check the non-optional parameter if (!(parameter is IEdmOptionalParameter)) { if (!this.parametersRead.Contains(parameter.Name) && !this.inputContext.EdmTypeResolver.GetParameterType(parameter).IsNullable) { missingParameters.Add(parameter.Name); } } } if (missingParameters.Count > 0) { this.scopes.Push(new Scope(ODataParameterReaderState.Exception, null, null)); throw new ODataException(Strings.ODataParameterReaderCore_ParametersMissingInPayload(this.Operation.Name, string.Join(",", missingParameters.ToArray()))); } } else if (name != null) { // Record the parameter names we read and check for duplicates. if (this.parametersRead.Contains(name)) { throw new ODataException(Strings.ODataParameterReaderCore_DuplicateParametersInPayload(name)); } this.parametersRead.Add(name); } this.scopes.Push(new Scope(state, name, value)); } }
/// <summary> /// Writes the json value (primitive, IDictionary or IEnumerable) to the underlying json writer. /// </summary> /// <param name="jsonWriter">The <see cref="JsonWriter"/> to write to.</param> /// <param name="propertyValue">value to write.</param> private static void WriteJsonValue(this IJsonWriter jsonWriter, object propertyValue) { if (propertyValue == null) { jsonWriter.WriteValue((string)null); } else if (EdmLibraryExtensions.IsPrimitiveType(propertyValue.GetType())) { jsonWriter.WritePrimitiveValue(propertyValue); } else { IDictionary <string, object> objectValue = propertyValue as IDictionary <string, object>; if (objectValue != null) { jsonWriter.WriteJsonObjectValue(objectValue, null /*typeName */); } else { IEnumerable arrayValue = propertyValue as IEnumerable; Debug.Assert(arrayValue != null, "arrayValue != null"); jsonWriter.WriteJsonArrayValue(arrayValue); } } }
/// <summary> /// Asynchronously writes the json value (primitive, IDictionary or IEnumerable) to the underlying json writer. /// </summary> /// <param name="jsonWriter">The <see cref="JsonWriter"/> to write to.</param> /// <param name="propertyValue">value to write.</param> /// <returns>A task that represents the asynchronous write operation.</returns> private static async Task WriteJsonValueAsync(this IJsonWriterAsync jsonWriter, object propertyValue) { if (propertyValue == null) { await jsonWriter.WriteValueAsync((string)null).ConfigureAwait(false); } else if (EdmLibraryExtensions.IsPrimitiveType(propertyValue.GetType())) { await jsonWriter.WritePrimitiveValueAsync(propertyValue).ConfigureAwait(false); } else { IDictionary <string, object> objectValue = propertyValue as IDictionary <string, object>; if (objectValue != null) { await jsonWriter.WriteJsonObjectValueAsync(objectValue, null /*typeName */).ConfigureAwait(false); } else { IEnumerable arrayValue = propertyValue as IEnumerable; Debug.Assert(arrayValue != null, "arrayValue != null"); await jsonWriter.WriteJsonArrayValueAsync(arrayValue).ConfigureAwait(false); } } }
/// <summary> /// Writes the ODataValue (primitive, collection or resource value) to the underlying json writer. /// </summary> /// <param name="jsonWriter">The <see cref="JsonWriter"/> to write to.</param> /// <param name="odataValue">value to write.</param> internal static void WriteODataValue(this IJsonWriter jsonWriter, ODataValue odataValue) { if (odataValue == null || odataValue is ODataNullValue) { jsonWriter.WriteValue((string)null); return; } object objectValue = odataValue.FromODataValue(); if (EdmLibraryExtensions.IsPrimitiveType(objectValue.GetType())) { jsonWriter.WritePrimitiveValue(objectValue); return; } ODataResourceValue resourceValue = odataValue as ODataResourceValue; if (resourceValue != null) { jsonWriter.StartObjectScope(); foreach (ODataProperty property in resourceValue.Properties) { jsonWriter.WriteName(property.Name); jsonWriter.WriteODataValue(property.ODataValue); } jsonWriter.EndObjectScope(); return; } ODataCollectionValue collectionValue = odataValue as ODataCollectionValue; if (collectionValue != null) { jsonWriter.StartArrayScope(); foreach (object item in collectionValue.Items) { // Will not be able to accurately serialize complex objects unless they are ODataValues. ODataValue collectionItem = item as ODataValue; if (item != null) { jsonWriter.WriteODataValue(collectionItem); } else { throw new ODataException(ODataErrorStrings.ODataJsonWriter_UnsupportedValueInCollection); } } jsonWriter.EndArrayScope(); return; } throw new ODataException( ODataErrorStrings.ODataJsonWriter_UnsupportedValueType(odataValue.GetType().FullName)); }
private IEdmTypeReference VerifyCanWriteValueParameter(bool synchronousCall, string parameterName, object parameterValue) { IEdmTypeReference typeReference = this.VerifyCanWriteParameterAndGetTypeReference(synchronousCall, parameterName); if (((typeReference != null) && !typeReference.IsODataPrimitiveTypeKind()) && !typeReference.IsODataComplexTypeKind()) { throw new ODataException(Microsoft.Data.OData.Strings.ODataParameterWriterCore_CannotWriteValueOnNonValueTypeKind(parameterName, typeReference.TypeKind())); } if (((parameterValue != null) && (!EdmLibraryExtensions.IsPrimitiveType(parameterValue.GetType()) || (parameterValue is Stream))) && !(parameterValue is ODataComplexValue)) { throw new ODataException(Microsoft.Data.OData.Strings.ODataParameterWriterCore_CannotWriteValueOnNonSupportedValueType(parameterName, parameterValue.GetType())); } return(typeReference); }
/// <summary> /// Creates a new primitive value from the given CLR value. /// </summary> /// <param name="value">The primitive to wrap.</param> /// <remarks>The primitive value should not be an instance of ODataValue.</remarks> public ODataPrimitiveValue(object value) { if (value == null) { throw new ArgumentNullException(Strings.ODataPrimitiveValue_CannotCreateODataPrimitiveValueFromNull, (Exception)null); } if (!EdmLibraryExtensions.IsPrimitiveType(value.GetType())) { throw new ODataException(Strings.ODataPrimitiveValue_CannotCreateODataPrimitiveValueFromUnsupportedValueType(value.GetType())); } this.Value = value; }
/// <summary> /// Verify that calling WriteValue is valid. /// </summary> /// <param name="synchronousCall">true if the call is to be synchronous; false otherwise.</param> /// <param name="parameterName">The name of the parameter to be written.</param> /// <param name="parameterValue">The value of the parameter to write.</param> /// <returns>The type reference of the parameter; null if no operation import was specified to the writer.</returns> private IEdmTypeReference VerifyCanWriteValueParameter(bool synchronousCall, string parameterName, object parameterValue) { Debug.Assert(!string.IsNullOrEmpty(parameterName), "!string.IsNullOrEmpty(parameterName)"); IEdmTypeReference parameterTypeReference = this.VerifyCanWriteParameterAndGetTypeReference(synchronousCall, parameterName); if (parameterTypeReference != null && !parameterTypeReference.IsODataPrimitiveTypeKind() && !parameterTypeReference.IsODataComplexTypeKind() && !parameterTypeReference.IsODataEnumTypeKind()) { throw new ODataException(Strings.ODataParameterWriterCore_CannotWriteValueOnNonValueTypeKind(parameterName, parameterTypeReference.TypeKind())); } if (parameterValue != null && (!EdmLibraryExtensions.IsPrimitiveType(parameterValue.GetType()) || parameterValue is Stream) && !(parameterValue is ODataComplexValue) && !(parameterValue is ODataEnumValue)) { throw new ODataException(Strings.ODataParameterWriterCore_CannotWriteValueOnNonSupportedValueType(parameterName, parameterValue.GetType())); } return(parameterTypeReference); }
/// <summary> /// Creates a new <see cref="Scope"/> for the specified <paramref name="state"/> with the provided /// <paramref name="name"/> and <paramref name="value"/> and pushes it on the stack of scopes. /// </summary> /// <param name="state">The <see cref="ODataParameterReaderState"/> to use for the new scope.</param> /// <param name="name">The paramter name to attach with the state in the new scope.</param> /// <param name="value">The paramter value to attach with the state in the new scope.</param> protected void EnterScope(ODataParameterReaderState state, string name, object value) { if (state == ODataParameterReaderState.Value) { if (value != null && !(value is ODataComplexValue) && !EdmLibraryExtensions.IsPrimitiveType(value.GetType())) { throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataParameterReaderCore_ValueMustBePrimitiveOrComplexOrNull)); } } Debug.Assert(this.scopes.Count == 0 || this.State != ODataParameterReaderState.Exception || state == ODataParameterReaderState.Exception, "If the reader is already in Exception state, we shouldn't call EnterScope() except for another Exception."); // We only need to enter the exception state once. if (this.scopes.Count == 0 || this.State != ODataParameterReaderState.Exception) { Debug.Assert( state == ODataParameterReaderState.Exception || state == ODataParameterReaderState.Start && this.scopes.Count == 0 || state == ODataParameterReaderState.Completed && this.scopes.Count == 0 || this.scopes.Count == 1 && this.scopes.Peek().State == ODataParameterReaderState.Start, "Unexpected state in the scopes stack."); // Make sure there aren't any missing parameter in the payload. if (state == ODataParameterReaderState.Completed) { List <string> missingParameters = new List <string>(); // Note that the binding parameter will be specified on the Uri rather than the payload, skip the binding parameter. foreach (IEdmFunctionParameter parameter in this.FunctionImport.Parameters.Skip(this.FunctionImport.IsBindable ? 1 : 0)) { if (!this.parametersRead.Contains(parameter.Name) && !parameter.Type.IsNullable) { missingParameters.Add(parameter.Name); } } if (missingParameters.Count > 0) { this.scopes.Push(new Scope(ODataParameterReaderState.Exception, null, null)); throw new ODataException(Strings.ODataParameterReaderCore_ParametersMissingInPayload(this.FunctionImport.Name, string.Join(",", missingParameters.ToArray()))); } } this.scopes.Push(new Scope(state, name, value)); } }
public Scope(ODataCollectionReaderState state, object item, bool isCollectionElementEmpty) { Debug.Assert( state == ODataCollectionReaderState.Start && item == null || state == ODataCollectionReaderState.CollectionStart && item is ODataCollectionStart || state == ODataCollectionReaderState.Value && (item == null || EdmLibraryExtensions.IsPrimitiveType(item.GetType()) || item is ODataEnumValue) || state == ODataCollectionReaderState.CollectionEnd && item is ODataCollectionStart || state == ODataCollectionReaderState.Exception && item == null || state == ODataCollectionReaderState.Completed && item == null, "Reader state and associated item do not match."); this.state = state; this.item = item; this.isCollectionElementEmpty = isCollectionElementEmpty; if (this.isCollectionElementEmpty) { Debug.Assert(state == ODataCollectionReaderState.CollectionStart, "Expected state to be CollectionStart."); } }
private static void WriteJsonValue(this JsonWriter jsonWriter, object propertyValue, ODataVersion odataVersion) { if (propertyValue == null) { jsonWriter.WriteValue((string)null); } else if (EdmLibraryExtensions.IsPrimitiveType(propertyValue.GetType())) { jsonWriter.WritePrimitiveValue(propertyValue, odataVersion); } else { IDictionary <string, object> jsonObjectValue = propertyValue as IDictionary <string, object>; if (jsonObjectValue != null) { jsonWriter.WriteJsonObjectValue(jsonObjectValue, null, odataVersion); } else { IEnumerable arrayValue = propertyValue as IEnumerable; jsonWriter.WriteJsonArrayValue(arrayValue, odataVersion); } } }
public void IsPrimitiveTypeForUnsupportedTypesShouldBeFalse(Type type) { bool result = EdmLibraryExtensions.IsPrimitiveType(type); result.Should().BeFalse(); }
/// <summary> /// Constructor creating a new reader scope. /// </summary> /// <param name="state">The reader state of this scope.</param> /// <param name="name">The parameter name attached to this scope.</param> /// <param name="value">The parameter value attached to this scope.</param> public Scope(ODataParameterReaderState state, string name, object value) { Debug.Assert( state == ODataParameterReaderState.Start && name == null && value == null || state == ODataParameterReaderState.Value && !string.IsNullOrEmpty(name) && (value == null || value is ODataEnumValue || value is ODataComplexValue || EdmLibraryExtensions.IsPrimitiveType(value.GetType())) || #if SUPPORT_ENTITY_PARAMETER state == ODataParameterReaderState.Entry && !string.IsNullOrEmpty(name) && value == null || state == ODataParameterReaderState.Feed && !string.IsNullOrEmpty(name) && value == null || #endif state == ODataParameterReaderState.Collection && !string.IsNullOrEmpty(name) && value == null || state == ODataParameterReaderState.Exception && name == null && value == null || state == ODataParameterReaderState.Completed && name == null && value == null, "Reader state and associated item do not match."); this.state = state; this.name = name; this.value = value; }
public void IsPrimitiveTypeForSupportedTypesShouldBeTrue(Type type) { bool result = EdmLibraryExtensions.IsPrimitiveType(type); Assert.True(result); }
protected void EnterScope(ODataParameterReaderState state, string name, object value) { if (((state == ODataParameterReaderState.Value) && (value != null)) && (!(value is ODataComplexValue) && !EdmLibraryExtensions.IsPrimitiveType(value.GetType()))) { throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodes.ODataParameterReaderCore_ValueMustBePrimitiveOrComplexOrNull)); } if ((this.scopes.Count == 0) || (this.State != ODataParameterReaderState.Exception)) { if (state == ODataParameterReaderState.Completed) { List <string> list = new List <string>(); foreach (IEdmFunctionParameter parameter in this.FunctionImport.Parameters.Skip <IEdmFunctionParameter>(this.FunctionImport.IsBindable ? 1 : 0)) { if (!this.parametersRead.Contains(parameter.Name)) { list.Add(parameter.Name); } } if (list.Count > 0) { this.scopes.Push(new Scope(ODataParameterReaderState.Exception, null, null)); throw new ODataException(Microsoft.Data.OData.Strings.ODataParameterReaderCore_ParametersMissingInPayload(this.FunctionImport.Name, string.Join(",", list.ToArray()))); } } this.scopes.Push(new Scope(state, name, value)); } }