protected override void ConfigureJsonApiOptions(JsonApiOptions options)
        {
            base.ConfigureJsonApiOptions(options);

            ((DefaultContractResolver)options.SerializerSettings.ContractResolver).NamingStrategy = new KebabCaseNamingStrategy();
        }
 protected virtual void SetJsonApiOptions(JsonApiOptions options)
 {
     options.IncludeExceptionStackTraceInErrors = true;
     options.SerializerSettings.Formatting      = Formatting.Indented;
     options.SerializerSettings.Converters.Add(new StringEnumConverter());
 }
Exemplo n.º 3
0
        protected override void ConfigureJsonApiOptions(JsonApiOptions options)
        {
            base.ConfigureJsonApiOptions(options);

            options.DefaultPageSize = 0;
        }
 public BenchmarkFacade(
     IRequestContext currentRequest,
     JsonApiOptions options) : base(currentRequest, options)
 {
 }
        public void Applies_cascading_settings_for_top_level_links(LinkTypes linksInResourceContext, LinkTypes linksInOptions, LinkTypes expected)
        {
            // Arrange
            var exampleResourceContext = new ResourceContext
            {
                PublicName    = nameof(ExampleResource),
                ResourceType  = typeof(ExampleResource),
                TopLevelLinks = linksInResourceContext
            };

            var options = new JsonApiOptions
            {
                TopLevelLinks = linksInOptions
            };

            var request = new JsonApiRequest
            {
                PrimaryResource = exampleResourceContext,
                PrimaryId       = "1",
                IsCollection    = true,
                Kind            = EndpointKind.Relationship,
                Relationship    = new HasOneAttribute()
            };

            var paginationContext = new PaginationContext
            {
                PageSize           = new PageSize(1),
                PageNumber         = new PageNumber(2),
                TotalResourceCount = 10
            };

            var resourceGraph             = new ResourceGraph(exampleResourceContext.AsArray());
            var httpContextAccessor       = new FakeHttpContextAccessor();
            var linkGenerator             = new FakeLinkGenerator();
            var controllerResourceMapping = new FakeControllerResourceMapping();

            var linkBuilder = new LinkBuilder(options, request, paginationContext, resourceGraph, httpContextAccessor, linkGenerator,
                                              controllerResourceMapping);

            // Act
            TopLevelLinks topLevelLinks = linkBuilder.GetTopLevelLinks();

            // Assert
            if (expected == LinkTypes.None)
            {
                topLevelLinks.Should().BeNull();
            }
            else
            {
                if (expected.HasFlag(LinkTypes.Self))
                {
                    topLevelLinks.Self.Should().NotBeNull();
                }
                else
                {
                    topLevelLinks.Self.Should().BeNull();
                }

                if (expected.HasFlag(LinkTypes.Related))
                {
                    topLevelLinks.Related.Should().NotBeNull();
                }
                else
                {
                    topLevelLinks.Related.Should().BeNull();
                }

                if (expected.HasFlag(LinkTypes.Paging))
                {
                    topLevelLinks.First.Should().NotBeNull();
                    topLevelLinks.Last.Should().NotBeNull();
                    topLevelLinks.Prev.Should().NotBeNull();
                    topLevelLinks.Next.Should().NotBeNull();
                }
                else
                {
                    topLevelLinks.First.Should().BeNull();
                    topLevelLinks.Last.Should().BeNull();
                    topLevelLinks.Prev.Should().BeNull();
                    topLevelLinks.Next.Should().BeNull();
                }
            }
        }
        protected override void SetJsonApiOptions(JsonApiOptions options)
        {
            base.SetJsonApiOptions(options);

            options.ValidateModelState = true;
        }
Exemplo n.º 7
0
 public JsonApiCoreWithClientOptions()
 {
     Core   = new JsonApiOptions();
     Client = new ClientOptions();
 }
