/// <summary>
        /// Validates an <see cref="ODataResourceBase"/> to ensure all required information is specified and valid on WriteEnd call.
        /// </summary>
        /// <param name="resource">The resource to validate.</param>
        internal static void ValidateResourceAtEnd(ODataResourceBase resource)
        {
            Debug.Assert(resource != null, "resource != null");

            // If the Id was not specified in the beginning it might have been specified at the end, so validate it here as well.
            ValidateResourceId(resource.Id);
        }
        /// <summary>
        /// Validates an <see cref="ODataResourceBase"/> to ensure all required information is specified and valid on WriteStart call.
        /// </summary>
        /// <param name="resource">The resource to validate.</param>
        internal static void ValidateResourceAtStart(ODataResourceBase resource)
        {
            Debug.Assert(resource != null, "resource != null");

            // Id can be specified at the beginning (and might be written here), so we need to validate it here.
            ValidateResourceId(resource.Id);

            // Type name is verified in the format readers/writers since it's shared with other non-entity types
            // and verifying it here would mean doing it twice.
        }
        /// <summary>
        /// Writes an <see cref="ODataResourceBase"/> as either a resource or a deleted resource.
        /// </summary>
        /// <param name="writer">The <see cref="ODataWriter"/> to use to write the (deleted) resource.</param>
        /// <param name="resource">The resource, or deleted resource, to write.</param>
        private static void WriteStartResource(ODataWriter writer, ODataResourceBase resource)
        {
            ODataDeletedResource deletedResource = resource as ODataDeletedResource;

            if (deletedResource != null)
            {
                writer.WriteStart(deletedResource);
            }
            else
            {
                // will write a null resource if resource is not an ODataResource
                writer.WriteStart(resource as ODataResource);
            }
        }
        /// <summary>
        /// Converts a <see cref="ODataResourceBase"/> to a string for use in a Url.
        /// </summary>
        /// <param name="resource">Instance to convert.</param>
        /// <param name="model">Model to be used for validation. User model is optional. The EdmLib core model is expected as a minimum.</param>
        /// <returns>A string representation of <paramref name="resource"/> to be added to a Url.</returns>
        internal static string ConvertToUriEntityLiteral(ODataResourceBase resource, IEdmModel model)
        {
            ExceptionUtils.CheckArgumentNotNull(resource, "resource");
            ExceptionUtils.CheckArgumentNotNull(model, "model");

            return(ConvertToJsonLightLiteral(
                       model,
                       context =>
            {
                ODataWriter writer = context.CreateODataUriParameterResourceWriter(null, null);
                WriteStartResource(writer, resource);
                writer.WriteEnd();
            }));
        }
        /// <summary>
        /// If an entity type name is found in the payload this method is called to apply it to the current scope.
        /// This method should be called even if the type name was not found in which case a null should be passed in.
        /// The method validates that some type will be available as the current entity type after it returns (if we are parsing using metadata).
        /// </summary>
        /// <param name="resourceTypeNameFromPayload">The entity type name found in the payload or null if no type was specified in the payload.</param>
        protected void ApplyResourceTypeNameFromPayload(string resourceTypeNameFromPayload)
        {
            Debug.Assert(
                this.scopes.Count > 0 && this.scopes.Peek().Item is ODataResourceBase,
                "Resource type can be applied only when in resource scope.");

            ODataTypeAnnotation         typeAnnotation;
            EdmTypeKind                 targetTypeKind;
            IEdmStructuredTypeReference targetResourceTypeReference =
                this.inputContext.MessageReaderSettings.Validator.ResolvePayloadTypeNameAndComputeTargetType(
                    EdmTypeKind.None,
                    /*expectStructuredType*/ true,
                    /*defaultPrimitivePayloadType*/ null,
                    this.CurrentResourceTypeReference,
                    resourceTypeNameFromPayload,
                    this.inputContext.Model,
                    () => EdmTypeKind.Entity,
                    out targetTypeKind,
                    out typeAnnotation) as IEdmStructuredTypeReference;

            IEdmStructuredType targetResourceType = null;
            ODataResourceBase  resource           = this.Item as ODataResourceBase;

            if (targetResourceTypeReference != null)
            {
                targetResourceType = targetResourceTypeReference.StructuredDefinition();
                resource.TypeName  = targetResourceType.FullTypeName();

                if (typeAnnotation != null)
                {
                    resource.TypeAnnotation = typeAnnotation;
                }
            }
            else if (resourceTypeNameFromPayload != null)
            {
                resource.TypeName = resourceTypeNameFromPayload;
            }
            else if (this.CurrentResourceTypeReference.IsUntyped())
            {
                targetResourceTypeReference = this.CurrentResourceTypeReference.IsNullable ?
                                              EdmUntypedStructuredTypeReference.NullableTypeReference :
                                              EdmUntypedStructuredTypeReference.NonNullableTypeReference;
            }

            // Set the current resource type since the type might be derived from the expected one.
            this.CurrentResourceTypeReference = targetResourceTypeReference;
        }
