Skip to content
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

Improve how relationship URI segments are automatically calculated (e.g. to replace dash case with underscore) #220

Open
MeiKatz opened this issue Jan 7, 2023 · 4 comments

Comments

@MeiKatz
Copy link

MeiKatz commented Jan 7, 2023

Hej,

I tried to change the uri type of my schemas from being dasherized to use camel case. So far no big deal. But I noticed that this change is not reflected in the urls of related entities. Actually, this is what I expect to see when I change the #uriType() in a schema.

I will further investigate this topic in the next days. This issue is only to keep a light on it. Maybe somebody else has noticed this: you're not alone with it!

As far as I have searched the url is build within LaravelJsonApi\Core\Resources\Relation#relatedUrl(). But I guess there has to be a change in LaravelJsonApi\Eloquent\Fields\Relations\Relation.

@lindyhopchris
Copy link
Contributor

You can set the uri name on a relationship, as described here:
https://laraveljsonapi.io/docs/2.0/schemas/relationships.html#uri-name

I.e.:

HasMany::make('moduleNumbers')->withUriFieldName('module_numbers')

@lindyhopchris
Copy link
Contributor

FYI the reason for setting it on the relationship is because the URL part relates to the relationship name, not the resource type returned by that relationship. I.e. I could have a relationship called author that returns a users resource.

@MeiKatz
Copy link
Author

MeiKatz commented Jan 8, 2023

Yes, I see your point, but I think it's still a wrong or unexpected behaviour: using a "person" scheme for an editor or creator is one thing. But using a different naming way as the default value is something totally different. I don't want to add a method to all my relations, that could be derived from the name itself. Also it makes code a lot more erroneous.

So: please re-open this issue and I will try to create a pull request for this topic.

@lindyhopchris
Copy link
Contributor

So to be clear this isn't a bug - it's perfectly possible to set whatever URI segments you want, either for the resource type, or for a relationship endpoint.

Could how that is implemented be improved? Possibly. However, the thing to bear in mind is that to add some sort of calculation maybe reduces the dev effort of having to define it on relationships, but the "cost" of calculating it will be incurred on every single request. So there's a balancing act between saving the developer time and introducing repetitive calculations.

My best guess would be somewhere for both the resource type URI segment, and the relationship URI segment, there's a call to dash-case the segment. If that's the case, then it could perhaps be overriden somehow so you can replace the dash-casing with underscores if wanted.

@lindyhopchris lindyhopchris reopened this Jan 8, 2023
@lindyhopchris lindyhopchris changed the title [Bug] #uriType() is not used for related urls Improve how relationship URI segments are automatically calculated (e.g. to replace dash case with underscore) Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants