Skip to content

[pigeon] The generated Swift object crashes during parsing. #123387

@jackyshan

Description

@jackyshan

Use case

My Pigeon template definition is as follows:

class RequestParam {
  late String key;
  Map<String?, Object?>? data;
}
class ResponseParam {
  late ResponseCode code;
  String? message;
  Map<String?, Object?>? data;
}

Here's an example of a generated Swift code block:

/// Generated class from Pigeon that represents data sent in messages.
struct RequestParam {
  var key: String
  var data: [String?: Any?]? = nil

  static func fromList(_ list: [Any]) -> RequestParam? {
    let key = list[0] as! String
    let data = list[1] as! [String?: Any?]? 

    return RequestParam(
      key: key,
      data: data
    )
  }
  func toList() -> [Any?] {
    return [
      key,
      data,
    ]
  }
}

/// Generated class from Pigeon that represents data sent in messages.
struct ResponseParam {
  var code: ResponseCode
  var message: String? = nil
  var data: [String?: Any?]? = nil

  static func fromList(_ list: [Any]) -> ResponseParam? {
    let code = ResponseCode(rawValue: list[0] as! Int)!
    let message = list[1] as! String? 
    let data = list[2] as! [String?: Any?]? 

    return ResponseParam(
      code: code,
      message: message,
      data: data
    )
  }
  func toList() -> [Any?] {
    return [
      code.rawValue,
      message,
      data,
    ]
  }
}

When I use it like this, it crashes

func disconnect(param: RequestParam) throws -> ResponseParam {
    pushBridge?.disConnect()
    
    return ResponseParam(code: .success)
}

I see. The reason for the crash could be that both data and message have been converted to NSNull
image

Proposal

Changing the code from:

    let message = list[1] as! String? 
    let data = list[2] as! [String?: Any?]? 

to

let message = list[1] as? String
let data = list[2] as? [String?: Any?]

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listc: fatal crashCrashes that terminate the processe: OS-version specificAffects only some versions of the relevant operating systemfound in release: 3.7Found to occur in 3.7found in release: 3.9Found to occur in 3.9has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.platform-iosiOS applications specificallyr: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions