本文整理汇总了C#中IEdmTypeReference.ODataFullName方法的典型用法代码示例。如果您正苦于以下问题:C# IEdmTypeReference.ODataFullName方法的具体用法?C# IEdmTypeReference.ODataFullName怎么用?C# IEdmTypeReference.ODataFullName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEdmTypeReference
的用法示例。
在下文中一共展示了IEdmTypeReference.ODataFullName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetValueTypeNameForWriting
/// <summary>
/// Determines the type name to write to the payload. Json Light type names are only written into the payload for open properties
/// or if the payload type name is more derived than the model type name.
/// </summary>
/// <param name="value">The ODataValue whose type name is to be written.</param>
/// <param name="typeReferenceFromMetadata">The type as expected by the model.</param>
/// <param name="typeReferenceFromValue">The type resolved from the value.</param>
/// <param name="isOpenProperty">true if the type name belongs to an open property, false otherwise.</param>
/// <returns>Type name to write to the payload, or null if no type should be written.</returns>
internal override string GetValueTypeNameForWriting(
ODataValue value,
IEdmTypeReference typeReferenceFromMetadata,
IEdmTypeReference typeReferenceFromValue,
bool isOpenProperty)
{
SerializationTypeNameAnnotation typeNameAnnotation = value.GetAnnotation<SerializationTypeNameAnnotation>();
if (typeNameAnnotation != null)
{
return typeNameAnnotation.TypeName;
}
if (typeReferenceFromValue != null)
{
// Write type name when the type in the payload is more derived than the type from metadata.
if (typeReferenceFromMetadata != null && typeReferenceFromMetadata.Definition.AsActualType().ODataFullName() != typeReferenceFromValue.ODataFullName())
{
return typeReferenceFromValue.ODataFullName();
}
// Note: When writing derived complexType value in a payload, we don't have the expected type.
// So always write @odata.type for top-level derived complextype.
if (typeReferenceFromMetadata == null && typeReferenceFromValue.IsComplex())
{
if ((typeReferenceFromValue as IEdmComplexTypeReference).ComplexDefinition().BaseType != null)
{
return typeReferenceFromValue.ODataFullName();
}
}
// Do not write type name when the type is native json type.
if (typeReferenceFromValue.IsPrimitive() && JsonSharedUtils.ValueTypeMatchesJsonType((ODataPrimitiveValue)value, typeReferenceFromValue.AsPrimitive()))
{
return null;
}
}
if (!isOpenProperty)
{
// Do not write type name for non-open properties since we expect the reader to have an expected type (via API or context URI) and thus not need it.
return null;
}
return GetTypeNameFromValue(value);
}
示例2: CreateSerializationTypeNameAnnotation
private static SerializationTypeNameAnnotation CreateSerializationTypeNameAnnotation(string payloadTypeName, IEdmTypeReference targetTypeReference)
{
if ((payloadTypeName != null) && (string.CompareOrdinal(payloadTypeName, targetTypeReference.ODataFullName()) != 0))
{
return new SerializationTypeNameAnnotation { TypeName = payloadTypeName };
}
if (payloadTypeName == null)
{
return new SerializationTypeNameAnnotation { TypeName = null };
}
return null;
}
示例3: GetValueTypeNameForWriting
internal string GetValueTypeNameForWriting(
object value,
IEdmTypeReference typeReferenceFromValue,
SerializationTypeNameAnnotation typeNameAnnotation,
CollectionWithoutExpectedTypeValidator collectionValidator,
out string collectionItemTypeName)
{
Debug.Assert(value != null, "value != null");
collectionItemTypeName = null;
// if no type name is specified we will use the type name inferred from metadata
string typeName = GetTypeNameFromValue(value);
if (typeName == null && typeReferenceFromValue != null)
{
typeName = typeReferenceFromValue.ODataFullName();
}
if (typeName != null)
{
// If the type is the same as the one specified by the parent collection, omit the type name, since it's not needed.
if (collectionValidator != null && string.CompareOrdinal(collectionValidator.ItemTypeNameFromCollection, typeName) == 0)
{
typeName = null;
}
// If value is a collection value, get the item type name.
if (typeName != null && value is ODataCollectionValue)
{
collectionItemTypeName = ValidationUtils.ValidateCollectionTypeName(typeName);
}
}
if (typeNameAnnotation != null)
{
// If the value of TypeName is null, we'll flow it through here, thereby instructing the caller to write no type name.
typeName = typeNameAnnotation.TypeName;
}
return typeName;
}
示例4: ValidateIsExpectedPrimitiveType
/// <summary>
/// Validates that a given primitive value is of the expected (primitive) type.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="valuePrimitiveTypeReference">The primitive type reference for the value - some callers have this already, so we save the lookup here.</param>
/// <param name="expectedTypeReference">The expected type for the value.</param>
/// <param name="bypassValidation">Bypass the validation if it is true.</param>
/// <remarks>
/// Some callers have the primitive type reference already resolved (from the value type)
/// so this method is an optimized version to not lookup the primitive type reference again.
/// </remarks>
internal static void ValidateIsExpectedPrimitiveType(object value, IEdmPrimitiveTypeReference valuePrimitiveTypeReference, IEdmTypeReference expectedTypeReference, bool bypassValidation = false)
{
Debug.Assert(value != null, "value != null");
Debug.Assert(expectedTypeReference != null, "expectedTypeReference != null");
if (bypassValidation)
{
return;
}
if (valuePrimitiveTypeReference == null)
{
throw new ODataException(Strings.ValidationUtils_UnsupportedPrimitiveType(value.GetType().FullName));
}
Debug.Assert(valuePrimitiveTypeReference.IsEquivalentTo(EdmLibraryExtensions.GetPrimitiveTypeReference(value.GetType())), "The value and valuePrimitiveTypeReference don't match.");
if (!expectedTypeReference.IsODataPrimitiveTypeKind() && !expectedTypeReference.IsODataTypeDefinitionTypeKind())
{
// non-primitive type found for primitive value.
throw new ODataException(Strings.ValidationUtils_NonPrimitiveTypeForPrimitiveValue(expectedTypeReference.ODataFullName()));
}
ValidateMetadataPrimitiveType(expectedTypeReference, valuePrimitiveTypeReference);
}
示例5: ValidateMetadataPrimitiveType
/// <summary>
/// Validates that the expected primitive type or type definition matches the actual primitive type.
/// </summary>
/// <param name="expectedTypeReference">The expected type.</param>
/// <param name="typeReferenceFromValue">The actual type.</param>
internal static void ValidateMetadataPrimitiveType(IEdmTypeReference expectedTypeReference, IEdmTypeReference typeReferenceFromValue)
{
Debug.Assert(expectedTypeReference != null && (expectedTypeReference.IsODataPrimitiveTypeKind() || expectedTypeReference.IsODataTypeDefinitionTypeKind()), "expectedTypeReference must be a primitive type or type definition.");
Debug.Assert(typeReferenceFromValue != null && typeReferenceFromValue.IsODataPrimitiveTypeKind(), "typeReferenceFromValue must be a primitive type.");
IEdmType expectedType = expectedTypeReference.Definition;
IEdmPrimitiveType typeFromValue = (IEdmPrimitiveType)typeReferenceFromValue.Definition;
// The two primitive types match if they have the same definition and either both or only the
// expected type is nullable
// NOTE: for strings and binary values we must not check nullability here because the type reference
// from the value is always nullable since C# has no way to express non-nullable strings.
// However, this codepath is only hit if the value is not 'null' so we can assign a non-null
// value to both nullable and non-nullable string/binary types.
bool nullableCompatible = expectedTypeReference.IsNullable == typeReferenceFromValue.IsNullable ||
expectedTypeReference.IsNullable && !typeReferenceFromValue.IsNullable ||
!MetadataUtilsCommon.IsODataValueType(typeReferenceFromValue);
bool typeCompatible = expectedType.IsAssignableFrom(typeFromValue);
if (!nullableCompatible || !typeCompatible)
{
// incompatible type name for value!
throw new ODataException(Strings.ValidationUtils_IncompatiblePrimitiveItemType(
typeReferenceFromValue.ODataFullName(),
typeReferenceFromValue.IsNullable,
expectedTypeReference.ODataFullName(),
expectedTypeReference.IsNullable));
}
}
示例6: ValidateNullPropertyValue
/// <summary>
/// Validates that the expected property allows null value.
/// </summary>
/// <param name="expectedPropertyTypeReference">The expected property type or null if we don't have any.</param>
/// <param name="propertyName">The name of the property.</param>
/// <param name="writerBehavior">The <see cref="ODataWriterBehavior"/> instance controlling the behavior of the writer.</param>
/// <param name="model">The model to use to get the OData version.</param>
/// <param name="bypassValidation">Bypass the validation if it is true.</param>
internal static void ValidateNullPropertyValue(IEdmTypeReference expectedPropertyTypeReference, string propertyName, ODataWriterBehavior writerBehavior, IEdmModel model, bool bypassValidation = false)
{
Debug.Assert(writerBehavior != null, "writerBehavior != null");
Debug.Assert(model != null, "For null validation, model is required.");
if (bypassValidation)
{
return;
}
if (expectedPropertyTypeReference != null)
{
if (expectedPropertyTypeReference.IsNonEntityCollectionType())
{
throw new ODataException(Strings.WriterValidationUtils_CollectionPropertiesMustNotHaveNullValue(propertyName));
}
if (expectedPropertyTypeReference.IsODataPrimitiveTypeKind())
{
// WCF DS allows null values for non-nullable primitive types, so we need to check for a knob which enables this behavior.
// See the description of ODataWriterBehavior.AllowNullValuesForNonNullablePrimitiveTypes for more details.
if (!expectedPropertyTypeReference.IsNullable && !writerBehavior.AllowNullValuesForNonNullablePrimitiveTypes)
{
throw new ODataException(Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(propertyName, expectedPropertyTypeReference.ODataFullName()));
}
}
else if (expectedPropertyTypeReference.IsODataEnumTypeKind() && !expectedPropertyTypeReference.IsNullable)
{
throw new ODataException(Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(propertyName, expectedPropertyTypeReference.ODataFullName()));
}
else if (expectedPropertyTypeReference.IsStream())
{
throw new ODataException(Strings.WriterValidationUtils_StreamPropertiesMustNotHaveNullValue(propertyName));
}
else if (expectedPropertyTypeReference.IsODataComplexTypeKind())
{
if (ValidationUtils.ShouldValidateComplexPropertyNullValue(model))
{
IEdmComplexTypeReference complexTypeReference = expectedPropertyTypeReference.AsComplex();
if (!complexTypeReference.IsNullable)
{
throw new ODataException(Strings.WriterValidationUtils_NonNullablePropertiesMustNotHaveNullValue(propertyName, expectedPropertyTypeReference.ODataFullName()));
}
}
}
}
}
示例7: VerifyAndCoerceUriPrimitiveLiteral
/// <summary>
/// Verifies that the given <paramref name="primitiveValue"/> is or can be coerced to <paramref name="expectedTypeReference"/>, and coerces it if necessary.
/// </summary>
/// <param name="primitiveValue">An EDM primitive value to verify.</param>
/// <param name="model">Model to verify against.</param>
/// <param name="expectedTypeReference">Expected type reference.</param>
/// <returns>Coerced version of the <paramref name="primitiveValue"/>.</returns>
internal static object VerifyAndCoerceUriPrimitiveLiteral(object primitiveValue, IEdmModel model, IEdmTypeReference expectedTypeReference)
{
ExceptionUtils.CheckArgumentNotNull(primitiveValue, "primitiveValue");
ExceptionUtils.CheckArgumentNotNull(model, "model");
ExceptionUtils.CheckArgumentNotNull(expectedTypeReference, "expectedTypeReference");
// First deal with null literal
ODataNullValue nullValue = primitiveValue as ODataNullValue;
if (nullValue != null)
{
if (!expectedTypeReference.IsNullable)
{
throw new ODataException(ODataErrorStrings.ODataUriUtils_ConvertFromUriLiteralNullOnNonNullableType(expectedTypeReference.ODataFullName()));
}
return nullValue;
}
// Only other positive case is a numeric primitive that needs to be coerced
IEdmPrimitiveTypeReference expectedPrimitiveTypeReference = expectedTypeReference.AsPrimitiveOrNull();
if (expectedPrimitiveTypeReference == null)
{
throw new ODataException(ODataErrorStrings.ODataUriUtils_ConvertFromUriLiteralTypeVerificationFailure(expectedTypeReference.ODataFullName(), primitiveValue));
}
object coercedResult = CoerceNumericType(primitiveValue, expectedPrimitiveTypeReference.PrimitiveDefinition());
if (coercedResult != null)
{
return coercedResult;
}
// if expectedTypeReference is set, need to coerce cast
coercedResult = CoerceTemporalType(primitiveValue, expectedPrimitiveTypeReference.PrimitiveDefinition());
if (coercedResult != null)
{
return coercedResult;
}
Type actualType = primitiveValue.GetType();
Type targetType = TypeUtils.GetNonNullableType(EdmLibraryExtensions.GetPrimitiveClrType(expectedPrimitiveTypeReference));
// If target type is assignable from actual type, we're OK
if (targetType.IsAssignableFrom(actualType))
{
return primitiveValue;
}
throw new ODataException(ODataErrorStrings.ODataUriUtils_ConvertFromUriLiteralTypeVerificationFailure(expectedPrimitiveTypeReference.ODataFullName(), primitiveValue));
}
示例8: ResolveAndValidatePrimitiveTargetType
/// <summary>
/// Resolves the primitive payload type versus the expected type and validates that such combination is allowed.
/// </summary>
/// <param name="expectedTypeReference">The expected type reference, if any.</param>
/// <param name="payloadTypeKind">The kind of the payload type, or None if the detection was not possible.</param>
/// <param name="payloadType">The resolved payload type, or null if no payload type was specified.</param>
/// <param name="payloadTypeName">The name of the payload type, or null if no payload type was specified.</param>
/// <param name="defaultPayloadType">The default payload type if none is specified in the payload;
/// for ATOM this is Edm.String, for JSON it is null since there is no payload type name for primitive types in the payload.</param>
/// <param name="model">The model to use.</param>
/// <param name="messageReaderSettings">The message reader settings to use.</param>
/// <returns>The target type reference to use for parsing the value. This method never returns null.</returns>
internal static IEdmTypeReference ResolveAndValidatePrimitiveTargetType(
IEdmTypeReference expectedTypeReference,
EdmTypeKind payloadTypeKind,
IEdmType payloadType,
string payloadTypeName,
IEdmType defaultPayloadType,
IEdmModel model,
ODataMessageReaderSettings messageReaderSettings)
{
Debug.Assert(messageReaderSettings != null, "messageReaderSettings != null");
Debug.Assert(
payloadTypeKind == EdmTypeKind.Primitive || payloadTypeKind == EdmTypeKind.Complex ||
payloadTypeKind == EdmTypeKind.Entity || payloadTypeKind == EdmTypeKind.Collection ||
payloadTypeKind == EdmTypeKind.None || payloadTypeKind == EdmTypeKind.TypeDefinition,
"The payload type kind must be one of None, Primitive, Complex, Entity, Collection or TypeDefinition.");
Debug.Assert(
expectedTypeReference == null || expectedTypeReference.TypeKind() == EdmTypeKind.Primitive,
"This method only works for primitive expected type.");
Debug.Assert(
payloadType == null || payloadType.TypeKind == payloadTypeKind,
"The payload type kind must match the payload type if that is available.");
Debug.Assert(payloadType == null || payloadTypeName != null, "If we have a payload type, we must have its name as well.");
bool useExpectedTypeOnlyForTypeResolution = messageReaderSettings.ReaderBehavior.TypeResolver != null && payloadType != null;
if (expectedTypeReference != null && !useExpectedTypeOnlyForTypeResolution)
{
ValidateTypeSupported(expectedTypeReference);
}
// Validate type kinds except for open properties or when in lax mode, but only if primitive type conversion is enabled.
// If primitive type conversion is disabled, the type kind must match, no matter what validation mode is used.
// The rules for primitive types are:
// - In the strict mode the payload value must be convertible to the expected type. So the payload type must be a primitive type.
// - In the lax mode the payload type is ignored, so its type kind is not verified in any way
// - If the DisablePrimitiveTypeConversion == true, the lax/strict mode doesn't matter and we will read the payload value on its own.
// In this case we require the payload value to always be a primitive type (so type kinds must match), but it may not be convertible
// to the expected type, it will still be reported to the caller.
if (payloadTypeKind != EdmTypeKind.None && (messageReaderSettings.DisablePrimitiveTypeConversion || !messageReaderSettings.DisableStrictMetadataValidation))
{
// Make sure that the type kinds match.
ValidationUtils.ValidateTypeKind(payloadTypeKind, EdmTypeKind.Primitive, payloadTypeName);
}
if (!model.IsUserModel())
{
// If there's no model, it means we should not have the expected type either, and that there's no type to use,
// no metadata validation to perform.
Debug.Assert(expectedTypeReference == null, "If we don't have a model, we must not have expected type either.");
return MetadataUtils.GetNullablePayloadTypeReference(payloadType ?? defaultPayloadType);
}
// If the primitive type conversion is off, use the payload type always.
// If there's no expected type or the expected type is ignored, use the payload type as well.
if (expectedTypeReference == null || useExpectedTypeOnlyForTypeResolution || messageReaderSettings.DisablePrimitiveTypeConversion)
{
// If there's no payload type, use the default payload type.
// Note that in collections the items without type should inherit the type name from the collection, in that case the expectedTypeReference
// is never null (assuming we do have a model), so we won't get here.
return MetadataUtils.GetNullablePayloadTypeReference(payloadType ?? defaultPayloadType);
}
// The server ignores the payload type when expected type is specified
// The server is going to use lax mode everywhere so this is not an issue.
if (messageReaderSettings.DisableStrictMetadataValidation)
{
// Lax validation logic
// Always use the expected type, the payload type is ignored.
return expectedTypeReference;
}
// Strict validation logic
// We assume the expected type in the case where no payload type is specified
// for a primitive value (in strict mode); if no expected type is available we assume Edm.String.
if (payloadType != null)
{
// The payload type must be convertible to the expected type.
// Note that we compare the type definitions, since we want to ignore nullability (the payload type doesn't specify nullability).
if (!MetadataUtilsCommon.CanConvertPrimitiveTypeTo(
null /* sourceNodeOrNull */,
(IEdmPrimitiveType)payloadType.AsActualType(),
(IEdmPrimitiveType)(expectedTypeReference.Definition)))
{
throw new ODataException(Strings.ValidationUtils_IncompatibleType(payloadTypeName, expectedTypeReference.ODataFullName()));
}
}
// Read using the expected type.
//.........这里部分代码省略.........
示例9: ResolveAndValidateTargetTypeStrictValidationEnabled
/// <summary>
/// Resolves the payload type versus the expected type and validates that such combination is allowed when strict validation is enabled.
/// </summary>
/// <param name="expectedTypeKind">The expected type kind for the value.</param>
/// <param name="expectedTypeReference">The expected type reference, or null if no expected type is available.</param>
/// <param name="payloadType">The payload type, or null if the payload type was not specified, or it didn't resolve against the model.</param>
/// <returns>The target type reference to use for parsing the value.</returns>
private static IEdmTypeReference ResolveAndValidateTargetTypeStrictValidationEnabled(
EdmTypeKind expectedTypeKind,
IEdmTypeReference expectedTypeReference,
IEdmType payloadType)
{
// Strict validation logic
switch (expectedTypeKind)
{
case EdmTypeKind.Complex:
if (payloadType != null)
{
// The payload type must be compatible to the expected type.
VerifyComplexType(expectedTypeReference, payloadType, /* failIfNotRelated */ true);
// Use the payload type
return payloadType.ToTypeReference(/*nullable*/ true);
}
break;
case EdmTypeKind.Entity:
if (payloadType != null)
{
// The payload type must be assignable to the expected type.
IEdmTypeReference payloadTypeReference = payloadType.ToTypeReference(/*nullable*/ true);
ValidationUtils.ValidateEntityTypeIsAssignable((IEdmEntityTypeReference)expectedTypeReference, (IEdmEntityTypeReference)payloadTypeReference);
// Use the payload type
return payloadTypeReference;
}
break;
case EdmTypeKind.Enum:
if (payloadType != null && string.CompareOrdinal(payloadType.ODataFullName(), expectedTypeReference.ODataFullName()) != 0)
{
throw new ODataException(Strings.ValidationUtils_IncompatibleType(payloadType.ODataFullName(), expectedTypeReference.ODataFullName()));
}
break;
case EdmTypeKind.Collection:
// The type must be exactly equal - note that we intentionally ignore nullability of the items here, since the payload type
// can't specify that.
if (payloadType != null && !payloadType.IsElementTypeEquivalentTo(expectedTypeReference.Definition))
{
VerifyCollectionComplexItemType(expectedTypeReference, payloadType);
throw new ODataException(Strings.ValidationUtils_IncompatibleType(payloadType.ODataFullName(), expectedTypeReference.ODataFullName()));
}
break;
case EdmTypeKind.TypeDefinition:
if (payloadType != null && !expectedTypeReference.Definition.IsAssignableFrom(payloadType))
{
throw new ODataException(Strings.ValidationUtils_IncompatibleType(payloadType.ODataFullName(), expectedTypeReference.ODataFullName()));
}
break;
default:
throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ReaderValidationUtils_ResolveAndValidateTypeName_Strict_TypeKind));
}
// Either there's no payload type, in which case use the expected one, or the payload one and the expected one are equal.
return expectedTypeReference;
}
示例10: ValidateMetadataType
/// <summary>
/// Validates that the (optional) <paramref name="typeReferenceFromMetadata"/> is the same as the (optional) <paramref name="typeReferenceFromValue"/>.
/// </summary>
/// <param name="typeReferenceFromMetadata">The (optional) type from the metadata definition (the expected type).</param>
/// <param name="typeReferenceFromValue">The (optional) type from the value (the actual type).</param>
/// <returns>The type as derived from the <paramref name="typeReferenceFromMetadata"/> and/or <paramref name="typeReferenceFromValue"/>.</returns>
private static IEdmTypeReference ValidateMetadataType(IEdmTypeReference typeReferenceFromMetadata, IEdmTypeReference typeReferenceFromValue)
{
if (typeReferenceFromMetadata == null)
{
// if we have no metadata information there is nothing to validate
return typeReferenceFromValue;
}
if (typeReferenceFromValue == null)
{
// derive the property type from the metadata
return typeReferenceFromMetadata;
}
Debug.Assert(typeReferenceFromValue.TypeKind() == typeReferenceFromMetadata.TypeKind(), "typeReferenceFromValue.TypeKind() == typeReferenceFromMetadata.TypeKind()");
// Make sure the types are the same
if (typeReferenceFromValue.IsODataPrimitiveTypeKind())
{
// Primitive types must match exactly except for nullability
ValidationUtils.ValidateMetadataPrimitiveType(typeReferenceFromMetadata, typeReferenceFromValue);
}
else if (typeReferenceFromMetadata.IsEntity())
{
ValidationUtils.ValidateEntityTypeIsAssignable((IEdmEntityTypeReference)typeReferenceFromMetadata, (IEdmEntityTypeReference)typeReferenceFromValue);
}
else if (typeReferenceFromMetadata.IsComplex())
{
ValidationUtils.ValidateComplexTypeIsAssignable(typeReferenceFromMetadata.Definition as IEdmComplexType, typeReferenceFromValue.Definition as IEdmComplexType);
}
else if (typeReferenceFromMetadata.IsCollection())
{
// Collection types must match exactly.
if (!(typeReferenceFromMetadata.Definition.IsElementTypeEquivalentTo(typeReferenceFromValue.Definition)))
{
throw new ODataException(Strings.ValidationUtils_IncompatibleType(typeReferenceFromValue.ODataFullName(), typeReferenceFromMetadata.ODataFullName()));
}
}
else
{
// For other types, compare their full type name.
if (typeReferenceFromMetadata.ODataFullName() != typeReferenceFromValue.ODataFullName())
{
throw new ODataException(Strings.ValidationUtils_IncompatibleType(typeReferenceFromValue.ODataFullName(), typeReferenceFromMetadata.ODataFullName()));
}
}
return typeReferenceFromValue;
}
示例11: ResolveAndValidateTargetTypeStrictValidationEnabled
private static IEdmTypeReference ResolveAndValidateTargetTypeStrictValidationEnabled(EdmTypeKind expectedTypeKind, IEdmTypeReference expectedTypeReference, IEdmType payloadType, string payloadTypeName, out SerializationTypeNameAnnotation serializationTypeNameAnnotation)
{
switch (expectedTypeKind)
{
case EdmTypeKind.Entity:
{
if (payloadType == null)
{
break;
}
IEdmTypeReference targetTypeReference = payloadType.ToTypeReference(true);
ValidationUtils.ValidateEntityTypeIsAssignable((IEdmEntityTypeReference) expectedTypeReference, (IEdmEntityTypeReference) targetTypeReference);
serializationTypeNameAnnotation = CreateSerializationTypeNameAnnotation(payloadTypeName, targetTypeReference);
return targetTypeReference;
}
case EdmTypeKind.Complex:
if (payloadType != null)
{
VerifyComplexType(expectedTypeReference, payloadType, true);
}
break;
case EdmTypeKind.Collection:
if ((payloadType != null) && (string.CompareOrdinal(payloadType.ODataFullName(), expectedTypeReference.ODataFullName()) != 0))
{
VerifyCollectionComplexItemType(expectedTypeReference, payloadType);
throw new ODataException(Microsoft.Data.OData.Strings.ValidationUtils_IncompatibleType(payloadType.ODataFullName(), expectedTypeReference.ODataFullName()));
}
break;
default:
throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodes.ReaderValidationUtils_ResolveAndValidateTypeName_Strict_TypeKind));
}
serializationTypeNameAnnotation = CreateSerializationTypeNameAnnotation(payloadTypeName, expectedTypeReference);
return expectedTypeReference;
}
示例12: ValidateNullValueAllowed
/// <summary>
/// Validates that the specified <paramref name="expectedValueTypeReference"/> allows null values.
/// </summary>
/// <param name="expectedValueTypeReference">The expected type for the value, or null if no such type is available.</param>
/// <param name="validateNullValue">true to validate the null value; otherwise false.</param>
/// <param name="model">The model to use to get the data service version.</param>
private static void ValidateNullValueAllowed(IEdmTypeReference expectedValueTypeReference, bool validateNullValue, IEdmModel model)
{
DebugUtils.CheckNoExternalCallers();
Debug.Assert(model != null, "For null validation, model is required.");
if (validateNullValue && expectedValueTypeReference != null)
{
Debug.Assert(
expectedValueTypeReference.IsODataPrimitiveTypeKind() ||
expectedValueTypeReference.IsODataComplexTypeKind() ||
expectedValueTypeReference.IsNonEntityODataCollectionTypeKind(),
"Only primitive, complex and collection types are supported by this method.");
if (expectedValueTypeReference.IsODataPrimitiveTypeKind())
{
if (!expectedValueTypeReference.IsNullable)
{
throw new ODataException(Strings.ReaderValidationUtils_NullValueForNonNullableType(expectedValueTypeReference.ODataFullName()));
}
}
else if (expectedValueTypeReference.IsNonEntityODataCollectionTypeKind())
{
throw new ODataException(Strings.ReaderValidationUtils_NullValueForNonNullableType(expectedValueTypeReference.ODataFullName()));
}
else if (expectedValueTypeReference.IsODataComplexTypeKind())
{
if (ValidationUtils.ShouldValidateComplexPropertyNullValue(model))
{
IEdmComplexTypeReference complexTypeReference = expectedValueTypeReference.AsComplex();
if (!complexTypeReference.IsNullable)
{
throw new ODataException(Strings.ReaderValidationUtils_NullValueForNonNullableType(expectedValueTypeReference.ODataFullName()));
}
}
}
}
}
示例13: ResolveAndValidatePrimitiveTargetType
/// <summary>
/// Resolves the primitive payload type versus the expected type and validates that such combination is allowed.
/// </summary>
/// <param name="expectedTypeReference">The expected type reference, if any.</param>
/// <param name="payloadTypeKind">The kind of the payload type, or None if the detection was not possible.</param>
/// <param name="payloadType">The resolved payload type, or null if no payload type was specified.</param>
/// <param name="payloadTypeName">The name of the payload type, or null if no payload type was specified.</param>
/// <param name="defaultPayloadType">The default payload type if none is specified in the payload;
/// for ATOM this is Edm.String, for JSON it is null since there is no payload type name for primitive types in the payload.</param>
/// <param name="model">The model to use.</param>
/// <param name="messageReaderSettings">The message reader settings to use.</param>
/// <param name="version">The version of the payload being read.</param>
/// <returns>The target type reference to use for parsing the value. This method never returns null.</returns>
internal static IEdmTypeReference ResolveAndValidatePrimitiveTargetType(
IEdmTypeReference expectedTypeReference,
EdmTypeKind payloadTypeKind,
IEdmType payloadType,
string payloadTypeName,
IEdmType defaultPayloadType,
IEdmModel model,
ODataMessageReaderSettings messageReaderSettings,
ODataVersion version)
{
DebugUtils.CheckNoExternalCallers();
Debug.Assert(messageReaderSettings != null, "messageReaderSettings != null");
Debug.Assert(
payloadTypeKind == EdmTypeKind.Primitive || payloadTypeKind == EdmTypeKind.Complex ||
payloadTypeKind == EdmTypeKind.Entity || payloadTypeKind == EdmTypeKind.Collection ||
payloadTypeKind == EdmTypeKind.None,
"The payload type kind must be one of None, Primitive, Complex, Entity or Collection.");
Debug.Assert(
expectedTypeReference == null || expectedTypeReference.TypeKind() == EdmTypeKind.Primitive,
"This method only works for primitive expected type.");
Debug.Assert(
payloadType == null || payloadType.TypeKind == payloadTypeKind,
"The payload type kind must match the payload type if that is available.");
Debug.Assert(payloadType == null || payloadTypeName != null, "If we have a payload type, we must have its name as well.");
bool useExpectedTypeOnlyForTypeResolution = messageReaderSettings.ReaderBehavior.TypeResolver != null && payloadType != null;
if (expectedTypeReference != null && !useExpectedTypeOnlyForTypeResolution)
{
ValidateTypeSupported(expectedTypeReference, version);
}
// Validate type kinds except for open properties or when in lax mode, but only if primitive type conversion is enabled.
if (payloadTypeKind != EdmTypeKind.None && (messageReaderSettings.DisablePrimitiveTypeConversion || !messageReaderSettings.DisableStrictMetadataValidation))
{
// Make sure that the type kinds match.
ValidationUtils.ValidateTypeKind(payloadTypeKind, EdmTypeKind.Primitive, payloadTypeName);
}
if (!model.IsUserModel())
{
// If there's no model, it means we should not have the expected type either, and that there's no type to use,
// no metadata validation to perform.
Debug.Assert(expectedTypeReference == null, "If we don't have a model, we must not have expected type either.");
return GetNullablePayloadTypeReference(payloadType ?? defaultPayloadType);
}
// If the primitive type conversion is off, use the payload type always.
// If there's no expected type or the expected type is ignored, use the payload type as well.
if (expectedTypeReference == null || useExpectedTypeOnlyForTypeResolution || messageReaderSettings.DisablePrimitiveTypeConversion)
{
// If there's no payload type, use the default payload type.
// Note that in collections the items without type should inherit the type name from the collection, in that case the expectedTypeReference
// is never null (assuming we do have a model), so we won't get here.
return GetNullablePayloadTypeReference(payloadType ?? defaultPayloadType);
}
if (messageReaderSettings.DisableStrictMetadataValidation)
{
// Lax validation logic
// Always use the expected type, the payload type is ignored.
return expectedTypeReference;
}
// Strict validation logic
if (payloadType != null)
{
// The payload type must be convertible to the expected type.
// Note that we compare the type definitions, since we want to ignore nullability (the payload type doesn't specify nullability).
if (!MetadataUtilsCommon.CanConvertPrimitiveTypeTo(
(IEdmPrimitiveType)payloadType,
(IEdmPrimitiveType)(expectedTypeReference.Definition)))
{
throw new ODataException(Strings.ValidationUtils_IncompatibleType(payloadTypeName, expectedTypeReference.ODataFullName()));
}
}
// Read using the expected type.
// If there was a payload type we verified that it's convertible and thus we can safely read
// the content of the value as the expected type.
return expectedTypeReference;
}
示例14: Create
/// <summary>
/// Create ODataContextUrlInfo from ODataCollectionStartSerializationInfo
/// </summary>
/// <param name="info">The ODataCollectionStartSerializationInfo to be used.</param>
/// <param name="itemTypeReference">ItemTypeReference specifying element type.</param>
/// <returns>The generated ODataContextUrlInfo.</returns>
internal static ODataContextUrlInfo Create(ODataCollectionStartSerializationInfo info, IEdmTypeReference itemTypeReference)
{
string collectionTypeName = null;
if (info != null)
{
collectionTypeName = info.CollectionTypeName;
}
else if (itemTypeReference != null)
{
collectionTypeName = EdmLibraryExtensions.GetCollectionTypeName(itemTypeReference.ODataFullName());
}
return new ODataContextUrlInfo()
{
TypeName = collectionTypeName,
};
}
示例15: ConvertToTypeIfNeeded
/// <summary>
/// If the source node is not of the specified type, then we check if type promotion is possible and inject a convert node.
/// If the source node is the same type as the target type (or if the target type is null), we just return the source node as is.
/// </summary>
/// <param name="source">The source node to apply the convertion to.</param>
/// <param name="targetTypeReference">The target primitive type. May be null - this method will do nothing in that case.</param>
/// <returns>The converted query node, or the original source node unchanged.</returns>
internal static SingleValueNode ConvertToTypeIfNeeded(SingleValueNode source, IEdmTypeReference targetTypeReference)
{
Debug.Assert(source != null, "source != null");
if (targetTypeReference == null)
{
return source;
}
if (source.TypeReference != null)
{
if (source.TypeReference.IsEquivalentTo(targetTypeReference))
{
// For source is type definition, if source's underlying type == target type.
// We create a conversion node from source to its underlying type (target type)
// so that the service can convert value of source clr type to underlying clr type.
if (source.TypeReference.IsTypeDefinition())
{
return new ConvertNode(source, targetTypeReference);
}
return source;
}
if (!TypePromotionUtils.CanConvertTo(source, source.TypeReference, targetTypeReference))
{
throw new ODataException(ODataErrorStrings.MetadataBinder_CannotConvertToType(source.TypeReference.ODataFullName(), targetTypeReference.ODataFullName()));
}
else
{
ConstantNode constantNode = source as ConstantNode;
if (source.TypeReference.IsEnum() && constantNode != null)
{
return new ConstantNode(constantNode.Value, ODataUriUtils.ConvertToUriLiteral(constantNode.Value, ODataVersion.V4), targetTypeReference);
}
object originalPrimitiveValue;
if (MetadataUtilsCommon.TryGetConstantNodePrimitiveDate(source, out originalPrimitiveValue) && (originalPrimitiveValue != null))
{
// DateTimeOffset -> Date when (target is Date) and (originalValue match Date format) and (ConstantNode)
object targetPrimitiveValue = ODataUriConversionUtils.CoerceTemporalType(originalPrimitiveValue, targetTypeReference.AsPrimitive().Definition as IEdmPrimitiveType);
if (targetPrimitiveValue != null)
{
if (string.IsNullOrEmpty(constantNode.LiteralText))
{
return new ConstantNode(targetPrimitiveValue);
}
return new ConstantNode(targetPrimitiveValue, constantNode.LiteralText, targetTypeReference);
}
}
if (MetadataUtilsCommon.TryGetConstantNodePrimitiveLDMF(source, out originalPrimitiveValue) && (originalPrimitiveValue != null))
{
// L F D M types : directly create a ConvertNode.
// 1. NodeToExpressionTranslator.cs won't allow implicitly converting single/double to decimal, which should be done here at Node tree level.
// 2. And prevent losing precision in float -> double, e.g. (double)1.234f => 1.2339999675750732d not 1.234d
object targetPrimitiveValue = ODataUriConversionUtils.CoerceNumericType(originalPrimitiveValue, targetTypeReference.AsPrimitive().Definition as IEdmPrimitiveType);
if (string.IsNullOrEmpty(constantNode.LiteralText))
{
return new ConstantNode(targetPrimitiveValue);
}
return new ConstantNode(targetPrimitiveValue, constantNode.LiteralText);
}
else
{
// other type conversion : ConvertNode
return new ConvertNode(source, targetTypeReference);
}
}
}
else
{
// If the source doesn't have a type (possibly an open property), then it's possible to convert it
// cause we don't know for sure.
return new ConvertNode(source, targetTypeReference);
}
}