apache / apache/beam

PubsubMessageWithAttributesCoder should not produce null attributes map

Open
#19,588 0 comments 0 reactions 0 assignees View on GitHub
gcp improvement io java P3
Dominant language
Java
Stars
8.7k
Forks
4.7k
Avg merge
1d 20h
Merged PRs (30d)
196

Description

Hi, I just got caught by an issue where PubsubMessage.getAttributeMap() returned null, because the message was created by PubsubMessageWithAttributesCoder which uses a NullableCoder for attributes.

Here are the relevant code snippets:

```

public class PubsubMessageWithAttributesCoder extends CustomCoder {
// A message's
payload can not be null
private static final Coder PAYLOAD_CODER = ByteArrayCoder.of();

// A message's attributes can be null.
private static final Coder> ATTRIBUTES_CODER
=
NullableCoder.of(MapCoder.of(StringUtf8Coder.of(), StringUtf8Coder.of()));

@Override

public PubsubMessage decode(InputStream inStream) throws IOException {
return decode(inStream,
Context.NESTED);
}

@Override
public PubsubMessage decode(InputStream inStream, Context context)
throws IOException {
byte[] payload = PAYLOAD_CODER.decode(inStream);
Map
attributes = ATTRIBUTES_CODER.decode(inStream, context);
return new PubsubMessage(payload, attributes);

}
}

```

 
```

public class PubsubMessage {

private byte[] message;
private Map attributes;

public PubsubMessage(byte[] payload, Map attributes) {
this.message = payload;

this.attributes = attributes;
}

/** Returns the main PubSub message. */
public byte[]
getPayload() {
return message;
}

/** Returns the given attribute value. If not such attribute
exists, returns null. */
@Nullable
public String getAttribute(String attribute) {
checkNotNull(attribute,
"attribute");
return attributes.get(attribute);
}

/** Returns the full map of attributes.
This is an unmodifiable map. */
public Map getAttributeMap() {
return attributes;

}
}

```

There are a handful of potential solutions:

- Remove the NullableCoder
- In PubsubMessageWithAttributesCoder.decode, check for null and create an empty Map before instantiating PubsubMessage
- Allow attributes to be null for PubsubMessage constructor, but create an empty Map if it is (similar to above, but handle it in PubsubMessage)
- Allow PubsubMessage.attributes to be nullable, and indicate it as such

Imported from Jira [BEAM-8085](https://issues.apache.org/jira/browse/BEAM-8085). Original Jira may contain additional context.
Reported by: chadrik.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.