fix: don't apply Str::singular() to relation type name references - #126
Open
lorenzodalaqua wants to merge 2 commits into
Open
fix: don't apply Str::singular() to relation type name references#126lorenzodalaqua wants to merge 2 commits into
lorenzodalaqua wants to merge 2 commits into
Conversation
The inflector was applied to PHP class names before using them as TypeScript type references in both singular and plural relations. This was breaking plural model names like OpeningHours, producing a type reference that never matched the emitted interface name, which always uses the exact class name. Removing Str::singular() makes the reference consistent with what is actually emitted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When generating TypeScript types for relations,
WriteRelationshipruns the related model's class name throughStr::singular()before using it as a type reference. But the interface for that model is always emitted under its exact class name, with no inflection applied.For most model names the two happen to match, so the bug is invisible. It only breaks when a class name looks plural to the inflector. Given a model named
OpeningHours:The generated output doesn't compile.
Fix
Remove the
Str::singular()calls inWriteRelationshipand use the class name as-is, so relation references always match the emitted interface names. This affects both arms of the match:HasMany,BelongsToMany, ...) whenpluralsis disabled:OpeningHours[]instead ofOpeningHour[]BelongsTo,HasOne, ...):OpeningHoursinstead ofOpeningHourThe
Str::plural()call for theplurals = trueoption is untouched. That behavior is opt-in and documented. This change only fixes the default path, where the reference should be the literal class name.Changes
src/Actions/WriteRelationship.php: use the exact related class name in type referencestest/Tests/Feature/Actions/WriteRelationshipTest.php: test covering a plural-looking class name in both singular and plural relation arms