-
-
Notifications
You must be signed in to change notification settings - Fork 158
Make ResourceGraphBuilder extensible #1099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @Khaos66, thanks for asking. I agree that
The way to solve this is by wrapping the type from the 3rd party library. I've attached a sample project that wraps WrappedThirdPartyResourceDemoApi.zip For convenience, I'm adding the wrapper here as text too: [Resource("versions")]
public sealed class VersionResource : Identifiable<string>
{
private Version _innerVersion = new();
public override string Id
{
get => _innerVersion.ToString();
set => _innerVersion = value == null ? new Version() : Version.Parse(value);
}
[Attr]
public int Major
{
get => _innerVersion.Major;
set => _innerVersion = new Version(value, _innerVersion.Minor);
}
[Attr]
public int Minor
{
get => _innerVersion.Minor;
set => _innerVersion = new Version(_innerVersion.Major, value);
}
} when running the project, it requests: GET http://localhost:24883/releases?include=version Which returns: {
"links": {
"self": "http://localhost:24883/releases?include=version",
"first": "http://localhost:24883/releases?include=version"
},
"data": [
{
"type": "releases",
"id": "1",
"attributes": {
"title": "JsonApiDotNetCore"
},
"relationships": {
"version": {
"links": {
"self": "http://localhost:24883/releases/1/relationships/version",
"related": "http://localhost:24883/releases/1/version"
},
"data": {
"type": "versions",
"id": "1.2"
}
}
},
"links": {
"self": "http://localhost:24883/releases/1"
}
}
],
"included": [
{
"type": "versions",
"id": "1.2",
"attributes": {
"major": 1,
"minor": 2
},
"links": {
"self": "http://localhost:24883/versions/1.2"
}
}
]
} Hope that helps. |
@bart-degreed Thank you for this comprehensive answer! In my oppinion something like this would be preverable: public class VersionDefinition : JsonApiResourceDefinition<Version, string>
{
public override void OnBuildAttributes(IResourceGraphBuilder builder)
{
// Attr will create a new instance of AttrAttribute
builder.Attr(nameof(Version.Major), typeof(Version).GetProperty("Major"));
builder.Attr(nameof(Version.Minor), typeof(Version).GetProperty("Minor"));
}
} Is this clear enough? EDIT: |
That's simply not possible, because each resource must implement |
@bart-degreed I'm sorry. I was too lazy and didn't explain it well enough... Of course it is not possible without a class that implements Here is my actual use case: using JsonApiDotNetCore.Resources;
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations.Schema;
[Resource("users")]
public sealed class UserResource : IdentityUser, IIdentifiable<string>
{
[NotMapped]
public string LocalId { get; set; }
[NotMapped]
public string StringId { get => Id; set => Id = value; }
[Attr]
[ProtectedPersonalData]
public string GivenName { get; set; }
[Attr]
[ProtectedPersonalData]
public string FamilyName { get; set; }
}
But sometimes this is not the case. And in this case I think it would be nice to be able the define additional Properties via public class UserDefinition : JsonApiResourceDefinition<UserResource, string>
{
public override void OnBuildGraph(IResourceGraphBuilder builder)
{
// Add missing properties from IdentityUser to UserResource graph
builder.Attr(nameof(IdentityUser.UserName), typeof(IdentityUser).GetProperty("UserName"));
builder.Attr(nameof(IdentityUser.Email), typeof(IdentityUser).GetProperty("Email"));
}
} In fact, you're doing this already in |
Thanks for the clarification, makes sense. We'd like to solve this using a fluent API in the future. Adding that to If I understand correctly, you're currently unblocked. Therefore I'd prefer to not open up |
Ok. I don't have the insights that you have ;) I get your point. In that case you already have this on your roadmap and I'm happy ;) |
Glad to help :) |
Is your feature request related to a problem? Please describe.
I need to make a resource type accessable through JADNC that is part of a third party lib. So I can't add AttrAttribute to the properties of this type.
Describe the solution you'd like
It would be nice if it was possible to extend the
ResourceGraphBuilder
. Maybe throughJsonApiResourceDefinition
?Sadly, the methods
CreateResourceType
andGetAttributes
are private and sealed off.Or did I miss another way to do this?
The text was updated successfully, but these errors were encountered: