From f96e090069177fa8788e55ef1940eb50082f305e Mon Sep 17 00:00:00 2001 From: Bart Koelman <10324372+bkoelman@users.noreply.github.com> Date: Sat, 4 May 2024 11:47:39 +0200 Subject: [PATCH 1/2] Demonstrates how to hide some fields by default --- .../Definitions/BooksDefinition.cs | 31 +++++++++++++++++++ src/Examples/GettingStarted/Models/Book.cs | 9 ++++++ src/Examples/GettingStarted/Program.cs | 2 ++ .../Properties/launchSettings.json | 4 +-- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/Examples/GettingStarted/Definitions/BooksDefinition.cs diff --git a/src/Examples/GettingStarted/Definitions/BooksDefinition.cs b/src/Examples/GettingStarted/Definitions/BooksDefinition.cs new file mode 100644 index 0000000..01d30f4 --- /dev/null +++ b/src/Examples/GettingStarted/Definitions/BooksDefinition.cs @@ -0,0 +1,31 @@ +using GettingStarted.Models; +using JsonApiDotNetCore.Configuration; +using JsonApiDotNetCore.Queries.Expressions; +using JsonApiDotNetCore.QueryStrings; +using JsonApiDotNetCore.Resources; + +namespace GettingStarted.Definitions; + +public sealed class BooksDefinition : JsonApiResourceDefinition +{ + private readonly IRequestQueryStringAccessor _queryStringAccessor; + + public BooksDefinition(IResourceGraph resourceGraph, IRequestQueryStringAccessor queryStringAccessor) + : base(resourceGraph) + { + _queryStringAccessor = queryStringAccessor; + } + + public override SparseFieldSetExpression? OnApplySparseFieldSet(SparseFieldSetExpression? existingSparseFieldSet) + { + if (!_queryStringAccessor.Query.ContainsKey("fields[books]")) + { + return existingSparseFieldSet + .Excluding(book => book.Refs1, ResourceGraph) + .Excluding(book => book.Refs2, ResourceGraph) + .Excluding(book => book.Refs3, ResourceGraph); + } + + return existingSparseFieldSet; + } +} diff --git a/src/Examples/GettingStarted/Models/Book.cs b/src/Examples/GettingStarted/Models/Book.cs index 49310bd..4eabb6e 100644 --- a/src/Examples/GettingStarted/Models/Book.cs +++ b/src/Examples/GettingStarted/Models/Book.cs @@ -16,4 +16,13 @@ public sealed class Book : HexStringMongoIdentifiable [Attr] public int PublishYear { get; set; } + + [Attr] + public List? Refs1 { get; set; } + + [Attr] + public List? Refs2 { get; set; } + + [Attr] + public List? Refs3 { get; set; } } diff --git a/src/Examples/GettingStarted/Program.cs b/src/Examples/GettingStarted/Program.cs index 7deafcc..8eb64fd 100644 --- a/src/Examples/GettingStarted/Program.cs +++ b/src/Examples/GettingStarted/Program.cs @@ -1,3 +1,4 @@ +using GettingStarted.Definitions; using GettingStarted.Models; using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.MongoDb.Configuration; @@ -19,6 +20,7 @@ builder.Services.AddJsonApiMongoDb(); builder.Services.AddResourceRepository>(); +builder.Services.AddResourceDefinition(); WebApplication app = builder.Build(); diff --git a/src/Examples/GettingStarted/Properties/launchSettings.json b/src/Examples/GettingStarted/Properties/launchSettings.json index b82968b..1322373 100644 --- a/src/Examples/GettingStarted/Properties/launchSettings.json +++ b/src/Examples/GettingStarted/Properties/launchSettings.json @@ -11,7 +11,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "api/books", + "launchUrl": "api/books?fields[books]=title,refs1,refs2", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -19,7 +19,7 @@ "Kestrel": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "api/books", + "launchUrl": "api/books?fields[books]=title,refs1,refs2", "applicationUrl": "http://localhost:24141", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" From bfcd6f536c31d211b7d878c9eb0a79ca53e53ffa Mon Sep 17 00:00:00 2001 From: Bart Koelman <10324372+bkoelman@users.noreply.github.com> Date: Sat, 4 May 2024 12:00:16 +0200 Subject: [PATCH 2/2] Backport to v4.2.0 --- .../GettingStarted/Controllers/BooksController.cs | 14 ++++++++++++++ src/Examples/GettingStarted/GettingStarted.csproj | 4 ++-- src/Examples/GettingStarted/Models/Book.cs | 3 +-- src/Examples/GettingStarted/Program.cs | 3 ++- 4 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 src/Examples/GettingStarted/Controllers/BooksController.cs diff --git a/src/Examples/GettingStarted/Controllers/BooksController.cs b/src/Examples/GettingStarted/Controllers/BooksController.cs new file mode 100644 index 0000000..b72a644 --- /dev/null +++ b/src/Examples/GettingStarted/Controllers/BooksController.cs @@ -0,0 +1,14 @@ +using GettingStarted.Models; +using JsonApiDotNetCore.Configuration; +using JsonApiDotNetCore.Controllers; +using JsonApiDotNetCore.Services; + +namespace GettingStarted.Controllers; + +public sealed class BooksController : JsonApiController +{ + public BooksController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService resourceService) + : base(options, loggerFactory, resourceService) + { + } +} diff --git a/src/Examples/GettingStarted/GettingStarted.csproj b/src/Examples/GettingStarted/GettingStarted.csproj index 0f6b40f..eb2877b 100644 --- a/src/Examples/GettingStarted/GettingStarted.csproj +++ b/src/Examples/GettingStarted/GettingStarted.csproj @@ -1,11 +1,11 @@ - net8.0;net6.0 + net6.0 - + diff --git a/src/Examples/GettingStarted/Models/Book.cs b/src/Examples/GettingStarted/Models/Book.cs index 4eabb6e..e84c9ad 100644 --- a/src/Examples/GettingStarted/Models/Book.cs +++ b/src/Examples/GettingStarted/Models/Book.cs @@ -5,8 +5,7 @@ namespace GettingStarted.Models; [UsedImplicitly(ImplicitUseTargetFlags.Members)] -[Resource] -public sealed class Book : HexStringMongoIdentifiable +public sealed class Book : MongoIdentifiable { [Attr] public string Title { get; set; } = null!; diff --git a/src/Examples/GettingStarted/Program.cs b/src/Examples/GettingStarted/Program.cs index 8eb64fd..ce59d17 100644 --- a/src/Examples/GettingStarted/Program.cs +++ b/src/Examples/GettingStarted/Program.cs @@ -5,6 +5,7 @@ using JsonApiDotNetCore.MongoDb.Repositories; using Microsoft.Extensions.DependencyInjection.Extensions; using MongoDB.Driver; +using Newtonsoft.Json; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); @@ -40,7 +41,7 @@ static void ConfigureJsonApiOptions(JsonApiOptions options) options.Namespace = "api"; options.UseRelativeLinks = true; options.IncludeTotalResourceCount = true; - options.SerializerOptions.WriteIndented = true; + options.SerializerSettings.Formatting = Formatting.Indented; } static async Task CreateSampleDataAsync(IMongoDatabase database)