-
Notifications
You must be signed in to change notification settings - Fork 731
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
Fatal error: Optional is only JSONEncodable if Wrapped #1536
Comments
Can I see the code you're using to make |
Hi, Since 0.30.0 we stopped using a custom function to map our JSON to a JSONDecadable/JSONEncodable, as for this changelog note.
It has been working fine as it is since 0.30.0 to 0.36.0. It has broken since we upgraded to 0.37.0 |
Hmmm. I'll have to look into that, that's odd. |
@ruizmarc Can you share the full dictionary you're getting back so I can use it to do some testing please? |
Here you have the payload of the request: https://pastebin.com/eTbhsheb Thanks for checking it! |
So I pulled the payload in and the JSON was failing to parse until I found all the func testProvidedTranslations() throws {
typealias CustomJSON = [String: Any?]
class GetTranslationsQuery: GraphQLQuery {
var operationDefinition: String {
"""
query GetTranslations {
translations
}
"""
}
var operationName: String = "GetTanslations"
struct Data: GraphQLSelectionSet {
static var selections: [GraphQLSelection] = [
GraphQLField("translations", type: .nonNull(.scalar(CustomJSON.self))),
]
var resultMap: ResultMap
init(unsafeResultMap: ResultMap) {
resultMap = unsafeResultMap
}
var translations: CustomJSON {
get {
return resultMap["translations"]! as! CustomJSON
}
set {
resultMap.updateValue(newValue, forKey: "translations")
}
}
}
}
let string = """
[ raw string from pastebin ]
"""
let jsonData = try XCTUnwrap(string.data(using: .utf8))
let parsedJSON = try JSONSerializationFormat.deserialize(data: jsonData)
let typedJSON = try XCTUnwrap(parsedJSON as? JSONObject)
let response = GraphQLResponse(operation: SomeQuery(), body: typedJSON)
let result = try response.parseResultFast()
XCTAssertNotNil(result.data)
XCTAssertEqual(result.data?.translations["INITIAL_CHARGE_LEVEL"] as? String, "Initial charge level")
} I'm betting what's happening is the underlying error is getting swallowed by the parser somewhere. Would you mind trying to update the escape sequences for quotation marks on your end to see if that might be the issue? |
Hi! Thanks for your investigation, unfortunately, it didn't worked taking out the quotes. However, looking at your example, I changed the code from:
to the following one, and it worked even without removing the quotes:
So at this moment the app is working again with 0.37.0 with just changing this. I don't know if it is because it was not intended that we use jsonObject or it is really an issue. I leave up to you to close the issue or not depending on this. In any case, thanks for your attention. |
What about using If you can share the generated code for your query I can get you something more accurate. |
Hi, It also works with your suggestion:
What I was using is this: https://developer.apple.com/documentation/foundation/jsonserialization/1415493-jsonobject But it is clear it is not necessary to use it anymore. We have been using apollo-ios since it very beginning and at some point in the past, that was the only way we could get it working, but with the pass of the time it has gotten much simpler to work with custom types like JSON, using jsonObject it's like legacy code, I don't think I should expect it to work. Here you have the generated code for the query with version 0.37.0:
|
OK - I'm still not totally clear on where In any case, it seems like we've gotten to the bottom of this - do you mind if we close this out? |
|
On my side, we can close the issue. Thanks for your time and kind attention. |
Bug report
Everything was working fine in 0.36 but after updating to 0.37 this error message appears:
Fatal error: Optional is only JSONEncodable if Wrapped is: file Apollo/JSONStandardTypeConversions.swift, line 109
Versions
Please fill in the versions you're currently using:
apollo-ios
SDK version: 0.37Steps to reproduce
Schema:
Typealias for JSON
Query:
Response Handling:
Response format
It crashes when executing jsonObject. With previous versions, everything was working fine.
The text was updated successfully, but these errors were encountered: