Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option to fail on unknown enum values #74

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
public class ProtobufJacksonConfig {
private final ExtensionRegistryWrapper extensionRegistry;
private final boolean acceptLiteralFieldnames;
private final UnknownEnumSerializationStrategy unknownEnumSerializationStrategy;

private ProtobufJacksonConfig(ExtensionRegistryWrapper extensionRegistry, boolean acceptLiteralFieldnames) {
private ProtobufJacksonConfig(ExtensionRegistryWrapper extensionRegistry, boolean acceptLiteralFieldnames,
UnknownEnumSerializationStrategy unknownEnumSerializationStrategy) {
this.extensionRegistry = extensionRegistry;
this.acceptLiteralFieldnames = acceptLiteralFieldnames;
this.unknownEnumSerializationStrategy = unknownEnumSerializationStrategy;
}

public static Builder builder() {
Expand All @@ -23,9 +26,14 @@ public boolean acceptLiteralFieldnames() {
return acceptLiteralFieldnames;
}

public UnknownEnumSerializationStrategy unknownEnumSerializationStrategy() {
return unknownEnumSerializationStrategy;
}

public static class Builder {
private ExtensionRegistryWrapper extensionRegistry = ExtensionRegistryWrapper.empty();
private boolean acceptLiteralFieldnames = false;
private UnknownEnumSerializationStrategy unknownEnumSerializationStrategy = UnknownEnumSerializationStrategy.SERIALIZE;

private Builder() {}

Expand All @@ -43,8 +51,13 @@ public Builder acceptLiteralFieldnames(boolean acceptLiteralFieldnames) {
return this;
}

public Builder unknownEnumSerializationStrategy(UnknownEnumSerializationStrategy unknownEnumSerializationStrategy) {
this.unknownEnumSerializationStrategy = unknownEnumSerializationStrategy;
return this;
}

public ProtobufJacksonConfig build() {
return new ProtobufJacksonConfig(extensionRegistry, acceptLiteralFieldnames);
return new ProtobufJacksonConfig(extensionRegistry, acceptLiteralFieldnames, unknownEnumSerializationStrategy);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,22 @@ public Version version() {
public void setupModule(SetupContext context) {
SimpleSerializers serializers = new SimpleSerializers();
serializers.addSerializer(new MessageSerializer(config));
serializers.addSerializer(new DurationSerializer());
serializers.addSerializer(new FieldMaskSerializer());
serializers.addSerializer(new ListValueSerializer());
serializers.addSerializer(new DurationSerializer(config));
serializers.addSerializer(new FieldMaskSerializer(config));
serializers.addSerializer(new ListValueSerializer(config));
serializers.addSerializer(new NullValueSerializer());
serializers.addSerializer(new StructSerializer());
serializers.addSerializer(new TimestampSerializer());
serializers.addSerializer(new ValueSerializer());
serializers.addSerializer(new WrappedPrimitiveSerializer<>(DoubleValue.class));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(FloatValue.class));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(Int64Value.class));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(UInt64Value.class));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(Int32Value.class));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(UInt32Value.class));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(BoolValue.class));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(StringValue.class));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(BytesValue.class));
serializers.addSerializer(new StructSerializer(config));
serializers.addSerializer(new TimestampSerializer(config));
serializers.addSerializer(new ValueSerializer(config));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(DoubleValue.class, config));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(FloatValue.class, config));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(Int64Value.class, config));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(UInt64Value.class, config));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(Int32Value.class, config));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(UInt32Value.class, config));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(BoolValue.class, config));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(StringValue.class, config));
serializers.addSerializer(new WrappedPrimitiveSerializer<>(BytesValue.class, config));

