Comments (7)
This can be done using a resource definition. See json-api-dotnet/JsonApiDotNetCore.MongoDb#54 for an example. The second commit backports to the version you're using.
from jsonapidotnetcore.
There's no looping in the code. You can ensure fields are always returned using the .Including
method, which works similarly.
More importantly, JSON:API is loved by clients because it allows them to specify exactly what data to fetch and return. A server that is sending fields the client never asked for negates that optimized network bandwidth experience (between client and server, as well as between server and database). A better approach would be to break down your data structure into smaller parts, so you won't need to include/exclude so many fields.
from jsonapidotnetcore.
Iām newbie.
I have a lot of fields to exclude/include. Is there a better way to implement this than looping one by one?
public override SparseFieldSetExpression? OnApplySparseFieldSet(SparseFieldSetExpression? existingSparseFieldSet)
{
if (!_queryStringAccessor.Query.ContainsKey("fields[books]"))
{
return existingSparseFieldSet
.Excluding<Book>(book => book.Refs1, ResourceGraph)
.Excluding<Book>(book => book.Refs2, ResourceGraph)
.Excluding<Book>(book => book.Refs3, ResourceGraph);
}
return existingSparseFieldSet;
}
from jsonapidotnetcore.
I got it. Thanks :)
from jsonapidotnetcore.
I created the following method to
- return all DefaultAttributes (primitive & string)
- return all DefaultAttributes + specific fields by fields[]
The first case works well. But the 2nd returns only the specific fields, even though I add more attributes into existingSparseFieldSet. Do you know why?
public override SparseFieldSetExpression? OnApplySparseFieldSet(SparseFieldSetExpression? existingSparseFieldSet)
{
//var resourceContext = ResourceGraph.GetResourceContext<TResource>();
// set default attribute first
var attributes = _resourceGraph.GetResourceContext<TResource>().Attributes;
var fields = "fields[" + _resourceGraph.GetResourceContext<TResource>().PublicName + "]";
var defaultAttributes = (IReadOnlyCollection<ResourceFieldAttribute>)
attributes.Where(a => PageConfigurableDefinition<TResource>.IsDefaultAttribute(a)).ToList();
if (_queryStringAccessor.Query.ContainsKey(fields) && existingSparseFieldSet != null)
{
HashSet<ResourceFieldAttribute> fieldSet = existingSparseFieldSet.Fields.ToHashSet();
foreach (var attr in defaultAttributes)
fieldSet.Add(attr);
return new SparseFieldSetExpression(fieldSet);
}
else
{
return new SparseFieldSetExpression(defaultAttributes);
}
}
from jsonapidotnetcore.
This does not work
public override SparseFieldSetExpression? OnApplySparseFieldSet(SparseFieldSetExpression? existingSparseFieldSet)
{
return existingSparseFieldSet
.Including<Book>(t => t.bookId, ResourceGraph)
.Including<Book>(t => t.bookGroup, ResourceGraph)
.Including<Book>(t => t.bookGroupName, ResourceGraph);
}
from jsonapidotnetcore.
You're right, using .Including()
doesn't add extra fields to the JSON response. I assumed it would, but that's not the case. It is actually by design and documented at https://www.jsonapi.net/api/JsonApiDotNetCore.Resources.IResourceDefinition-2.html#JsonApiDotNetCore_Resources_IResourceDefinition_2_OnApplySparseFieldSet_JsonApiDotNetCore_Queries_Expressions_SparseFieldSetExpression__remarks:
Including extra fields from this method will retrieve them, but not include them in the json output. This enables you to expose calculated properties whose value depends on a field that is not in the sparse fieldset.
So in that case, what you want is not possible. You'll just need to send the desired fields in the query string.
from jsonapidotnetcore.
Related Issues (20)
- Request with query string '=' returns 500 HOT 3
- cleanupcode.ps1 <revision> fails on Linux HOT 1
- Empty string ID is accepted in post resource request HOT 1
- Clarify usage of explicit controllers
- Add tests for submitting meta
- Look into usage of [BindRequired] HOT 1
- Source generation when models are in a separate project HOT 1
- PrimaryResourceType is null and giving error "Expected IJsonApiRequest.PrimaryResourceType not to be null at this point." HOT 6
- Include relations inside custom controller HOT 7
- Attempting to setup sample app with filtering and getting error on filtered call. HOT 3
- Differently encoded URLs in self and first links HOT 1
- DateTime in filter expression gets parsed as local DateTime when using "Z" designator HOT 3
- Use IResourceService outside of the path of a request HOT 10
- Multiple db context and keeping single resource HOT 10
- Multiple DbContexts seems to conflict each other at runtime HOT 2
- Customize paging for each controller ? HOT 10
- Support JSONB columns HOT 4
- TopLevelLinks All / Paging does not work as expected HOT 3
- Use form fields in issue template HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapidotnetcore.