Transform() public method

public Transform ( object objectGraph, Context context ) : CompoundDocument
objectGraph object
context Context
return NJsonApi.Serialization.Documents.CompoundDocument
コード例 #1
0
        public async Task <HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func <Task <HttpResponseMessage> > continuation)
        {
            InternalActionExecuting(actionContext, cancellationToken);

            if (actionContext.Response != null)
            {
                return(actionContext.Response);
            }

            HttpActionExecutedContext executedContext;

            try
            {
                var response = await continuation();

                executedContext = new HttpActionExecutedContext(actionContext, null)
                {
                    Response = response
                };
                InternalActionExecuted(executedContext, cancellationToken);
            }
            catch (Exception exception)
            {
                var context = new Context
                {
                    Configuration = configuration,
                    RoutePrefix   = string.Empty
                };

                executedContext = new HttpActionExecutedContext(actionContext, exception);
                if (executedContext.Response == null)
                {
                    var nJsonApiBaseException = exception as NJsonApiBaseException;
                    if (nJsonApiBaseException != null)
                    {
                        executedContext.Response = new HttpResponseMessage(nJsonApiBaseException.GetHttpStatusCode());
                        var transformed      = jsonApiTransformer.Transform(nJsonApiBaseException, context);
                        var jsonApiFormatter = new JsonApiFormatter(configuration, jsonApiTransformer.Serializer);
                        executedContext.Response.Content = new ObjectContent(transformed.GetType(), transformed, jsonApiFormatter);
                    }
                    else
                    {
                        executedContext.Response         = executedContext.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, new NJsonApiBaseException("Internal Server Error"));
                        executedContext.Response.Content = new HttpMessageContent(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                    }
                }
            }

            return(executedContext.Response);
        }
コード例 #2
0
        public void Creates_CompondDocument_for_metadatawrapper_single_not_nested_class_and_propertly_map_id()
        {
            // Arrange
            var context = CreateContext();
            MetaDataWrapper<SampleClass> objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            CompoundDocument result = sut.Transform(objectToTransform, context);

            // Assert
            var transformedObject = result.Data as SingleResource;
            transformedObject.Id.ShouldEqual(objectToTransform.Value.Id.ToString());
        }
コード例 #3
0
        public void Creates_CompondDocument_for_single_not_nested_class_and_propertly_map_href()
        {
            // Arrange
            var context = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            CompoundDocument result = sut.Transform(objectToTransform, context);

            // Assert
            var transformedObject = result.Data as SingleResource;
            transformedObject.Links["self"].ToString().ShouldEqual("http://sampleclass/1");
        }
コード例 #4
0
        public void Creates_CompondDocument_for_single_not_nested_class_and_propertly_map_resourceName()
        {
            // Arrange
            var context = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            CompoundDocument result = sut.Transform(objectToTransform, context);

            // Assert
            result.Data.ShouldNotBeNull();
            var transformedObject = result.Data as SingleResource;
            transformedObject.ShouldNotBeNull();
        }
コード例 #5
0
        public void Serilized_properly()
        {
            // Arrange
            var configuration = CreateContext();
            var objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            var transformed = sut.Transform(objectToTransform, configuration);

            // Act
            var json = JsonConvert.SerializeObject(transformed);

            // Assert
            json.ShouldNotContain("Data");
        }
コード例 #6
0
        public void Creates_CompondDocument_for_single_not_nested_class_and_propertly_map_properties()
        {
            // Arrange
            var context = CreateContext();
            SampleClass objectToTransform = CreateObjectToTransform();
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            CompoundDocument result = sut.Transform(objectToTransform, context);

            // Assert
            var transformedObject = result.Data as SingleResource;
            transformedObject.Attributes["someValue"].ShouldEqual(objectToTransform.SomeValue);
            transformedObject.Attributes["date"].ShouldEqual(objectToTransform.DateTime);
            transformedObject.Attributes.ShouldHaveCountOf(2);
        }
コード例 #7
0
        public void Creates_CompondDocument_for_metadatawrapper_single_not_nested_class_and_propertly_map_metadata()
        {
            // Arrange
            const string pagingValue = "1";
            const string countValue = "2";

            var context = CreateContext();
            MetaDataWrapper<SampleClass> objectToTransform = CreateObjectToTransform();
            objectToTransform.MetaData.Add("Paging", pagingValue);
            objectToTransform.MetaData.Add("Count", countValue);
            var sut = new JsonApiTransformer() { TransformationHelper = new TransformationHelper() };

            // Act
            CompoundDocument result = sut.Transform(objectToTransform, context);

            // Assert
            var transformedObjectMetadata = result.Meta;
            transformedObjectMetadata["Paging"].ShouldEqual(pagingValue);
            transformedObjectMetadata["Count"].ShouldEqual(countValue);
        }