context.addSerializers(serializers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public abstract class ProtobufSerializer<T extends MessageOrBuilder> extends Std
private static final String NULL_VALUE_FULL_NAME = NullValue.getDescriptor().getFullName();

private final Map<Class<?>, JsonSerializer<Object>> serializerCache;
private final ProtobufJacksonConfig config;

public ProtobufSerializer(Class<T> protobufType) {
public ProtobufSerializer(Class<T> protobufType, ProtobufJacksonConfig config) {
super(protobufType);
this.config = config;

this.serializerCache = new ConcurrentHashMap<>();
}
Expand Down Expand Up @@ -84,10 +86,16 @@ protected void writeValue(
// special-case NullValue
if (NULL_VALUE_FULL_NAME.equals(enumDescriptor.getType().getFullName())) {
generator.writeNull();
} else if (writeEnumsUsingIndex(serializerProvider)) {
generator.writeNumber(enumDescriptor.getNumber());
} else {
generator.writeString(enumDescriptor.getName());
if (isUnknownEnumValue(enumDescriptor)) {
enumDescriptor = config.unknownEnumSerializationStrategy().handleUnknownEnumValue(enumDescriptor);
}

if (writeEnumsUsingIndex(serializerProvider)) {
generator.writeNumber(enumDescriptor.getNumber());
} else {
generator.writeString(enumDescriptor.getName());
}
}
break;
case BYTE_STRING:
Expand All @@ -113,6 +121,10 @@ private static boolean writeEnumsUsingIndex(SerializerProvider config) {
return config.isEnabled(SerializationFeature.WRITE_ENUMS_USING_INDEX);
}

private static boolean isUnknownEnumValue(EnumValueDescriptor descriptor) {
return descriptor.getType().findValueByNumber(descriptor.getNumber()) == null;
}

private static IOException unrecognizedType(FieldDescriptor field, JsonGenerator generator) throws IOException {
String error = format("Unrecognized java type '%s' for field %s", field.getJavaType(), field.getFullName());
throw new JsonGenerationException(error, generator);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hubspot.jackson.datatype.protobuf;

import com.google.protobuf.Descriptors.EnumValueDescriptor;

@FunctionalInterface
public interface UnknownEnumSerializationStrategy {

EnumValueDescriptor handleUnknownEnumValue(EnumValueDescriptor descriptor);

UnknownEnumSerializationStrategy SERIALIZE = descriptor -> descriptor;

UnknownEnumSerializationStrategy FAIL =
descriptor -> {
throw new IllegalArgumentException(
"Unable to serialize an unknown enum value " + descriptor.getFullName());
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hubspot.jackson.datatype.protobuf.builtin.serializers;

import com.hubspot.jackson.datatype.protobuf.ProtobufJacksonConfig;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -10,8 +11,8 @@

public class DurationSerializer extends ProtobufSerializer<Duration> {

public DurationSerializer() {
super(Duration.class);
public DurationSerializer(ProtobufJacksonConfig config) {
super(Duration.class, config);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hubspot.jackson.datatype.protobuf.builtin.serializers;

import com.hubspot.jackson.datatype.protobuf.ProtobufJacksonConfig;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -10,8 +11,8 @@

public class FieldMaskSerializer extends ProtobufSerializer<FieldMask> {

public FieldMaskSerializer() {
super(FieldMask.class);
public FieldMaskSerializer(ProtobufJacksonConfig config) {
super(FieldMask.class, config);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hubspot.jackson.datatype.protobuf.builtin.serializers;

import com.hubspot.jackson.datatype.protobuf.ProtobufJacksonConfig;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -12,8 +13,8 @@
public class ListValueSerializer extends ProtobufSerializer<ListValue> {
private static final FieldDescriptor VALUES_FIELD = ListValue.getDescriptor().findFieldByName("values");

public ListValueSerializer() {
super(ListValue.class);
public ListValueSerializer(ProtobufJacksonConfig config) {
super(ListValue.class, config);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public MessageSerializer(ExtensionRegistryWrapper extensionRegistry) {
}

public MessageSerializer(ProtobufJacksonConfig config) {
super(MessageOrBuilder.class);
super(MessageOrBuilder.class, config);

this.config = config;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hubspot.jackson.datatype.protobuf.builtin.serializers;

import com.hubspot.jackson.datatype.protobuf.ProtobufJacksonConfig;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -11,8 +12,8 @@
public class StructSerializer extends ProtobufSerializer<Struct> {
private static final FieldDescriptor FIELDS_FIELD = Struct.getDescriptor().findFieldByName("fields");

public StructSerializer() {
super(Struct.class);
public StructSerializer(ProtobufJacksonConfig config) {
super(Struct.class, config);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hubspot.jackson.datatype.protobuf.builtin.serializers;

import com.hubspot.jackson.datatype.protobuf.ProtobufJacksonConfig;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -10,8 +11,8 @@

public class TimestampSerializer extends ProtobufSerializer<Timestamp> {

public TimestampSerializer() {
super(Timestamp.class);
public TimestampSerializer(ProtobufJacksonConfig config) {
super(Timestamp.class, config);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hubspot.jackson.datatype.protobuf.builtin.serializers;

import com.hubspot.jackson.datatype.protobuf.ProtobufJacksonConfig;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -12,8 +13,8 @@

public class ValueSerializer extends ProtobufSerializer<Value> {

public ValueSerializer() {
super(Value.class);
public ValueSerializer(ProtobufJacksonConfig config) {
super(Value.class, config);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hubspot.jackson.datatype.protobuf.builtin.serializers;

import com.hubspot.jackson.datatype.protobuf.ProtobufJacksonConfig;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -10,8 +11,8 @@

public class WrappedPrimitiveSerializer<T extends MessageOrBuilder> extends ProtobufSerializer<T> {

public WrappedPrimitiveSerializer(Class<T> wrapperType) {
super(wrapperType);
public WrappedPrimitiveSerializer(Class<T> wrapperType, ProtobufJacksonConfig config) {
super(wrapperType, config);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.hubspot.jackson.datatype.protobuf.proto3;

import static com.hubspot.jackson.datatype.protobuf.UnknownEnumSerializationStrategy.FAIL;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubspot.jackson.datatype.protobuf.ProtobufJacksonConfig;
import com.hubspot.jackson.datatype.protobuf.ProtobufModule;
import com.hubspot.jackson.datatype.protobuf.util.TestProtobuf3.NestedProto3;
import org.junit.Test;

public class EnumTest {

@Test
public void itFailsToSerializeAnUnknownEnum() {
ProtobufJacksonConfig config = ProtobufJacksonConfig.builder()
.unknownEnumSerializationStrategy(FAIL)
.build();

ObjectMapper mapper = new ObjectMapper().registerModules(new ProtobufModule(config));

NestedProto3 message = NestedProto3.newBuilder()
.setEnumValue(42)
.build();

assertThatThrownBy(() -> mapper.writeValueAsString(message))
.hasMessageContaining("Unable to serialize an unknown enum value")
.hasRootCauseInstanceOf(IllegalArgumentException.class);
}
}