Skip to content

Decode structs for schema modules that are not yet loaded - #8

Open
col wants to merge 1 commit into
aj-foster:mainfrom
col:fix-typed-decoder-unloaded-modules
Open

Decode structs for schema modules that are not yet loaded#8
col wants to merge 1 commit into
aj-foster:mainfrom
col:fix-typed-decoder-unloaded-modules

Conversation

@col

@col col commented Aug 17, 2026

Copy link
Copy Markdown

Problem

GitHub.Plugin.TypedDecoder decides 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

base = if function_exported?(module, :__struct__, 0), do: struct(module), else: %{}

function_exported?/3 returns false for 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 the else branch and comes back as a bare map with atom keys. The module.__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:

loaded before 1st decode: false
1st decode: %{token: "ghs_x", expires_at: "...", repository_selection: "all"}
2nd decode: %GitHub.Installation.Token{token: "ghs_x", ...}

Any caller pattern-matching the documented return type crashes on that first call. In our case:

** (CaseClauseError) no case clause matching:
    {:ok, %{token: "ghs_...", expires_at: "...", repository_selection: "all", permissions: %{...}}}

from a straightforward match on create_installation_access_token/3's specced {:ok, GitHub.Installation.Token.t()}:

case GitHub.Apps.create_installation_access_token(installation_id, %{}, auth: jwt) do
  {:ok, %GitHub.Installation.Token{token: token, expires_at: expires_at}} -> ...

It is easy to miss because it only shows up in development:

  • Releases boot in embedded mode with every module preloaded, so the check always passes.
  • Test suites that use GitHub.Plugin.TestClient never reach TypedDecoder at all.

Nested types are affected the same way — the first GitHub.Repository inside a list_repos_accessible_to_installation/1 response 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:

base =
  if Code.ensure_loaded?(module) and function_exported?(module, :__struct__, 0),
    do: struct(module),
    else: %{}

This preserves the existing fallback for modules that define __fields__/1 without a struct, and costs nothing for loaded modules — :code.ensure_loaded/1 short-circuits on erlang:module_loaded/1.

Tests

Adds test/github/plugin/typed_decoder_test.exs covering all three paths: module already loaded, module not yet loaded (the regression — unloads it with :code.purge/1 + :code.delete/1 first), and a module with __fields__/1 but no struct.

The middle test fails on main with exactly the reported symptom and passes with the change:

1) test decode/2 decodes a struct when the schema module has not been loaded yet
   left:  %GitHub.Installation.Token{token: "ghs_abc"}
   right: %{token: "ghs_abc"}

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_fun 1.1.6 no longer compiles). I bumped it to 1.1.7 locally to run the tests and reverted mix.lock before committing, so this PR touches only the two files.

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