This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
Handle generating field for schema that isn't defined yet #35
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.
Closes #5
Adding a new parameter to
AbstractConverter
is probably a breaking change :/ -- but can use the upgrade to Marshmallow 3 as an excuse to also bump major version. 👀Anyways, idea here is that when we are generating a schema if we encounter a field that we don't know about yet, we can just create a kind of thunk field (appropriately named
ThunkedField
) that delays creating the actual field until it's requested, and then caches that field.The
ThunkedField
will proxy all operations to the underlying field, creating it if necessary. This is potentially dangerous if there's a marshmallow plugin that'll poke at the created field BEFORE the other schema is created.This first pass implementation actually improperly handles lists, optionals, etc by just generating thunks for them all since it happens pretty early in
_get_field_from_typehint
, but as a first pass works pretty well. Fixing this might require additionally updatingTypeRegistry.get
to accept an additional argument or adding aget_or_thunk
method (which is probably better) in which case theThunkedField
can carry less information, just what theFieldFactory
delegate declaresRisks (setting aside the new parameter thing):
The following would now generate a schema but fail at first serialization attempt -- rather than eagerly fail:
Possible solution: Push a
__THUNKED__: bool
into the field settings and make the end user explicitly declare that a field is to be thunked:I like the explicit opt-in better, even if it's slightly cumbersome, since it prevents unexpected surprises down the line (leaving only expected surprises). And means that only
convert
needs to change sinceconvert_all
would receive the opt-in via the field configurations.