Skip to content

Conversation

MajorTanya
Copy link
Member

@MajorTanya MajorTanya commented Jan 30, 2025

kotlinx.serialization does NOT serialize default values (like createdAt in BGMOAuth.kt), so every time the Bangumi tracker deserialized the tracker OAuth, createdAt was set to the time of the read, not the time of issuance.

Separately, BangumiInterceptor did correctly fetch new OAuth credentials upon detected expiry of the stored credentials and saved them, but did not use them for the current request (the new credentials were used for all subsequent requests only). This led to 401 errors from Bangumi because the expired access_token was provided.
A subsequent request using the newly acquired access_token would end
up being successful.

Some version or another of this bug has been around since before #1103 so for once it's not me breaking things. 😅

This MIGHT close #988 and fix #564.

kotlinx.serialization does NOT serialize default values (like
createdAt in BGMOAuth.kt), so every time the Bangumi tracker
deserialized the tracker OAuth, createdAt was set to the time of the
read, not the time of issuance.

Separately, BangumiInterceptor did correctly fetch new OAuth
credentials upon detected expiry of the stored credentials and saved
them, but did not use them for the current request (the new
credentials were used for all subsequent requests only). This led to
401 errors from Bangumi because the expired access_token was provided.
 A subsequent request using the newly acquired access_token would end
 up being successful.
@AntsyLich
Copy link
Member

The doc is talking about serialization and we're doing deserialization the annotation has no effect here.

@MajorTanya
Copy link
Member Author

The doc is talking about serialization and we're doing deserialization the annotation has no effect here.

It very much has. When we first request the data from Bangumi, we get a new instance of BGMOAuth, with the current timestamp added as createdAt. We proceed to serialise it in the trackPreferences (in the saveToken method). Here, we do not get createdAt stored.

Once the app restarts or the Bangumi tracker initialises itself anew for any other reason, we pull the BGMOAuth Json from the trackPreferences, without createdAt. So, we get the default value provided in the instance. But this will be the current time, not token creation time.

With the annotation, createdAt gets stored when the BGMOAuth object gets serialised to the trackPreferences and therefore will be available on next restart/re-init/etc.

@AntsyLich
Copy link
Member

Hmm that's interesting. Not how I expected it to work.

@MajorTanya
Copy link
Member Author

I only figured this out because I had the same "Huh?" reaction with kotlinx.serialization on a different project before. When I checked the default assignment and looked into the preferences this gets serialised to, I suddenly rem and checked the docs again.

@AntsyLich
Copy link
Member

I was under the impression it would recheck the defaults but I suppose it's just easier to check if the property had custom value or not

@MajorTanya
Copy link
Member Author

It wouldn't know what the original timestamp value was, so it assigns the default again (the result of a assignment-time function call). This would be perfectly workable with constants but alas.

@AntsyLich AntsyLich merged commit dce6aac into mihonapp:main Jan 30, 2025
1 check passed
@MajorTanya MajorTanya deleted the fix-bangumi-401 branch January 30, 2025 15:39
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hope that bangumi’s login API can be updated The Bangumi Tracker should handle 401 response
2 participants