facebookarchive / swift

An annotation-based Java library for creating Thrift serializable types and services.
Apache License 2.0
900 stars 297 forks source link

java.lang.IllegalArgumentException: Type can not be coerced to a Thrift type: K #336

Closed johje349 closed 7 years ago

johje349 commented 7 years ago

I'm having problems using HashMap with Swift.

Interface:

@ThriftService
public interface DummyService {
    @ThriftMethod
    Map foo() throws TException;
}

Handler:

public class DummyHandler implements DummyService {
    public Map foo() {
        Map<Integer, String> hmap = new HashMap<Integer, String>();

        hmap.put(1, "one");

        return hmap;
    }
}

Server:

public static void main(String [] args) {
        try {
            ThriftServiceProcessor processor = new ThriftServiceProcessor(new ThriftCodecManager(), Arrays.<ThriftEventHandler>asList(), new DummyHandler());
            ThriftServer server = new ThriftServer(processor, new ThriftServerConfig().setPort(8899));

            server.start();
        }
        catch (Exception x) {
            x.printStackTrace();
        }
    }

When I run the server I get the following stack trace: java.lang.IllegalArgumentException: Type can not be coerced to a Thrift type: K at com.facebook.swift.codec.metadata.ThriftCatalog.buildThriftTypeInternal(ThriftCatalog.java:350) at com.facebook.swift.codec.metadata.ThriftCatalog.buildThriftType(ThriftCatalog.java:246) at com.facebook.swift.codec.metadata.ThriftCatalog.getThriftTypeReference(ThriftCatalog.java:426) at com.facebook.swift.codec.metadata.ThriftCatalog.getMapKeyThriftTypeReference(ThriftCatalog.java:394) at com.facebook.swift.codec.metadata.ThriftCatalog.buildThriftTypeInternal(ThriftCatalog.java:318) at com.facebook.swift.codec.metadata.ThriftCatalog.buildThriftType(ThriftCatalog.java:246) at com.facebook.swift.codec.metadata.ThriftCatalog.getThriftType(ThriftCatalog.java:234) at com.facebook.swift.service.metadata.ThriftMethodMetadata.(ThriftMethodMetadata.java:92) at com.facebook.swift.service.metadata.ThriftServiceMetadata.(ThriftServiceMetadata.java:73) at com.facebook.swift.service.ThriftServiceProcessor.(ThriftServiceProcessor.java:78) at com.facebook.swift.service.ThriftServiceProcessor.(ThriftServiceProcessor.java:67) at com.bidtheatre.swift.MultiplicationServer.main(MultiplicationServer.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Ay idea what's going on?

dain commented 7 years ago

The problem is in the signature of the RPC method Map foo() throws TException. The Map return type must have generic types like Map<String, Integer>

johje349 commented 7 years ago

Ok, thanks!