Skip to content

Commit

Permalink
fix(datastore): configure json encoder with sorted key
Browse files Browse the repository at this point in the history
  • Loading branch information
5d committed Dec 12, 2023
1 parent a766796 commit 4e197d1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
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

0 comments on commit 4e197d1

Please sign in to comment.