private AtomicResultObject SerializeOperation(OperationContainer operation)
        {
            ResourceObject resourceObject = null;

            if (operation != null)
            {
                _request.CopyFrom(operation.Request);
                _fieldsToSerialize.ResetCache();
                _evaluatedIncludeCache.Set(null);

                _resourceDefinitionAccessor.OnSerialize(operation.Resource);

                Type resourceType = operation.Resource.GetType();
                IReadOnlyCollection <AttrAttribute>         attributes    = _fieldsToSerialize.GetAttributes(resourceType);
                IReadOnlyCollection <RelationshipAttribute> relationships = _fieldsToSerialize.GetRelationships(resourceType);

                resourceObject = ResourceObjectBuilder.Build(operation.Resource, attributes, relationships);
            }

            if (resourceObject != null)
            {
                resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
            }

            return(new AtomicResultObject
            {
                Data = resourceObject
            });
        }
Esempio n. 2
0
        protected virtual void ValidateModelState(IEnumerable <OperationContainer> operations)
        {
            // We must validate the resource inside each operation manually, because they are typed as IIdentifiable.
            // Instead of validating IIdentifiable we need to validate the resource runtime-type.

            var violations = new List <ModelStateViolation>();

            int index = 0;

            foreach (OperationContainer operation in operations)
            {
                if (operation.Kind == OperationKind.CreateResource || operation.Kind == OperationKind.UpdateResource)
                {
                    _targetedFields.Attributes    = operation.TargetedFields.Attributes;
                    _targetedFields.Relationships = operation.TargetedFields.Relationships;

                    _request.CopyFrom(operation.Request);

                    var validationContext = new ActionContext();
                    ObjectValidator.Validate(validationContext, null, string.Empty, operation.Resource);

                    if (!validationContext.ModelState.IsValid)
                    {
                        AddValidationErrors(validationContext.ModelState, operation.Resource.GetType(), index, violations);
                    }
                }

                index++;
            }

            if (violations.Any())
            {
                throw new InvalidModelStateException(violations, _options.IncludeExceptionStackTraceInErrors, _options.SerializerNamingStrategy);
            }
        }
        private AtomicResultObject SerializeOperation(OperationContainer operation)
        {
            ResourceObject resourceObject = null;

            if (operation != null)
            {
                _request.CopyFrom(operation.Request);
                _fieldsToSerialize.ResetCache();

                var resourceType  = operation.Resource.GetType();
                var attributes    = _fieldsToSerialize.GetAttributes(resourceType);
                var relationships = _fieldsToSerialize.GetRelationships(resourceType);

                resourceObject = ResourceObjectBuilder.Build(operation.Resource, attributes, relationships);
            }

            if (resourceObject != null)
            {
                resourceObject.Links = _linkBuilder.GetResourceLinks(resourceObject.Type, resourceObject.Id);
            }

            return(new AtomicResultObject
            {
                Data = resourceObject
            });
        }
        protected virtual async Task <OperationContainer> ProcessOperationAsync(OperationContainer operation, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            TrackLocalIdsForOperation(operation);

            _targetedFields.Attributes    = operation.TargetedFields.Attributes;
            _targetedFields.Relationships = operation.TargetedFields.Relationships;

            _request.CopyFrom(operation.Request);

            return(await _operationProcessorAccessor.ProcessAsync(operation, cancellationToken));
        }