quarkus-http icon indicating copy to clipboard operation
quarkus-http copied to clipboard

Classes can't implement multiple Decoder/Encoder interfaces

Open micheljung opened this issue 4 years ago • 0 comments

When implementing a class like this:

public class UuidDecoder implements Decoder.TextStream<UUID>, Decoder, Decoder.Binary<UUID>, Decoder.Text<UUID> {
    public UuidDecoder() {
        System.out.println("OK!");
    }

    @Override
    public void init(EndpointConfig config) {

    }

    @Override
    public void destroy() {

    }

    @Override
    public UUID decode(Reader reader) throws DecodeException, IOException {
        return UUID.fromString(CharStreams.toString(reader));
    }

    @Override
    public UUID decode(ByteBuffer bytes) throws DecodeException {
        return UUID.fromString(new String(bytes.array()));
    }

    @Override
    public boolean willDecode(ByteBuffer bytes) {
        try {
            UUID.fromString(new String(bytes.array()));
            return true;
        } catch (IllegalArgumentException e) {
            return false;
        }
    }

    @Override
    public UUID decode(String s) throws DecodeException {
        return UUID.fromString(s);
    }

    @Override
    public boolean willDecode(String s) {
        try {
            UUID.fromString(s);
            return true;
        } catch (IllegalArgumentException e) {
            return false;
        }
    }
}

And using it like this:

@ServerEndpoint(value = "/api/ws/search/{id}", decoders = UuidDecoder.class)
public class SearchResource {
}

Only the binary encoder/decoder is registered, since EncodingFactory checks for interface implementations using else-if.

See https://stackoverflow.com/questions/60134388/how-to-use-custom-encoder-and-decoder-in-quarkus

I'll submit a PR to fix this.

micheljung avatar Feb 07 '21 21:02 micheljung