Correct import paths for string-form refs to other generated serializers#35
Open
pixelblend wants to merge 1 commit intoElMassimo:mainfrom
Open
Correct import paths for string-form refs to other generated serializers#35pixelblend wants to merge 1 commit intoElMassimo:mainfrom
pixelblend wants to merge 1 commit intoElMassimo:mainfrom
Conversation
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.
Description 📖
This pull request fixes broken TypeScript import paths in generated files when a serializer uses a string-form type annotation (
type "Foo",type "Foo[]",type "Record<string, Foo>") that names another generated serializer.Background 📜
This was happening because
Interface#used_importsresolves imports via two branches.has_many :x, serializer: YusesY's known output pathname. Buttype "Y"in any form always joinsYontoconfig.relative_custom_types_dir(which defaults tooutput_dir.parent, i.e.types/), with no awareness thatYmay itself be a generated serializer.The resulting import lands one directory too high. At the top level this often goes unnoticed because a hand-written
types/Y.d.tscan happen to exist. In a nested output file it creates an unresolvable path.The correct path is one parent up, landing on the sibling generated file:
The Fix 🔨
Changed
Interface#used_importsto first look up each extracted type name against the set of currently-loaded serializer interfaces: if it matches, use that serializer's output pathname (same resolution as thehas_manybranch). Otherwise fall back tocustom_types_diras before.Added a new serializer and test to cover this case.
Please note, this changes the precedence rule for name collisions. Previously a string-form type reference was unconditionally routed through
custom_types_dir, so a hand-writtentypes/X.d.tswon over a generatedtypes/serializers/X.tswhen both existed. After this PR, generated serializers take precedence.Screenshots 📷
Updated for the new serializer example.