Skip to content

Commit

Permalink
Bug fix: issue when encoding objects that are already saved (#48)
Browse files Browse the repository at this point in the history
* Bug fix: issue when encoding objects that are already saved

* Add test cases to detect if ParseObject body encoding breaks

* Add dates to ensure skipKeys doesn't break

* codecov patch set back to auto
  • Loading branch information
cbaker6 authored Jan 10, 2021
1 parent 9575409 commit ca096b5
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ coverage:
status:
patch:
default:
target: 69
target: auto
changes: false
project:
default:
Expand Down
4 changes: 3 additions & 1 deletion Sources/ParseSwift/Coding/ParseEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ private class _ParseEncoder: JSONEncoder, Encoder {
throw ParseError(code: .unknownError, message: "Found a circular dependency when encoding.")
}
self.uniqueObjects.insert(object)
if !self.collectChildren {
if !self.collectChildren && codingPath.count > 0 {
valueToEncode = value
} else if !self.collectChildren {
valueToEncode = value.toPointer()
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ extension ParseLiveQuery {
}
}

// MARK: Query - Subscribe
// MARK: ParseLiveQuery - Subscribe
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public extension Query {
/**
Expand Down Expand Up @@ -728,7 +728,7 @@ public extension Query {
}
}

// MARK: Query - Unsubscribe
// MARK: ParseLiveQuery - Unsubscribe
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public extension Query {
/**
Expand Down Expand Up @@ -768,7 +768,7 @@ public extension Query {
}
}

// MARK: Query - Update
// MARK: ParseLiveQuery - Update
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public extension Query {
/**
Expand Down
62 changes: 62 additions & 0 deletions Tests/ParseSwiftTests/ParseObjectBatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ class ParseObjectBatchTests: XCTestCase { // swiftlint:disable:this type_body_le
try? ParseStorage.shared.deleteAll()
}

func testSaveAllCommand() throws {
let score = GameScore(score: 10)
let score2 = GameScore(score: 20)

var scoreOnServer = score
scoreOnServer.objectId = "yarr"
scoreOnServer.createdAt = Date()
scoreOnServer.updatedAt = scoreOnServer.createdAt
scoreOnServer.ACL = nil

var scoreOnServer2 = score2
scoreOnServer2.objectId = "yolo"
scoreOnServer2.createdAt = Date()
scoreOnServer2.updatedAt = scoreOnServer2.createdAt
scoreOnServer2.ACL = nil

let objects = [score, score2]
let commands = objects.map { $0.saveCommand() }
let body = BatchCommand(requests: commands)
// swiftlint:disable:next line_length
let expected = "{\"requests\":[{\"path\":\"\\/classes\\/GameScore\",\"method\":\"POST\",\"body\":{\"score\":10}},{\"path\":\"\\/classes\\/GameScore\",\"method\":\"POST\",\"body\":{\"score\":20}}]}"
let encoded = try ParseCoding.parseEncoder()
.encode(body, collectChildren: false,
objectsSavedBeforeThisOne: nil,
filesSavedBeforeThisOne: nil).encoded
let decoded = String(data: encoded, encoding: .utf8)
XCTAssertEqual(decoded, expected)
}

func testSaveAll() { // swiftlint:disable:this function_body_length cyclomatic_complexity
let score = GameScore(score: 10)
let score2 = GameScore(score: 20)
Expand Down Expand Up @@ -235,6 +264,39 @@ class ParseObjectBatchTests: XCTestCase { // swiftlint:disable:this type_body_le
}
}

func testUpdateAllCommand() throws {
var score = GameScore(score: 10)
var score2 = GameScore(score: 20)

score.objectId = "yarr"
score.createdAt = Date()
score.updatedAt = Date()
score2.objectId = "yolo"
score2.createdAt = Date()
score2.updatedAt = Date()

let objects = [score, score2]
let initialCommands = objects.map { $0.saveCommand() }
let commands = initialCommands.compactMap { (command) -> API.Command<GameScore, GameScore>? in
let path = ParseConfiguration.mountPath + command.path.urlComponent
guard let body = command.body else {
return nil
}
return API.Command<GameScore, GameScore>(method: command.method, path: .any(path),
body: body, mapper: command.mapper)
}
let body = BatchCommand(requests: commands)
// swiftlint:disable:next line_length
let expected = "{\"requests\":[{\"path\":\"\\/1\\/classes\\/GameScore\\/yarr\",\"method\":\"PUT\",\"body\":{\"score\":10}},{\"path\":\"\\/1\\/classes\\/GameScore\\/yolo\",\"method\":\"PUT\",\"body\":{\"score\":20}}]}"

let encoded = try ParseCoding.parseEncoder()
.encode(body, collectChildren: false,
objectsSavedBeforeThisOne: nil,
filesSavedBeforeThisOne: nil).encoded
let decoded = String(data: encoded, encoding: .utf8)
XCTAssertEqual(decoded, expected)
}

func testUpdateAll() { // swiftlint:disable:this function_body_length cyclomatic_complexity
var score = GameScore(score: 10)
score.objectId = "yarr"
Expand Down
34 changes: 30 additions & 4 deletions Tests/ParseSwiftTests/ParseObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
self.fetchAsync(score: score, scoreOnServer: scoreOnServer, callbackQueue: .main)
}

func testSaveCommand() {
func testSaveCommand() throws {
let score = GameScore(score: 10)
let className = score.className

Expand All @@ -470,23 +470,49 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
XCTAssertEqual(command.path.urlComponent, "/classes/\(className)")
XCTAssertEqual(command.method, API.Method.POST)
XCTAssertNil(command.params)
XCTAssertNotNil(command.body)
XCTAssertNotNil(command.data)

guard let body = command.body else {
XCTFail("Should be able to unwrap")
return
}

let expected = "{\"score\":10,\"player\":\"Jen\"}"
let encoded = try ParseCoding.parseEncoder()
.encode(body, collectChildren: false,
objectsSavedBeforeThisOne: nil,
filesSavedBeforeThisOne: nil).encoded
let decoded = String(data: encoded, encoding: .utf8)
XCTAssertEqual(decoded, expected)
}

func testUpdateCommand() {
func testUpdateCommand() throws {
var score = GameScore(score: 10)
let className = score.className
let objectId = "yarr"
score.objectId = objectId
score.createdAt = Date()
score.updatedAt = Date()

let command = score.saveCommand()
XCTAssertNotNil(command)
XCTAssertEqual(command.path.urlComponent, "/classes/\(className)/\(objectId)")
XCTAssertEqual(command.method, API.Method.PUT)
XCTAssertNil(command.params)
XCTAssertNotNil(command.body)
XCTAssertNotNil(command.data)

guard let body = command.body else {
XCTFail("Should be able to unwrap")
return
}

let expected = "{\"score\":10,\"player\":\"Jen\"}"
let encoded = try ParseCoding.parseEncoder()
.encode(body, collectChildren: false,
objectsSavedBeforeThisOne: nil,
filesSavedBeforeThisOne: nil).encoded
let decoded = String(data: encoded, encoding: .utf8)
XCTAssertEqual(decoded, expected)
}

func testSave() { // swiftlint:disable:this function_body_length
Expand Down

0 comments on commit ca096b5

Please sign in to comment.