Decode structs for schema modules that are not yet loaded - #8
Open
col wants to merge 1 commit into
Open
Conversation
`function_exported?/3` returns false for a module that has not been loaded, so under interactive code loading the first response decoded for any given schema fell through to a bare map. The `module.__fields__/1` call on the following line then loads the module, so every decode after the first one returns the struct — callers matching on the struct see an intermittent failure that clears itself on retry. Guard the check with `Code.ensure_loaded?/1`, which loads the module if needed and preserves the existing fallback for modules that define `__fields__/1` without a struct. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P4MKHHE3EuHYGL3sgbYsZS
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
GitHub.Plugin.TypedDecoderdecides between building a struct and building a bare map here:https://github.com/aj-foster/open-api-github/blob/main/lib/github/plugin/typed_decoder.ex#L79
function_exported?/3returnsfalsefor a module that has not been loaded yet. Under interactive code loading (mix phx.server,iex -S mix,mix run), the first response decoded for any given schema therefore takes theelsebranch and comes back as a bare map with atom keys. Themodule.__fields__(type)call on the very next line is what loads the module, so every decode after the first returns the struct as expected.The result is an intermittent failure that clears itself on retry. Reproduced on a stock project:
Any caller pattern-matching the documented return type crashes on that first call. In our case:
from a straightforward match on
create_installation_access_token/3's specced{:ok, GitHub.Installation.Token.t()}:It is easy to miss because it only shows up in development:
GitHub.Plugin.TestClientnever reachTypedDecoderat all.Nested types are affected the same way — the first
GitHub.Repositoryinside alist_repos_accessible_to_installation/1response decodes as a map even once the outer type is loaded.Fix
Guard the existing check with
Code.ensure_loaded?/1, which loads the module if it isn't already:This preserves the existing fallback for modules that define
__fields__/1without a struct, and costs nothing for loaded modules —:code.ensure_loaded/1short-circuits onerlang:module_loaded/1.Tests
Adds
test/github/plugin/typed_decoder_test.exscovering all three paths: module already loaded, module not yet loaded (the regression — unloads it with:code.purge/1+:code.delete/1first), and a module with__fields__/1but no struct.The middle test fails on
mainwith exactly the reported symptom and passes with the change:Full suite passes: 14 doctests, 25 tests, 0 failures. No changes to generated code.
Note: I could not run the suite on the CI's OTP 25 locally (OTP 28 here, where the locked
ssl_verify_fun1.1.6 no longer compiles). I bumped it to 1.1.7 locally to run the tests and revertedmix.lockbefore committing, so this PR touches only the two files.