Skip to content

Commit

Permalink
fix: Encode id and className properties for nested non-ParseObjects (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 committed Jul 13, 2024
1 parent 173e7d8 commit f13ad6b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
# Parse-Swift Changelog

### main
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.10.2...main), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/main/documentation/parseswift)
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.10.3...main), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/main/documentation/parseswift)
* _Contributing to this repo? Add info about your change here to be included in the next release_

### 5.10.3
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.10.2...5.10.3), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/5.10.3/documentation/parseswift)

__Fixes__
* Allow encoding of id and className on nested types that are not ParseObjects ([#176](https://github.com/netreconlab/Parse-Swift/pull/177)), thanks to [Corey Baker](https://github.com/cbaker6).

### 5.10.2
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.10.1...5.10.2), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/5.10.2/documentation/parseswift)

Expand Down
19 changes: 11 additions & 8 deletions Sources/ParseSwift/Coding/ParseEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,17 @@ public struct ParseEncoder: Sendable {
case custom(Set<String>)

func keys() -> Set<String> {
let defaultObjectKeys = Set(["createdAt",
"updatedAt",
"objectId",
"className",
"emailVerified",
"id",
"score",
"originalData"])
let defaultObjectKeys = Set(
[
"objectId",
"createdAt",
"updatedAt",
"emailVerified",
"score",
"originalData"
]
)

switch self {

case .object:
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/ParseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

enum ParseConstants {
static let sdk = "swift"
static let version = "5.10.2"
static let version = "5.10.3"
static let fileManagementDirectory = "parse/"
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
static let fileManagementLibraryDirectory = "Library/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import XCTest
@testable import ParseSwift

class ParseEncoderTests: XCTestCase {

struct Dummy: Codable, Hashable {
var id: String
}

struct GameScore: ParseObject, ParseQueryScorable {
// These are required by ParseObject
var objectId: String?
Expand All @@ -24,6 +29,7 @@ class ParseEncoderTests: XCTestCase {

// Your own properties
var points: Int
var dummy: Dummy?

// a custom initializer
init() {
Expand Down
29 changes: 29 additions & 0 deletions Tests/ParseSwiftTests/ParseObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import XCTest
@testable import ParseSwift

class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length

struct Dummy: Codable, Hashable {
var id: String
}

struct Level: ParseObject {
var objectId: String?

Expand Down Expand Up @@ -43,6 +48,7 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
var level: Level?
var levels: [Level]?
var nextLevel: Level?
var dummy: Dummy?

//: custom initializers
init() {}
Expand Down Expand Up @@ -348,6 +354,29 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
XCTAssertTrue(score1.shouldRestoreKey(\.nextLevel, original: score2))
}

func testParseEncoderAllowsIdOnNestedTypesOnParseObject() throws {
var score = GameScore(points: 5)
score.dummy = Dummy(id: "hello")

let object = try ParseCoding
.parseEncoder()
.encode(
score,
acl: nil,
collectChildren: true,
objectsSavedBeforeThisOne: nil,
filesSavedBeforeThisOne: nil
)
let decoded = String(
decoding: object.encoded,
as: UTF8.self
)
XCTAssertEqual(
decoded,
#"{"dummy":{"id":"hello"},"player":"Jen","points":5}"#
)
}

func testParseObjectMutable() throws {
var score = GameScore(points: 19, name: "fire")
XCTAssertEqual(score, score.mergeable)
Expand Down

0 comments on commit f13ad6b

Please sign in to comment.