Skip to content

fix: don't apply Str::singular() to relation type name references - #126

Open
lorenzodalaqua wants to merge 2 commits into
fumeapp:masterfrom
lorenzodalaqua:fix/relation-type-singular
Open

fix: don't apply Str::singular() to relation type name references#126
lorenzodalaqua wants to merge 2 commits into
fumeapp:masterfrom
lorenzodalaqua:fix/relation-type-singular

Conversation

@lorenzodalaqua

Copy link
Copy Markdown
Contributor

Problem

When generating TypeScript types for relations, WriteRelationship runs the related model's class name through Str::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 interface is emitted with the exact class name:
export interface OpeningHours { ... }

// But the relation reference gets singularized, pointing at a type that doesn't exist:
export interface Store {
  // ...
  opening_hours: OpeningHour[]  // TS2304: Cannot find name 'OpeningHour'
}

The generated output doesn't compile.

Fix

Remove the Str::singular() calls in WriteRelationship and use the class name as-is, so relation references always match the emitted interface names. This affects both arms of the match:

  • Plural relations (HasMany, BelongsToMany, ...) when plurals is disabled: OpeningHours[] instead of OpeningHour[]
  • Singular relations (BelongsTo, HasOne, ...): OpeningHours instead of OpeningHour

The Str::plural() call for the plurals = true option 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 references
  • test/Tests/Feature/Actions/WriteRelationshipTest.php: test covering a plural-looking class name in both singular and plural relation arms

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.
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

Successfully merging this pull request may close these issues.

1 participant