Skip to content

fix: decoder and encoder default instances #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

grdsdev
Copy link
Collaborator

@grdsdev grdsdev commented Apr 23, 2025

What kind of change does this PR introduce?

Close #702

Introduce a supabase default JSONEncoder and JSONDecoder for usage across all sub-packages, this makes JSON decoding configuration consistent across the codebase.

Since now all sub-packages uses the same Encoder/Decoder, it fixes an issue that decoding a type in Postgrest works, but not in Realtime change, as reported in #702

@grdsdev grdsdev requested a review from Copilot April 23, 2025 12:08
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the default JSONDecoder and JSONEncoder instances to use a centralized supabase configuration and unifies date formatting across the codebase. Key changes include:

  • Replacing custom JSON decoding/encoding strategies in various modules with JSONDecoder.supabase() and JSONEncoder.supabase().
  • Refactoring date formatting logic in SupabaseLogger and DateFormatter to use the new iso8601String property.
  • Updating related tests and configurations (e.g., DotEnv) to ensure consistency.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Tests/IntegrationTests/DotEnv.swift Added a DotEnv enum with environment configuration keys.
Sources/Storage/Codable.swift Updated defaultStorageDecoder to use the supabase configuration.
Sources/PostgREST/Defaults.swift Refactored jsonDecoder and jsonEncoder to use supabase implementations.
Sources/Helpers/SupabaseLogger.swift Revised date formatting by using the iso8601String property.
Sources/Helpers/DateFormatter.swift Reworked date formatter extensions with new DateFormatter helpers.
Sources/Helpers/Codable.swift Switched to supabase() functions for JSONDecoder and JSONEncoder.
Sources/Helpers/AnyJSON/AnyJSON+Codable.swift Updated AnyJSON encoder/decoder to use supabase implementations.
Sources/Auth/Defaults.swift Adjusted AuthClient default encoders/decoders to utilize supabase().

@grdsdev grdsdev changed the title fix: JSONDecoder and JSONEncoder default instances fix: decoder and encoder default instances Apr 23, 2025
@grdsdev grdsdev requested a review from Copilot April 23, 2025 16:52
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the default encoder and decoder instances and updates the session storage migration logic. Key changes include:

  • Adopting JSONDecoder.supabase() and JSONEncoder.supabase() as default implementations.
  • Replacing XCTAssertEqual assertions with expectNoDifference in tests.
  • Adding a new storage migration to use the default encoder for sessions.

Reviewed Changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Tests/IntegrationTests/DotEnv.swift Adds DotEnv constants for integration testing.
Tests/AuthTests/SessionManagerTests.swift Updates session refresh assertions using expectNoDifference.
Tests/AuthTests/AuthClientTests.swift Replaces multiple XCTAssertEqual calls with expectNoDifference.
Sources/Storage/Codable.swift Uses JSONDecoder.supabase() for the default storage decoder.
Sources/PostgREST/Defaults.swift Switches JSONDecoder/Encoder to use supabase() implementations.
Sources/Helpers/SupabaseLogger.swift Uses Date.iso8601String for log message formatting.
Sources/Helpers/DateFormatter.swift Refactors date formatting to use DateFormatter-based helpers.
Sources/Helpers/Codable.swift Refactors JSONDecoder/Encoder to expose supabase() functions.
Sources/Helpers/AnyJSON/AnyJSON+Codable.swift Updates AnyJSON to use the supabase defaults.
Sources/Auth/Internal/SessionStorage.swift Updates session storage migration and decoding/encoding methods.
Sources/Auth/Defaults.swift Uses the supabase default encoder/decoder for AuthClient configuration.
Files not reviewed (1)
  • Tests/AuthTests/Resources/local-storage.json: Language not supported

}
}

do {
let storedData = try storage.retrieve(key: key)
return try storedData.flatMap {
try AuthClient.Configuration.jsonDecoder.decode(Session.self, from: $0)
try JSONDecoder().decode(Session.self, from: $0)
Copy link
Preview

Copilot AI Apr 23, 2025

Choose a reason for hiding this comment

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

Consider using JSONDecoder.supabase() here instead of instantiating a new JSONDecoder(), to ensure consistent decoding behavior across the codebase.

Copilot uses AI. Check for mistakes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since this is the decoder used for decoding the stored session, it is safer to use a new decoder instance, unrelated to the other ones used in codebase, since any change to decoder configuration may break the session decoding.

@@ -64,7 +67,7 @@ extension SessionStorage {
do {
try storage.store(
key: key,
value: AuthClient.Configuration.jsonEncoder.encode(session)
value: JSONEncoder().encode(session)
Copy link
Preview

Copilot AI Apr 23, 2025

Choose a reason for hiding this comment

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

Consider using JSONEncoder.supabase() here instead of a new JSONEncoder(), to maintain consistent encoding behavior in session storage.

Suggested change
value: JSONEncoder().encode(session)
value: JSONEncoder.supabase().encode(session)

Copilot uses AI. Check for mistakes.

Copy link
Collaborator Author

@grdsdev grdsdev Apr 25, 2025

Choose a reason for hiding this comment

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

Same as the comment related to JSONDecoder.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 14623634293

Details

  • 71 of 106 (66.98%) changed or added relevant lines in 8 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.04%) to 74.831%

Changes Missing Coverage Covered Lines Changed/Added Lines %
Sources/Helpers/SupabaseLogger.swift 0 1 0.0%
Sources/Auth/Internal/SessionStorage.swift 23 30 76.67%
Sources/Helpers/DateFormatter.swift 24 51 47.06%
Files with Coverage Reduction New Missed Lines %
Sources/Helpers/DateFormatter.swift 2 45.28%
Totals Coverage Status
Change from base Build 14590365536: -0.04%
Covered Lines: 5096
Relevant Lines: 6810

💛 - Coveralls

@grdsdev grdsdev marked this pull request as ready for review April 25, 2025 18:16
@grdsdev grdsdev requested a review from dshukertjr April 25, 2025 18:16
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.

Supabase Swift does not encode / decode fractional seconds on Date types
2 participants