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

fields named "id" are skipped in nested objects #140

Open
3 tasks done
wtholliday opened this issue Dec 28, 2023 · 6 comments
Open
3 tasks done

fields named "id" are skipped in nested objects #140

wtholliday opened this issue Dec 28, 2023 · 6 comments
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@wtholliday
Copy link

wtholliday commented Dec 28, 2023

New Issue Checklist

Issue Description

Fields named "id" are skipped in nested objects.

Steps to reproduce

    struct MyType: Hashable, Codable {
        // BUG: if we call this "id" then parse won't save it
        var id: String
    }

    struct MyParseObject: ParseObject {
        public var originalData: Data?
        public var objectId: String?
        public var createdAt: Date?
        public var updatedAt: Date?
        public var ACL: ParseSwift.ParseACL?

        var nested: MyType?
        public init() {}
    }

    func test_parseEncodeDictionary() throws {
        var myObject = MyParseObject()
        myObject.nested = MyType(id: "test1")

        let object = try ParseCoding.parseEncoder()
            .encode(myObject,
                    acl: nil,
                    collectChildren: true,
                    objectsSavedBeforeThisOne: nil,
                    filesSavedBeforeThisOne: nil)
        XCTExpectFailure("currently skipping id fields")
        XCTAssertEqual(String(decoding: object.encoded, as: UTF8.self), #"{"nested":{"id":"test1"}}"#)
    }

Actual Outcome

Fields named "id" are ignored and don't make it to the server.

Expected Outcome

Fields named "id" (not in ParseObjects) should still be saved or an error should be reported so the user knows to rename the field.

Environment

Client

  • Parse Swift SDK version: 5.8.2
  • Xcode version: 15.1 (15C65)
  • Operating system (iOS, macOS, watchOS, etc.): macOS
  • Operating system version: 14.2.1 (23C71)

Server

  • Parse Server version: 4.5.0
  • Operating system: Ubuntu
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): BackForApp

Database

  • System (MongoDB or Postgres): MongoDB
  • Database version: 3.6
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): BackForApp
@cbaker6 cbaker6 added the question Further information is requested label Dec 30, 2023
@cbaker6
Copy link
Member

cbaker6 commented Dec 30, 2023

id is an internal keyword for all Parse classes and should not be used to name a property or else you will get unexpected behavior. This is not just for ParseSwift, but all of the SDKs. You can use a name like localId, key names you are not suppose to use are:

let defaultObjectKeys = Set(["createdAt",
"updatedAt",
"objectId",
"className",
"emailVerified",
"id",
"score",
"originalData"])

@wtholliday
Copy link
Author

Why skip fields inside a struct which isn't a ParseObject? Seems like it could store them without any issues, since they are just represented as json strings on the server, right?

@cbaker6
Copy link
Member

cbaker6 commented Dec 31, 2023

If you think you have a better solution, feel free to open a PR with your suggestion.

@cbaker6 cbaker6 reopened this Jan 9, 2024
@cbaker6 cbaker6 added enhancement New feature or request bug Something isn't working and removed question Further information is requested enhancement New feature or request labels Jan 9, 2024
@cbaker6
Copy link
Member

cbaker6 commented Jan 9, 2024

Reopening this, will see if I can provide a fix by the end of the week

@roaring-b
Copy link

Thanks, Corey! Discovering this the hard way was a huge pain.

@cbaker6
Copy link
Member

cbaker6 commented Jan 9, 2024

I looked further into this and attempting to provide a fix now will cause more harm than good. The ParseEncoder is tricky as there are a number of properties that shouldn't be sent to the server.

As a workaround, you can do any of the following:

  1. Not use property names that contain any of the keys being skipped by the SDK, fields named "id" are skipped in nested objects #140 (comment)
  2. Encode your respective object that contains the keys being skipped to JSON string before storing it in your ParseObject (the string will be stored as-is and won't skip keys). You can then decode your JSON string whenever you fetch your object from the server. You could then add another property with a getter and setter that decodes the string in the getter and encodes the string on the setter similar to

@cbaker6 cbaker6 added the wontfix This will not be worked on label Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants