elliottminns / blackfire

A minimal, fast and unopinionated web framework for Swift
Apache License 2.0
904 stars 39 forks source link

JSON parsing bug #62

Closed iDevPro closed 7 years ago

iDevPro commented 7 years ago

Swift: Apple Swift version 3.0.2 (swift-3.0.2-RELEASE) Target: x86_64-apple-macosx10.9

Xcode: Xcode 8.2.1 (8C1002)

OS: macOS 10.12.2

Function: public func send(json: Any) { self.headers["Content-Type"] = "application/json" do { let data = try JSONSerialization.data(withJSONObject: json, options: []) self.body = Buffer(data: data) } catch { self.status = 500 } send() }

P.s: JSONSerialization.isValidJSONObject(json) => false

Result: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write' First throw call stack: ( 0 CoreFoundation 0x00007fffc73c1e7b exceptionPreprocess + 171 1 libobjc.A.dylib 0x00007fffdbfaccad objc_exception_throw + 48 2 CoreFoundation 0x00007fffc744099d +[NSException raise:format:] + 205 3 Foundation 0x00007fffc8d5dd5c +[NSJSONSerialization dataWithJSONObject:options:error:] + 249 4 Blackfire 0x000000010068ebbe _TFC9Blackfire12HTTPResponse4sendfT4jsonP_T + 702 5 OwnServer 0x00000001000024c0 _TF9OwnServerU2_FTC9Blackfire7RequestCS0_12HTTPResponseT + 224 6 Blackfire 0x00000001006934d3 _TTRXFo_oC9Blackfire7RequestoCS_12HTTPResponseXFo_iTS0_S1_iT__ + 35 7 Blackfire 0x0000000100693571 _TPATTRXFo_oC9Blackfire7RequestoCS_12HTTPResponseXFo_iTS0_S1_iT + 81 8 Blackfire 0x0000000100697507 _TTRXFo_iTC9Blackfire7RequestCS_12HTTPResponseiTXFo_oS0_oS1___ + 39 9 Blackfire 0x000000010069726a _TFFV9Blackfire14RequestHandler6handleFT7requestCS_11HTTPRequest8responseCS_12HTTPResponse_T_U_FFTCS_7RequestS2TT + 90 10 Blackfire 0x00000001006973be _TTRXFo_oXFo_oC9Blackfire7RequestoCS_12HTTPResponse_zoPs5ErrorXFo_iXFo_iTS0S1iT_zoPS2_ + 126 11 libswiftCore.dylib 0x0000000100775dd5 _TFEsPs8Sequence7forEachfzFzWx8Iterator7Element_TT + 389 12 Blackfire 0x0000000100697084 _TFV9Blackfire14RequestHandler6handlefT7requestCS_11HTTPRequest8responseCS_12HTTPResponseT + 1076 13 Blackfire 0x00000001006839e1 _TFC9Blackfire5Flame6serverfTCS_10HTTPServer10didReceiveCS_11HTTPRequest8responseCS_12HTTPResponseT + 161 14 Blackfire 0x0000000100683a53 _TTWC9Blackfire5FlameS_18HTTPServerDelegateS_FS1_6serverfTCS_10HTTPServer10didReceiveCS_11HTTPRequest8responseCS_12HTTPResponseT + 67 15 Blackfire 0x0000000100690f80 _TFFC9Blackfire10HTTPServer6serverFTCS_6Server19didCreateConnectionPS_10ConnectionT_U_FTCS_6BufferSiT + 800 16 Blackfire 0x00000001006856d5 _TFFC9Blackfire16SocketConnection4readFT8callbackFTCS_6BufferSi_TT_U_FTT + 453 17 Blackfire 0x00000001006857f7 TTRXFoXFdCb___ + 39 18 libdispatch.dylib 0x00000001006e5f5c _dispatch_client_callout + 8 19 libdispatch.dylib 0x00000001006fcb15 _dispatch_continuation_pop + 1025 20 libdispatch.dylib 0x00000001006f2658 _dispatch_source_latch_and_call + 195 21 libdispatch.dylib 0x00000001006e8da5 _dispatch_source_invoke + 1106 22 libdispatch.dylib 0x00000001006feca7 _dispatch_root_queue_drain_deferred_item + 704 23 libdispatch.dylib 0x00000001007038f0 _dispatch_kevent_worker_thread + 983 24 libsystem_pthread.dylib 0x000000010075d773 _pthread_wqthread + 1004 25 libsystem_pthread.dylib 0x000000010075d375 start_wqthread + 13 ) libc++abi.dylib: terminating with uncaught exception of type NSException