Beispiel #6
0
        /// <summary>
        /// Validates that the specified <paramref name="resource"/> is a valid resource as per the specified type.
        /// </summary>
        /// <param name="resource">The resource to validate.</param>
        /// <param name="resourceType">Optional entity type to validate the resource against.</param>
        /// <remarks>If the <paramref name="resourceType"/> is available only resource-level tests are performed, properties and such are not validated.</remarks>
        internal static void ValidateMediaResource(ODataResourceBase resource, IEdmEntityType resourceType)
        {
            Debug.Assert(resource != null, "resource != null");

            if (resourceType != null)
            {
                if (resource.MediaResource == null)
                {
                    if (resourceType.HasStream)
                    {
                        throw new ODataException(Strings.ValidationUtils_ResourceWithoutMediaResourceAndMLEType(resourceType.FullTypeName()));
                    }
                }
                else
                {
                    if (!resourceType.HasStream)
                    {
                        throw new ODataException(Strings.ValidationUtils_ResourceWithMediaResourceAndNonMLEType(resourceType.FullTypeName()));
                    }
                }
            }
        }
Beispiel #7
0
        public static string ConvertToUriLiteral(object value, ODataVersion version, IEdmModel model)
        {
            if (value == null)
            {
                value = new ODataNullValue();
            }

            if (model == null)
            {
                model = Microsoft.OData.Edm.EdmCoreModel.Instance;
            }

            ODataNullValue nullValue = value as ODataNullValue;

            if (nullValue != null)
            {
                return(ExpressionConstants.KeywordNull);
            }

            ODataResourceValue resourceValue = value as ODataResourceValue;

            if (resourceValue != null)
            {
                return(ODataUriConversionUtils.ConvertToResourceLiteral(resourceValue, model, version));
            }

            ODataCollectionValue collectionValue = value as ODataCollectionValue;

            if (collectionValue != null)
            {
                return(ODataUriConversionUtils.ConvertToUriCollectionLiteral(collectionValue, model, version));
            }

            ODataEnumValue enumValue = value as ODataEnumValue;

            if (enumValue != null)
            {
                return(ODataUriConversionUtils.ConvertToUriEnumLiteral(enumValue, version));
            }

            ODataResourceBase resource = value as ODataResourceBase;

            if (resource != null)
            {
                return(ODataUriConversionUtils.ConvertToUriEntityLiteral(resource, model));
            }

            ODataEntityReferenceLink link = value as ODataEntityReferenceLink;

            if (link != null)
            {
                return(ODataUriConversionUtils.ConvertToUriEntityReferenceLiteral(link, model));
            }

            ODataEntityReferenceLinks links = value as ODataEntityReferenceLinks;

            if (links != null)
            {
                return(ODataUriConversionUtils.ConvertToUriEntityReferencesLiteral(links, model));
            }

            IEnumerable <ODataResourceBase> list = value as IEnumerable <ODataResourceBase>;

            if (list != null)
            {
                return(ODataUriConversionUtils.ConvertToUriEntitiesLiteral(list, model));
            }

            // Try to convert uints to their underlying type first according to the model.
            value = model.ConvertToUnderlyingTypeIfUIntValue(value);

            return(ODataUriConversionUtils.ConvertToUriPrimitiveLiteral(value, version));
        }
Beispiel #8
0
 /// <summary>
 /// Validates that the specified <paramref name="resource"/> is a valid resource as per the
 /// specified type.
 /// </summary>
 /// <param name="resource">The resource to validate.</param>
 /// <param name="resourceType">Optional entity type to validate the resource against.</param>
 /// <remarks>
 /// If the <paramref name="resourceType"/> is available, only resource-level tests are
 /// performed; properties and such are not validated.
 /// </remarks>
 public virtual void ValidateMetadataResource(ODataResourceBase resource, IEdmEntityType resourceType)
 {
     ValidationUtils.ValidateMediaResource(resource, resourceType);
 }
 /// <summary>
 /// Provide additional serialization information to the <see cref="ODataWriter"/> for <paramref name="resource"/>.
 /// </summary>
 /// <param name="resource">The instance to set the serialization info.</param>
 /// <param name="serializationInfo">The serialization info to set.</param>
 public static void SetSerializationInfo(this ODataResourceBase resource, ODataResourceSerializationInfo serializationInfo)
 {
     ExceptionUtils.CheckArgumentNotNull(resource, "resource");
     resource.SerializationInfo = serializationInfo;
 }