Skip to content

Fix: Don't raise an error if expiration_time_millis is nil; fall back to 0 - #552

Open
sampart wants to merge 1 commit into
googleapis:mainfrom
sampart:expires-at-handle-nil
Open

sampart wants to merge 1 commit into
googleapis:mainfrom
sampart:expires-at-handle-nil

Conversation

@sampart

@sampart sampart commented Dec 3, 2025

Copy link
Copy Markdown
Contributor

The change

data.fetch("expiration_time_millis", 0) will default to 0 only if the key is not present. If the key is present but the associated value is nil, then prior to this change the code would raise as follows:

NoMethodError (undefined method `/' for nil:NilClass

          expires_at:    data.fetch("expiration_time_millis", 0) / 1000
                                                                 ^):

This commit updates the code to default to 0 if the key is not found or if the value is nil.

With the existing token stores, this situation may not arise, but it's a particular issue when using a custom token store where values could be nil, like a database.

Testing

Since the existing tests don't cover the existing fallback, I've not added to them here either.

I've tested this change manually locally by modifying my copy of the gem in a test project.

Changelog

https://github.com/googleapis/google-auth-library-ruby/blob/main/.github/CONTRIBUTING.md mentions updating the CHANGELOG. However, CHANGELOG updates now appear to be an automated process so I've left this alone.

@sampart
sampart requested a review from a team December 3, 2025 12:49
@sampart sampart changed the title Don't raise an error if expiration_time_millis is nil; fall back to 0 Fix: Don't raise an error if expiration_time_millis is nil; fall back to 0 Dec 3, 2025
@sampart
sampart force-pushed the expires-at-handle-nil branch from 3d80798 to 3f1709d Compare December 3, 2025 14:07
@amanda-tarafa amanda-tarafa added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 9, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 9, 2026
@amanda-tarafa

Copy link
Copy Markdown

The change seems fine but tests are failing, possibly because the branch is so old. Can you rebase on main and push again?

…ck to 0

`data.fetch("expiration_time_millis", 0)` will default to 0 only if the key is not present. If the key is present but the associated value is nil, then prior to this change the code would raise as follows:

```
NoMethodError (undefined method `/' for nil:NilClass

          expires_at:    data.fetch("expiration_time_millis", 0) / 1000
                                                                 ^):

```

This commit updates the code to default to 0 if the key is not found or if the value is nil.

With the existing token stores, this situation may not arise, but it's a particular issue when using a custom token store where values could be nil, like a database.
@sampart
sampart force-pushed the expires-at-handle-nil branch from 3f1709d to 145b832 Compare October 13, 2026 13:20
@sampart
sampart requested a review from a team as a code owner October 13, 2026 13:20
@sampart

sampart commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @amanda-tarafa. Done 👍

@quartzmo quartzmo added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Oct 13, 2026
@quartzmo

Copy link
Copy Markdown
Member

Adding do not merge while I review, will remove it once I approve.

access_token: data["access_token"],
refresh_token: data["refresh_token"],
expires_at: data.fetch("expiration_time_millis", 0) / 1000
expires_at: (data.fetch("expiration_time_millis") || 0) / 1000

@robertvoinescu-work robertvoinescu-work Oct 13, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using fetch without a default will raise a KeyError if the key missing, could we instead do:

expires_at:    (data["expiration_time_millis"] || 0) / 1000

Furthermore, could you also add a couple of unit tests covering both the nil and missing key cases in spec/googleauth/user_authorizer_spec.rb? That way we can avoid regressions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I was concerned about as well.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, agreed, I didn't "see" that the default was being removed, just saw the coalescing bit.

@quartzmo quartzmo removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Oct 13, 2026
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.

5 participants