Skip to content

Refactor JWT::JWK::Set#initialize - #758

Merged
anakinj merged 1 commit into
jwt:mainfrom
anakinj:refactor/jwk-set-initializer
Sep 5, 2026
Merged

Refactor JWT::JWK::Set#initialize#758
anakinj merged 1 commit into
jwt:mainfrom
anakinj:refactor/jwk-set-initializer

Conversation

@anakinj

@anakinj anakinj commented Sep 5, 2026

Copy link
Copy Markdown
Member

Description

Pure refactor of JWT::JWK::Set#initialize, no behaviour change. Groundwork for #744.

The two branches that build keys from hashes had drifted apart: the JWKS document form (Hash) skips a key whose kty is unsupported, per RFC 7517 §5, while the array form raises. Both were each_with_object/map blocks nested in the middle of a case, which makes the difference between them easy to miss — and that is how the gap #744 reports came about.

Each branch now calls a named method, so the difference is stated rather than buried:

when Hash  then build_supported_keys(jwks.transform_keys(&:to_sym)[:keys], options)
when Array then build_keys(jwks, options)

Side effects of the extraction:

  • initialize fits the metrics limits, so the # rubocop:disable Metrics/CyclomaticComplexity it has carried is gone.
  • jwks = jwks.transform_keys(&:to_sym) no longer reassigns the subject of the case from inside one of its own branches; jwks ||= {} becomes an explicit when nil then [].

On #744

#744 currently fails RuboCop, because copying the rescue block into the array branch pushes initialize to Metrics/MethodLength [22/18] and Metrics/PerceivedComplexity [9/8]. On top of this refactor that PR becomes a one-line change — point the Array branch at build_supported_keys and delete build_keys — with its spec unchanged.

Verification

  • Full suite green, RuboCop clean on the file with no exemptions.
  • Behaviour compared against main across 22 input shapes (nil / no args / empty hash, symbol and string keys, unsupported kty in first, last and only position, keys: nil, keys: given a bare hash, a bare JWK hash, arrays of hashes and of JWK objects, a single JWK object, construction from a Set, copy independence, bad types, options pass-through, merge). Output is identical, including the array-with-unsupported-kty cases still raising JWT::UnsupportedKeyType.

Checklist

Before the PR can be merged be sure the following are checked:

  • There are tests for the fix or feature added/changed
  • A description of the changes and a reference to the PR has been added to CHANGELOG.md. More details in the CONTRIBUTING.md

@anakinj
anakinj force-pushed the refactor/jwk-set-initializer branch from e15cba0 to 250f3e0 Compare September 5, 2026 18:45
The two construction paths that build keys from hashes had drifted: the
JWKS document form skips a key whose kty is unsupported, the array form
raises. That is easy to miss when the two are nested blocks in the middle
of a case expression, and it is how the gap in jwt#744 came about.

Give each branch a named method, so what differs between them is visible
in the branch itself. initialize now fits the metrics limits without the
Metrics/CyclomaticComplexity exemption it has carried until now.

No behaviour change.
@anakinj
anakinj force-pushed the refactor/jwk-set-initializer branch from 250f3e0 to f7219a8 Compare September 5, 2026 18:51
@anakinj
anakinj merged commit 7cede0c into jwt:main Sep 5, 2026
15 checks passed
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