Skip to content

fix generated types for self-referential attributes#28

Merged
ElMassimo merged 2 commits intoElMassimo:mainfrom
adriand:feature/self-referential-attributes
Jan 2, 2026
Merged

fix generated types for self-referential attributes#28
ElMassimo merged 2 commits intoElMassimo:mainfrom
adriand:feature/self-referential-attributes

Conversation

@adriand
Copy link
Contributor

@adriand adriand commented Oct 6, 2025

Description 📖

Fixes TypeScript compilation errors when serializers have self-referencing associations (e.g., comments with comment replies, folders containing folders).

Previously, self-referencing serializers generated imports that caused TS2440 and TS2652 errors:

import type Comment from './Comment'  // conflicts with interface declaration

export default interface Comment {
  replies: Comment[]
}

Now generates valid TypeScript without the self-import:

export default interface Comment {
  replies: Comment[]
}

Background 📜

TypeScript interfaces can reference themselves without imports, but the generator was adding import statements for all association serializers without checking if they referenced the current serializer.

This pattern is common in hierarchical data structures:

  • Comment threads (comments have child comments)
  • File systems (folders contain folders)
  • Organization charts (managers manage managers)
  • Menu structures (menu items contain sub-menus)

The Fix 🔨

Added a filter in Interface#used_imports (generator.rb) to skip imports when an association serializer references itself:

serializer_type_imports = association_serializers.map(&:ts_interface)
  .reject { |type| type.name == name } # avoid self-imports (e.g. for recursive types)
  .map { |type| [type.name, relative_path(type.pathname, pathname)] }

Note: this is probably not the clearest-written comment...my words failed me a little with this issue.

The fix compares TypeScript interface names to detect self-references. Properties still include the self-referencing type, but no import is generated.

Test coverage includes both standard and namespace modes with a CommentSerializer that has has_many :replies, serializer: CommentSerializer.

@adriand
Copy link
Contributor Author

adriand commented Oct 6, 2025

@ElMassimo I don't think the failing checks are due to my code, seems to be an issue with the pipeline.

@ElMassimo ElMassimo merged commit abd5073 into ElMassimo:main Jan 2, 2026
1 of 6 checks passed
@ElMassimo
Copy link
Owner

Thanks Adrian!

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.

2 participants