apocolipse/Thrift-Swift

optional keyword is ignored in argument lists

Closed this issue · 3 comments

Hi, how to be if I need to pass null value in method to the server,
for example
list<User> getUsers(1: AuthTokenBase64 token, 2: ID systemId, 3: othervalues othertypes);
ID is typealias for String. Parameter systemId can be value or null (on other platforms).
How do I set it as null / nil in swift ?
If thrift file has:
list<User> getUsers(1: AuthTokenBase64 token, 2: optional ID systemId, 3: othervalues othertypes);
we get this:
optional keyword is ignored in argument lists

Hello,

I believe you are receiving that warning because thrift does not guarantee it can support optional params in a service function across all generated languages.

As a best practice, it's better to create a struct to wrap your params as part of the request. This also gives you the flexibility in adding new (optional) fields to the struct, which remains backwards compatible with clients.

For your example:

struct UsersRequest {
  1: required AuthTokenBase64 token,
  2: optional ID systemId,
  3: required othervalues othertypes
}
list<User> getUsers(1: UsersRequest request);

As a worst case workaround, you might be able to use the swift.nullable annotation on the param to silence this warning.

2: optional ID systemId (swift.nullable = "")

However, I don't believe it is currently available in the apache source yet since the fork found here is still an opened pull request with the apache team:
https://github.com/apocolipse/thrift

Ok to close? Or are there any remaining open questions in this issue?

Sure