Exemplo n.º 8
0
        public void Sets_Attribute_Values_On_Included_HasOne_Relationships()
        {
            // arrange
            var resourceGraphBuilder = new ResourceGraphBuilder();

            resourceGraphBuilder.AddResource <OneToManyIndependent>("independents");
            resourceGraphBuilder.AddResource <OneToManyDependent>("dependents");
            var resourceGraph = resourceGraphBuilder.Build();

            var jsonApiContextMock = new Mock <IJsonApiContext>();

            jsonApiContextMock.SetupAllProperties();
            jsonApiContextMock.Setup(m => m.ResourceGraph).Returns(resourceGraph);
            jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary <AttrAttribute, object>());
            jsonApiContextMock.Setup(m => m.RelationshipsToUpdate).Returns(new Dictionary <RelationshipAttribute, object>());
            jsonApiContextMock.Setup(m => m.HasManyRelationshipPointers).Returns(new HasManyRelationshipPointers());
            jsonApiContextMock.Setup(m => m.HasOneRelationshipPointers).Returns(new HasOneRelationshipPointers());

            var jsonApiOptions = new JsonApiOptions();

            jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);

            var deserializer = new JsonApiDeSerializer(jsonApiContextMock.Object);

            var expectedName  = "John Doe";
            var contentString =
                @"{
                ""data"": {
                    ""type"": ""dependents"",
                    ""id"": ""1"",
                    ""attributes"": { },
                    ""relationships"": {
                        ""independent"": {
                            ""data"": {
                                ""type"": ""independents"",
                                ""id"": ""2""
                            }
                        }
                    }
                },
                ""included"": [
                    {
                        ""type"": ""independents"",
                        ""id"": ""2"",
                        ""attributes"": {
                            ""name"": """ + expectedName + @"""
                        }
                    }
                ]
            }";

            // act
            var result = deserializer.Deserialize <OneToManyDependent>(contentString);

            // assert
            Assert.NotNull(result);
            Assert.Equal(1, result.Id);
            Assert.NotNull(result.Independent);
            Assert.Equal(2, result.Independent.Id);
            Assert.Equal(expectedName, result.Independent.Name);
        }
 public BenchmarkFacade(
     IControllerContext controllerContext,
     JsonApiOptions options) : base(controllerContext, options)
 {
 }
        protected override void ConfigureJsonApiOptions(JsonApiOptions options)
        {
            base.ConfigureJsonApiOptions(options);

            options.Namespace = null;
        }
 private static void AddMvcOptions(MvcOptions options, JsonApiOptions config)
 {
     options.Filters.Add(typeof(JsonApiExceptionFilter));
     options.Filters.Add(typeof(TypeMatchFilter));
     options.SerializeAsJsonApi(config);
 }
        public static void AddJsonApiInternals(
            this IServiceCollection services,
            JsonApiOptions jsonApiOptions)
        {
            if (jsonApiOptions.ResourceGraph == null)
            {
                jsonApiOptions.ResourceGraph = jsonApiOptions.ResourceGraphBuilder.Build();
            }

            if (jsonApiOptions.ResourceGraph.UsesDbContext == false)
            {
                services.AddScoped <DbContext>();
                services.AddSingleton(new DbContextOptionsBuilder().Options);
            }

            if (jsonApiOptions.EnableOperations)
            {
                AddOperationServices(services);
            }

            services.AddScoped(typeof(IEntityRepository <>), typeof(DefaultEntityRepository <>));
            services.AddScoped(typeof(IEntityRepository <,>), typeof(DefaultEntityRepository <,>));

            services.AddScoped(typeof(ICreateService <>), typeof(EntityResourceService <>));
            services.AddScoped(typeof(ICreateService <,>), typeof(EntityResourceService <,>));

            services.AddScoped(typeof(IGetAllService <>), typeof(EntityResourceService <>));
            services.AddScoped(typeof(IGetAllService <,>), typeof(EntityResourceService <,>));

            services.AddScoped(typeof(IGetByIdService <>), typeof(EntityResourceService <>));
            services.AddScoped(typeof(IGetByIdService <,>), typeof(EntityResourceService <,>));

            services.AddScoped(typeof(IGetRelationshipService <,>), typeof(EntityResourceService <>));
            services.AddScoped(typeof(IGetRelationshipService <,>), typeof(EntityResourceService <,>));

            services.AddScoped(typeof(IUpdateService <>), typeof(EntityResourceService <>));
            services.AddScoped(typeof(IUpdateService <,>), typeof(EntityResourceService <,>));

            services.AddScoped(typeof(IDeleteService <>), typeof(EntityResourceService <>));
            services.AddScoped(typeof(IDeleteService <,>), typeof(EntityResourceService <,>));

            services.AddScoped(typeof(IResourceService <>), typeof(EntityResourceService <>));
            services.AddScoped(typeof(IResourceService <,>), typeof(EntityResourceService <,>));

            services.AddSingleton(jsonApiOptions);
            services.AddSingleton(jsonApiOptions.ResourceGraph);
            services.AddScoped <IJsonApiContext, JsonApiContext>();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IScopedServiceProvider, RequestScopedServiceProvider>();
            services.AddScoped <JsonApiRouteHandler>();
            services.AddScoped <IMetaBuilder, MetaBuilder>();
            services.AddScoped <IDocumentBuilder, DocumentBuilder>();
            services.AddScoped <IJsonApiSerializer, JsonApiSerializer>();
            services.AddScoped <IJsonApiWriter, JsonApiWriter>();
            services.AddScoped <IJsonApiDeSerializer, JsonApiDeSerializer>();
            services.AddScoped <IJsonApiReader, JsonApiReader>();
            services.AddScoped <IGenericProcessorFactory, GenericProcessorFactory>();
            services.AddScoped(typeof(GenericProcessor <>));
            services.AddScoped <IQueryAccessor, QueryAccessor>();
            services.AddScoped <IQueryParser, QueryParser>();
            services.AddScoped <IControllerContext, Services.ControllerContext>();
            services.AddScoped <IDocumentBuilderOptionsProvider, DocumentBuilderOptionsProvider>();

            // services.AddScoped<IActionFilter, TypeMatchFilter>();
        }