iDevPro commented 7 years ago

Hmm, but so strange: In playground: JSONSerialization.isValidJSONObject(["a","b","c"]) -> True JSONSerialization.isValidJSONObject(["a":"b"]) -> True JSONSerialization.isValidJSONObject("a") -> False

In project debug console: po JSONSerialization.isValidJSONObject(["a","b","c"]) -> False po JSONSerialization.isValidJSONObject(["a":"b"]) -> False po JSONSerialization.isValidJSONObject("a") -> False

WAT?!

elliottminns commented 7 years ago

Hmmm this is odd. What is the exact JSON you're sending in?

iDevPro commented 7 years ago

I try example :)

elliottminns commented 7 years ago

So, for me:

import Blackfire

let app = Flame(type: .concurrent)

app.get("/") { req, res in
  res.send(json: ["a", "b", "c'"])
}

app.start(port: 3001) { result in
  switch result {
    case .success:
      print("App listening on port 3001")
    case .failure:
      print("App failed to launch")
  }
}

Works with producing the below:

λ curl localhost:3001
["a","b","c'"]%

But you're correct, it does seem to throw an error for incorrect JSON objects. I'll double check the throwing logic and let you know.

elliottminns commented 7 years ago

Release 0.1.1 fixes this issue temporarily.

iDevPro commented 7 years ago

Try your code in 0.1.2 and: 2017-01-31 14 45 32

in console:

objc[90797]: Class _TtCs18_EmptyArrayStorage is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a07c30) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c32e0). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs24_ContiguousArrayStorage1 is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a07cd8) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3388). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs17_CocoaSetIterator is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a1b1d8) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002d6998). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs24_CocoaDictionaryIterator is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a1bf28) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002d76e8). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs17NonObjectiveCBase is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a1d9c0) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002d9180). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs17_stdlib_AtomicInt is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a1f4c8) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002dac88). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs19_SwiftNativeNSArray is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a07d78) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3428). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs24_SwiftNativeNSDictionary is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a07de0) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3490). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs17_SwiftNativeNSSet is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a07e48) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c34f8). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs24_SwiftNativeNSEnumerator is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a07eb0) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3560). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs18_SwiftNativeNSData is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a07f18) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c35c8). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs26_SwiftNativeNSCharacterSet is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a07f80) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3630). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs31_stdlib_ReturnAutoreleasedDummy is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a07fe8) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3698). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs20_SwiftNativeNSString is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08060) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3710). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs19_NSContiguousString is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a080c8) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3778). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs40_SwiftNativeNSArrayWithContiguousStorage is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08148) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c37f8). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs21_SwiftDeferredNSArray is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a081b8) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3868). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs27_ContiguousArrayStorageBase is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08248) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c38f8). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs14_VaListBuilder is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a24970) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002e0130). One of the two will be used. Which one is undefined.
objc[90797]: Class _TtCs13VaListBuilder is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a24a60) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002e0220). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftNativeNSError is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a082d0) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3980). One of the two will be used. Which one is undefined.
objc[90797]: Class SwiftObject is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08320) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c39d0). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftValue is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08398) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3a48). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftNull is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a083e8) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3a98). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftNativeNSArrayBase is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08438) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3ae8). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftNativeNSDictionaryBase is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08488) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3b38). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftNativeNSSetBase is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a084d8) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3b88). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftNativeNSStringBase is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08528) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3bd8). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftNativeNSEnumeratorBase is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08578) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3c28). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftNativeNSDataBase is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a085c8) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3c78). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftNativeNSCharacterSetBase is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08618) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3cc8). One of the two will be used. Which one is undefined.
objc[90797]: Class _SwiftNativeNSIndexSetBase is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftCore.dylib (0x100a08668) and /Users/dev/Library/Developer/Xcode/DerivedData/swiftWeb-fkoiangfmmbwxbgkyniiempewzxw/Build/Products/Debug/swiftWeb (0x1002c3d18). One of the two will be used. Which one is undefined.
App listening on port 3001

But has not result yet :)