Skip to content
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

fix(datastore): configure json encoder with sorted key #3418

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ extension Model where Self: Codable {
/// application making any change to these `public` types should be backward compatible, otherwise it will be a
/// breaking change.
public func toJSON(encoder: JSONEncoder? = nil) throws -> String {
let resolvedEncoder: JSONEncoder
var resolvedEncoder: JSONEncoder
if let encoder = encoder {
resolvedEncoder = encoder
} else {
resolvedEncoder = JSONEncoder(dateEncodingStrategy: ModelDateFormatting.encodingStrategy)
}

if isKnownUniquelyReferenced(&resolvedEncoder) {
resolvedEncoder.outputFormatting = .sortedKeys
}

let data = try resolvedEncoder.encode(self)
guard let json = String(data: data, encoding: .utf8) else {
throw DataStoreError.decodingError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ extension ModelValueConverter {
}

static var jsonEncoder: JSONEncoder {
JSONEncoder(dateEncodingStrategy: ModelDateFormatting.encodingStrategy)
let encoder = JSONEncoder(dateEncodingStrategy: ModelDateFormatting.encodingStrategy)
encoder.outputFormatting = .sortedKeys
return encoder
}

/// - Warning: Although this has `public` access, it is intended for internal & codegen use and should not be used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SQLModelValueConverterTests: BaseDataStoreTests {
/// - bool must be `Int` (1 or 0)
/// - the remaining types should not change
func testModelWithEveryTypeConversionToBindings() {
let nonModelJSON = "{\"someString\":\"string\",\"someEnum\":\"foo\"}"
let nonModelJSON = "{\"someEnum\":\"foo\",\"someString\":\"string\"}"
let example = exampleModel

// convert model to SQLite Bindings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ProcessMutationErrorFromCloudOperationTests: XCTestCase {
var localPost = Post(title: "localTitle", content: "localContent", createdAt: .now())
let queue = OperationQueue()
let reconciliationQueue = MockReconciliationQueue()

override func setUp() {
tryOrFail {
try setUpWithAPI()
Expand Down
4 changes: 2 additions & 2 deletions AmplifyTests/CoreTests/Model+CodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import AmplifyTestCommon

class ModelCodableTests: XCTestCase {
let postJSONWithFractionalSeconds = """
{"id":"post-1","title":"title","content":"content","comments":[],"createdAt":"1970-01-12T13:46:40.123Z"}
{"comments":[],"content":"content","createdAt":"1970-01-12T13:46:40.123Z","id":"post-1","title":"title"}
"""

let postJSONWithoutFractionalSeconds = """
{"id":"post-1","title":"title","content":"content","comments":[],"createdAt":"1970-01-12T13:46:40Z"}
{"comments":[],"content":"content","createdAt":"1970-01-12T13:46:40Z","id":"post-1","title":"title"}
"""

override func setUp() {
Expand Down
Loading