Exemplo n.º 13
0
        public static IServiceCollection AddJsonApi(this IServiceCollection services, IMvcBuilder mvcBuilder,
                                                    ContainerBuilder containerBuilder, IConfiguration configuration,
                                                    Action <IMapperConfigurationExpression> configure)
        {
            // setup auto mapper
            var    siteOptions = configuration.GetOptions <SiteOptions>();
            string siteKey     = siteOptions.Id;

            services.AddAutoMapper(mapConfig =>
            {
                mapConfig.ValidateInlineMaps = false;
                mapConfig.AddProfile(new XFMapperProfile(siteKey));
                configure(mapConfig);
            }, new Assembly[0]);

            JsonApiOptions.ResourceNameFormatter = new XFResourceNameFormatter();
            var graphBuilder = new XFResourceGraphBuilder();
            // find all resources
            var assemblies = new[] { Assembly.GetEntryAssembly(), Assembly.GetExecutingAssembly() };

            ResourceDescriptor[] resourceDescriptors = assemblies
                                                       .SelectMany(a => ResourceTypeLocator.GetIdentifableTypes(a)).ToArray();
            foreach (ResourceDescriptor resourceDescriptor in resourceDescriptors)
            {
                // add resource to graph
                string resourceName = JsonApiOptions.ResourceNameFormatter.FormatResourceName(
                    resourceDescriptor.ResourceType);
                graphBuilder.AddResource(resourceDescriptor.ResourceType, resourceDescriptor.IdType, resourceName);

                // register resource service
                Type   serviceInterfaceType = typeof(IResourceService <,>);
                Type[] genericArguments     = new[] { resourceDescriptor.ResourceType, resourceDescriptor.IdType };
                Type   serviceType          = ResourceTypeLocator.GetGenericInterfaceImplementation(
                    resourceDescriptor.ResourceType.Assembly, serviceInterfaceType, genericArguments);
                if (serviceType != null)
                {
                    RegisterResourceService(containerBuilder, serviceType);
                }
            }

            var jsonApiOptions = new JsonApiOptions
            {
                Namespace               = XForgeConstants.JsonApiNamespace,
                ResourceGraph           = graphBuilder.Build(),
                AllowClientGeneratedIds = true,
                IncludeTotalRecordCount = true
            };

            jsonApiOptions.SerializerSettings.ContractResolver = new JsonApiContractResolver();
            jsonApiOptions.SerializerSettings.Converters.Add(new StringEnumConverter());

            mvcBuilder.AddMvcOptions(options =>
            {
                options.Filters.Add(typeof(JsonApiExceptionFilter));
                options.Filters.Add(typeof(TypeMatchFilter));
                SerializeAsJsonApi(options, jsonApiOptions);
            });

            services.AddJsonApiInternals(jsonApiOptions);
            services.AddScoped <IDocumentBuilder, XFDocumentBuilder>();

            // generate resource schema
            var schema = ResourceSchema.Build(jsonApiOptions.ResourceGraph, resourceDescriptors);

            services.AddSingleton(schema);

            return(services);
        }
Exemplo n.º 14
0
 private static void SerializeAsJsonApi(MvcOptions options, JsonApiOptions jsonApiOptions)
 {
     options.InputFormatters.Insert(0, new JsonApiInputFormatter());
     options.OutputFormatters.Insert(0, new JsonApiOutputFormatter());
     options.Conventions.Insert(0, new XFDasherizedRoutingConvention(jsonApiOptions.Namespace));
 }
Exemplo n.º 15
0
 public OrbitJSQueryParser(IControllerContext controllerContext, JsonApiOptions options) : base(controllerContext, options)
 {
 }