Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Crash on encoding decoded value from nested structuture #80

Open
DominatorVbN opened this issue Nov 15, 2023 · 3 comments
Open

Crash on encoding decoded value from nested structuture #80

DominatorVbN opened this issue Nov 15, 2023 · 3 comments

Comments

@DominatorVbN
Copy link

{
    "aps": {
        "alert": {
            "title": "Test Title",
            "subtitle": "Test Subtitle",
            "body": "Test body content"
        },
    "badge": 1,
    "sound": "default",
    "category": "CUSTOM_UNNotificationCategory",
    "interruption-level": "active",
    "relevance-score": 0.0
    }
}

Decoded this successfully using

JSONDecoder().decode(AnyCodable.self, from: payloadData)

Causing error while

JSONEncoder().encode(_payloadEncodable)

Stacktrace:

* thread #20, queue = 'com.apple.root.default-qos.cooperative', stop reason = EXC_BREAKPOINT (code=1, subcode=0x18a773698)
    frame #0: 0x000000018a773698 Foundation`Foundation.__JSONEncoder.encode<τ_0_0 where τ_0_0: Swift.Encodable>(τ_0_0) throws -> () + 292
    frame #1: 0x000000018a773ae8 Foundation`protocol witness for Swift.SingleValueEncodingContainer.encode<τ_0_0 where τ_1_0: Swift.Encodable>(τ_1_0) throws -> () in conformance Foundation.__JSONEncoder : Swift.SingleValueEncodingContainer in Foundation + 24
    frame #2: 0x0000000198f26588 libswiftCore.dylib`dispatch thunk of Swift.SingleValueEncodingContainer.encode<τ_0_0 where τ_1_0: Swift.Encodable>(τ_1_0) throws -> () + 32
  * frame #3: 0x000000010594d3e4 NotifyHub`AnyCodable.encode(encoder=Swift.Encoder @ 0x000000016b261e58, self=Codability.AnyCodable @ 0x000000016b261ae0) at AnyCodable.swift:87:27
    frame #4: 0x000000010594de30 NotifyHub`protocol witness for Encodable.encode(to:) in conformance AnyCodable at <compiler-generated>:0
    frame #5: 0x0000000198f2489c libswiftCore.dylib`dispatch thunk of Swift.Encodable.encode(to: Swift.Encoder) throws -> () + 32
    frame #6: 0x0000000104eaed18
@DominatorVbN
Copy link
Author

Closing this,
Seems like the issue arise only when the encodable object is passed to swiftapns, which uses https://github.com/swift-server-community/APNSwift/blob/915f4d0e5c4441388a7d95a072bd0b6cfdccb7f4/Sources/APNS/Coding/APNSJSONEncoder.swift#L21

Might be something to do with buffer.writeData(data).

@DominatorVbN
Copy link
Author

The implementation of
https://github.com/googleapis/google-api-swift-client/blob/a7a7b3c0ea6a55f51f306e3d29df86cb9fe1ff83/Sources/GoogleAPIRuntime/JSONAny.swift#L98

Works for the SwiftNIO based JSONEncoder, might be good idea to incorporate this.

@DominatorVbN DominatorVbN reopened this Nov 16, 2023
@wangzhizhou
Copy link

This problem has be fixed by APNSwift, now on the main branch and not be released:

swift-server-community/APNSwift#200

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants