Skip to content

Commit 7ed548e

Browse files
authored
Add forceLowercase in enumOf (#8724)
1 parent 289561c commit 7ed548e

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

data/beaconrestapi/src/integration-test/resources/tech/pegasys/teku/beaconrestapi/beacon/paths/_eth_v1_debug_fork_choice.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
},
6464
"validity" : {
6565
"type" : "string",
66-
"enum" : [ "VALID", "INVALID", "OPTIMISTIC" ]
66+
"enum" : [ "valid", "invalid", "optimistic" ]
6767
},
6868
"execution_block_hash" : {
6969
"type" : "string",

data/beaconrestapi/src/main/java/tech/pegasys/teku/beaconrestapi/handlers/v1/debug/GetForkChoice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class GetForkChoice extends RestApiEndpoint {
7272
.withField("weight", UINT64_TYPE, ProtoNodeData::getWeight)
7373
.withField(
7474
"validity",
75-
DeserializableTypeDefinition.enumOf(ProtoNodeValidationStatus.class),
75+
DeserializableTypeDefinition.enumOf(ProtoNodeValidationStatus.class, true),
7676
ProtoNodeData::getValidationStatus)
7777
.withField(
7878
"execution_block_hash",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"justified_checkpoint":{"epoch":"1","root":"0x0000000000000000000000000000000000000000000000000000000000001111"},"finalized_checkpoint":{"epoch":"0","root":"0x0000000000000000000000000000000000000000000000000000000000002222"},"fork_choice_nodes":[{"slot":"32","block_root":"0x0000000000000000000000000000000000000000000000000000000000003333","parent_root":"0x0000000000000000000000000000000000000000000000000000000000004444","justified_epoch":"10","finalized_epoch":"11","weight":"409600000000","validity":"OPTIMISTIC","execution_block_hash":"0x0000000000000000000000000000000000000000000000000000000000006666","extra_data":{"state_root":"0x0000000000000000000000000000000000000000000000000000000000005555","justified_root":"0x0000000000000000000000000000000000000000000000000000000000007777","unrealised_justified_epoch":"12","unrealized_justified_root":"0x0000000000000000000000000000000000000000000000000000000000009999","unrealised_finalized_epoch":"13","unrealized_finalized_root":"0x0000000000000000000000000000000000000000000000000000000000000000"}}],"extra_data":{}}
1+
{"justified_checkpoint":{"epoch":"1","root":"0x0000000000000000000000000000000000000000000000000000000000001111"},"finalized_checkpoint":{"epoch":"0","root":"0x0000000000000000000000000000000000000000000000000000000000002222"},"fork_choice_nodes":[{"slot":"32","block_root":"0x0000000000000000000000000000000000000000000000000000000000003333","parent_root":"0x0000000000000000000000000000000000000000000000000000000000004444","justified_epoch":"10","finalized_epoch":"11","weight":"409600000000","validity":"optimistic","execution_block_hash":"0x0000000000000000000000000000000000000000000000000000000000006666","extra_data":{"state_root":"0x0000000000000000000000000000000000000000000000000000000000005555","justified_root":"0x0000000000000000000000000000000000000000000000000000000000007777","unrealised_justified_epoch":"12","unrealized_justified_root":"0x0000000000000000000000000000000000000000000000000000000000009999","unrealised_finalized_epoch":"13","unrealized_finalized_root":"0x0000000000000000000000000000000000000000000000000000000000000000"}}],"extra_data":{}}

infrastructure/json/src/main/java/tech/pegasys/teku/infrastructure/json/types/DeserializableTypeDefinition.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ static <TKey, TValue> DeserializableTypeDefinition<Map<TKey, TValue>> mapOf(
6060

6161
static <TObject extends Enum<TObject>> EnumTypeDefinition<TObject> enumOf(
6262
final Class<TObject> itemType) {
63-
return new EnumTypeDefinition.EnumTypeBuilder<>(itemType).build();
63+
return new EnumTypeDefinition.EnumTypeBuilder<>(itemType, false).build();
64+
}
65+
66+
static <TObject extends Enum<TObject>> EnumTypeDefinition<TObject> enumOf(
67+
final Class<TObject> itemType, final boolean forceLowercase) {
68+
return new EnumTypeDefinition.EnumTypeBuilder<>(itemType, forceLowercase).build();
6469
}
6570

6671
static <TObject> DeserializableObjectTypeDefinitionBuilder<TObject, TObject> object(

infrastructure/json/src/main/java/tech/pegasys/teku/infrastructure/json/types/EnumTypeDefinition.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.fasterxml.jackson.core.JsonParser;
1818
import java.io.IOException;
1919
import java.util.HashSet;
20+
import java.util.Locale;
2021
import java.util.Objects;
2122
import java.util.Optional;
2223
import java.util.Set;
@@ -135,8 +136,13 @@ public EnumTypeBuilder(final Class<T> itemType, final Function<T, String> serial
135136
}
136137

137138
public EnumTypeBuilder(final Class<T> itemType) {
138-
this.itemType = itemType;
139-
this.serializer = Enum::toString;
139+
this(itemType, false);
140+
}
141+
142+
public EnumTypeBuilder(final Class<T> itemType, final boolean forceLowercase) {
143+
this(
144+
itemType,
145+
forceLowercase ? (val) -> val.toString().toLowerCase(Locale.ROOT) : Enum::toString);
140146
}
141147

142148
public EnumTypeBuilder<T> parser(final Class<T> itemType) {

infrastructure/json/src/test/java/tech/pegasys/teku/infrastructure/json/types/EnumTypeDefinitionTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ void shouldSerializeEnum() throws Exception {
3131
assertThat(json).isEqualTo("\"yes\"");
3232
}
3333

34+
@Test
35+
void shouldSerializeEnumForceLowerCase() throws Exception {
36+
DeserializableTypeDefinition<YES_NO> definition =
37+
DeserializableTypeDefinition.enumOf(YES_NO.class);
38+
String json = JsonUtil.serialize(YES_NO.YES, definition);
39+
assertThat(json).isEqualTo("\"YES\"");
40+
41+
definition = DeserializableTypeDefinition.enumOf(YES_NO.class, true);
42+
json = JsonUtil.serialize(YES_NO.YES, definition);
43+
assertThat(json).isEqualTo("\"yes\"");
44+
}
45+
3446
@Test
3547
void shouldParseEnum() throws Exception {
3648
assertThat(JsonUtil.parse("\"no\"", definition)).isEqualTo(YesNo.NO);
@@ -73,4 +85,10 @@ public String toString() {
7385
return displayName;
7486
}
7587
}
88+
89+
@SuppressWarnings("JavaCase")
90+
private enum YES_NO {
91+
YES,
92+
NO
93+
}
7694
}

0 commit comments

Comments
 (0)