1
0
mirror of https://github.com/google/nomulus synced 2026-04-23 17:50:49 +00:00

Add custom YAML serializer for Duration (#2161)

* Add custom YAML serializer for Duration

This addresses b/301119144. This changes the YAML representation of a TLD to show Duration fields as a String reperesntation using the Java Duration object's toString() format. This eliminates the previous ambiguity over the time unit that is being used for each duration.

* change standardSeconds to standardMinutes in test

* Add custom serializer to the entire mapper
This commit is contained in:
sarahcaseybot
2023-10-03 13:46:19 -04:00
committed by GitHub
parent 87e54c001f
commit cc1777af0c
13 changed files with 119 additions and 101 deletions

View File

@@ -58,13 +58,13 @@ public class EntityYamlUtils {
SimpleModule module = new SimpleModule();
module.addSerializer(Money.class, new MoneySerializer());
module.addDeserializer(Money.class, new MoneyDeserializer());
module.addSerializer(Duration.class, new DurationSerializer());
ObjectMapper mapper =
JsonMapper.builder(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER))
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
.build()
.registerModule(module);
mapper.findAndRegisterModules();
.build();
mapper.findAndRegisterModules().registerModule(module);
return mapper;
}
@@ -201,6 +201,24 @@ public class EntityYamlUtils {
}
}
/** A custom JSON serializer for a {@link Duration} object. */
public static class DurationSerializer extends StdSerializer<Duration> {
public DurationSerializer() {
this(null);
}
public DurationSerializer(Class<Duration> t) {
super(t);
}
@Override
public void serialize(Duration value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeString(value.toString());
}
}
/** A custom JSON serializer for an Optional of a {@link Duration} object. */
public static class OptionalDurationSerializer extends StdSerializer<Optional<Duration>> {
@@ -216,7 +234,7 @@ public class EntityYamlUtils {
public void serialize(Optional<Duration> value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
if (value.isPresent()) {
gen.writeNumber(value.get().getMillis());
gen.writeString(value.get().toString());
} else {
gen.writeNull();
}

View File

@@ -45,7 +45,7 @@ class GetTldCommandTest extends CommandTestCase<GetTldCommand> {
PremiumList premiumList = persistPremiumList("test", USD, "silver,USD 50", "gold,USD 80");
persistResource(
tld.asBuilder()
.setDnsAPlusAaaaTtl(Duration.millis(900))
.setDnsAPlusAaaaTtl(Duration.standardMinutes(15))
.setDriveFolderId("driveFolder")
.setCreateBillingCost(Money.of(USD, 25))
.setPremiumList(premiumList)

View File

@@ -1,10 +1,10 @@
addGracePeriodLength: 432000000
addGracePeriodLength: "PT432000S"
allowedFullyQualifiedHostNames:
- "foo"
allowedRegistrantContactIds: []
anchorTenantAddGracePeriodLength: 2592000000
autoRenewGracePeriodLength: 3888000000
automaticTransferLength: 432000000
anchorTenantAddGracePeriodLength: "PT2592000S"
autoRenewGracePeriodLength: "PT3888000S"
automaticTransferLength: "PT432000S"
claimsPeriodEnd: "294247-01-10T04:00:54.775Z"
createBillingCost:
currency: "USD"
@@ -13,7 +13,7 @@ creationTime: "1970-01-01T00:00:00.000Z"
currency: "USD"
defaultPromoTokens:
- "bbbbb"
dnsAPlusAaaaTtl: 3600000
dnsAPlusAaaaTtl: "PT3600S"
dnsDsTtl: null
dnsNsTtl: null
dnsPaused: false
@@ -38,10 +38,10 @@ idnTables:
invoicingEnabled: false
lordnUsername: null
numDnsPublishLocks: 1
pendingDeleteLength: 432000000
pendingDeleteLength: "PT432000S"
premiumListName: "tld"
pricingEngineClassName: "google.registry.model.pricing.StaticPremiumListPricingEngine"
redemptionGracePeriodLength: 2592000000
redemptionGracePeriodLength: "PT2592000S"
registryLockOrUnlockBillingCost:
currency: "USD"
amount: 0.00
@@ -49,7 +49,7 @@ renewBillingCostTransitions:
"1970-01-01T00:00:00.000Z":
currency: "USD"
amount: 11.00
renewGracePeriodLength: 432000000
renewGracePeriodLength: "PT432000S"
reservedListNames: []
restoreBillingCost:
currency: "USD"
@@ -63,4 +63,4 @@ tldStateTransitions:
tldStr: "tld"
tldType: "REAL"
tldUnicode: "tld"
transferGracePeriodLength: 432000000
transferGracePeriodLength: "PT432000S"

View File

@@ -1,9 +1,9 @@
addGracePeriodLength: 432000000
addGracePeriodLength: "PT2592000S"
allowedFullyQualifiedHostNames: []
allowedRegistrantContactIds: []
anchorTenantAddGracePeriodLength: 2592000000
autoRenewGracePeriodLength: 3888000000
automaticTransferLength: 432000000
anchorTenantAddGracePeriodLength: "PT2592000S"
autoRenewGracePeriodLength: "PT2592000S"
automaticTransferLength: "PT2592000S"
claimsPeriodEnd: "294247-01-10T04:00:54.775Z"
createBillingCost:
currency: "USD"
@@ -11,7 +11,7 @@ createBillingCost:
creationTime: "2022-09-01T00:00:00.000Z"
currency: "USD"
defaultPromoTokens: []
dnsAPlusAaaaTtl: 900
dnsAPlusAaaaTtl: "PT900S"
dnsDsTtl: null
dnsNsTtl: null
dnsPaused: false
@@ -27,10 +27,10 @@ idnTables: []
invoicingEnabled: false
lordnUsername: null
numDnsPublishLocks: 1
pendingDeleteLength: 432000000
pendingDeleteLength: "PT2592000S"
premiumListName: "test"
pricingEngineClassName: "google.registry.model.pricing.StaticPremiumListPricingEngine"
redemptionGracePeriodLength: 2592000000
redemptionGracePeriodLength: "PT2592000S"
registryLockOrUnlockBillingCost:
currency: "USD"
amount: 0.00
@@ -38,7 +38,7 @@ renewBillingCostTransitions:
"1970-01-01T00:00:00.000Z":
currency: "USD"
amount: 11.00
renewGracePeriodLength: 432000000
renewGracePeriodLength: "PT2592000S"
reservedListNames: []
restoreBillingCost:
currency: "USD"
@@ -52,4 +52,4 @@ tldStateTransitions:
tldStr: "1tld"
tldType: "REAL"
tldUnicode: "1tld"
transferGracePeriodLength: 432000000
transferGracePeriodLength: "PT2592000S"

View File

@@ -1,9 +1,9 @@
addGracePeriodLength: 432000000
addGracePeriodLength: "PT2592000S"
allowedFullyQualifiedHostNames: []
allowedRegistrantContactIds: []
anchorTenantAddGracePeriodLength: 2592000000
autoRenewGracePeriodLength: 3888000000
automaticTransferLength: 432000000
anchorTenantAddGracePeriodLength: "PT2592000S"
autoRenewGracePeriodLength: "PT2592000S"
automaticTransferLength: "PT2592000S"
claimsPeriodEnd: "294247-01-10T04:00:54.775Z"
createBillingCost:
currency: "USD"
@@ -11,7 +11,7 @@ createBillingCost:
creationTime: "2022-09-01T00:00:00.000Z"
currency: "USD"
defaultPromoTokens: []
dnsAPlusAaaaTtl: 900
dnsAPlusAaaaTtl: "PT900S"
dnsDsTtl: null
dnsNsTtl: null
dnsPaused: false
@@ -28,10 +28,10 @@ idnTables:
invoicingEnabled: false
lordnUsername: null
numDnsPublishLocks: 1
pendingDeleteLength: 432000000
pendingDeleteLength: "PT2592000S"
premiumListName: "test"
pricingEngineClassName: "google.registry.model.pricing.StaticPremiumListPricingEngine"
redemptionGracePeriodLength: 2592000000
redemptionGracePeriodLength: "PT2592000S"
registryLockOrUnlockBillingCost:
currency: "USD"
amount: 0.00
@@ -39,7 +39,7 @@ renewBillingCostTransitions:
"1970-01-01T00:00:00.000Z":
currency: "USD"
amount: 11.00
renewGracePeriodLength: 432000000
renewGracePeriodLength: "PT2592000S"
reservedListNames: []
restoreBillingCost:
currency: "USD"
@@ -53,4 +53,4 @@ tldStateTransitions:
tldStr: "badidn"
tldType: "REAL"
tldUnicode: "badidn"
transferGracePeriodLength: 432000000
transferGracePeriodLength: "PT2592000S"

View File

@@ -1,9 +1,9 @@
addGracePeriodLength: 432000000
addGracePeriodLength: "PT2592000S"
allowedFullyQualifiedHostNames: []
allowedRegistrantContactIds: []
anchorTenantAddGracePeriodLength: 2592000000
autoRenewGracePeriodLength: 3888000000
automaticTransferLength: 432000000
anchorTenantAddGracePeriodLength: "PT2592000S"
autoRenewGracePeriodLength: "PT2592000S"
automaticTransferLength: "PT2592000S"
claimsPeriodEnd: "294247-01-10T04:00:54.775Z"
createBillingCost:
currency: "USD"
@@ -11,7 +11,7 @@ createBillingCost:
creationTime: "2022-09-01T00:00:00.000Z"
currency: "USD"
defaultPromoTokens: []
dnsAPlusAaaaTtl: 900
dnsAPlusAaaaTtl: "PT900S"
dnsDsTtl: null
dnsNsTtl: null
dnsPaused: false
@@ -27,10 +27,10 @@ idnTables: []
invoicingEnabled: false
lordnUsername: null
numDnsPublishLocks: 1
pendingDeleteLength: 432000000
pendingDeleteLength: "PT2592000S"
premiumListName: "test"
pricingEngineClassName: "google.registry.model.pricing.StaticPremiumListPricingEngine"
redemptionGracePeriodLength: 2592000000
redemptionGracePeriodLength: "PT2592000S"
registryLockOrUnlockBillingCost:
currency: "USD"
amount: 0.00
@@ -38,7 +38,7 @@ renewBillingCostTransitions:
"1970-01-01T00:00:00.000Z":
currency: "USD"
amount: 11.00
renewGracePeriodLength: 432000000
renewGracePeriodLength: "PT2592000S"
reservedListNames: []
restoreBillingCost:
currency: "USD"
@@ -52,4 +52,4 @@ tldStateTransitions:
tldStr: "badunicode"
tldType: "REAL"
tldUnicode: "tld"
transferGracePeriodLength: 432000000
transferGracePeriodLength: "PT2592000S"

View File

@@ -1,9 +1,9 @@
addGracePeriodLength: 432000000
addGracePeriodLength: "PT2592000S"
allowedFullyQualifiedHostNames: []
allowedRegistrantContactIds: []
anchorTenantAddGracePeriodLength: 2592000000
autoRenewGracePeriodLength: 3888000000
automaticTransferLength: 432000000
anchorTenantAddGracePeriodLength: "PT2592000S"
autoRenewGracePeriodLength: "PT2592000S"
automaticTransferLength: "PT2592000S"
claimsPeriodEnd: "294247-01-10T04:00:54.775Z"
createBillingCost:
currency: "USD"
@@ -11,7 +11,7 @@ createBillingCost:
creationTime: "2022-09-01T00:00:00.000Z"
currency: "USD"
defaultPromoTokens: []
dnsAPlusAaaaTtl: 900
dnsAPlusAaaaTtl: "PT900S"
dnsDsTtl: null
dnsNsTtl: null
dnsPaused: false
@@ -27,10 +27,10 @@ idnTables: []
invoicingEnabled: false
lordnUsername: null
numDnsPublishLocks: 1
pendingDeleteLength: 432000000
pendingDeleteLength: "PT2592000S"
premiumListName: "test"
pricingEngineClassName: "google.registry.model.pricing.StaticPremiumListPricingEngine"
redemptionGracePeriodLength: 2592000000
redemptionGracePeriodLength: "PT2592000S"
registryLockOrUnlockBillingCost:
currency: "USD"
amount: 0.00
@@ -38,7 +38,7 @@ renewBillingCostTransitions:
"1970-01-01T00:00:00.000Z":
currency: "USD"
amount: 11.00
renewGracePeriodLength: 432000000
renewGracePeriodLength: "PT2592000S"
reservedListNames: []
restoreBillingCost:
currency: "USD"
@@ -52,5 +52,5 @@ tldStateTransitions:
tldStr: "extrafield"
tldType: "REAL"
tldUnicode: "extrafield"
transferGracePeriodLength: 432000000
transferGracePeriodLength: "PT2592000S"
extraField: "hello"

View File

@@ -1,13 +1,13 @@
addGracePeriodLength: 432000000
addGracePeriodLength: "PT432000S"
allowedFullyQualifiedHostNames:
- "beta"
- "zeta"
- "alpha"
- "gamma"
allowedRegistrantContactIds: []
anchorTenantAddGracePeriodLength: 2592000000
autoRenewGracePeriodLength: 3888000000
automaticTransferLength: 432000000
anchorTenantAddGracePeriodLength: "PT2592000S"
autoRenewGracePeriodLength: "PT3888000S"
automaticTransferLength: "PT432000S"
claimsPeriodEnd: "294247-01-10T04:00:54.775Z"
createBillingCost:
currency: "USD"
@@ -34,10 +34,10 @@ idnTables:
invoicingEnabled: false
lordnUsername: null
numDnsPublishLocks: 1
pendingDeleteLength: 432000000
pendingDeleteLength: "PT432000S"
premiumListName: "idns"
pricingEngineClassName: "google.registry.model.pricing.StaticPremiumListPricingEngine"
redemptionGracePeriodLength: 2592000000
redemptionGracePeriodLength: "PT2592000S"
registryLockOrUnlockBillingCost:
currency: "USD"
amount: 0.00
@@ -45,7 +45,7 @@ renewBillingCostTransitions:
"1970-01-01T00:00:00.000Z":
currency: "USD"
amount: 11.00
renewGracePeriodLength: 432000000
renewGracePeriodLength: "PT432000S"
reservedListNames: []
restoreBillingCost:
currency: "USD"
@@ -59,4 +59,4 @@ tldStateTransitions:
tldStr: "idns"
tldType: "REAL"
tldUnicode: "idns"
transferGracePeriodLength: 432000000
transferGracePeriodLength: "PT432000S"

View File

@@ -1,9 +1,9 @@
addGracePeriodLength: 432000000
addGracePeriodLength: "PT2592000S"
allowedFullyQualifiedHostNames: []
allowedRegistrantContactIds: []
anchorTenantAddGracePeriodLength: 2592000000
automaticTransferLength: 432000000
autoRenewGracePeriodLength: 3888000000
anchorTenantAddGracePeriodLength: "PT2592000S"
automaticTransferLength: "PT2592000S"
autoRenewGracePeriodLength: "PT2592000S"
claimsPeriodEnd: "294247-01-10T04:00:54.775Z"
createBillingCost:
currency: "USD"
@@ -25,9 +25,9 @@ escrowEnabled: false
idnTables: []
invoicingEnabled: false
lordnUsername: null
pendingDeleteLength: 432000000
pendingDeleteLength: "PT2592000S"
pricingEngineClassName: "google.registry.model.pricing.StaticPremiumListPricingEngine"
redemptionGracePeriodLength: 2592000000
redemptionGracePeriodLength: "PT2592000S"
registryLockOrUnlockBillingCost:
currency: "USD"
amount: 0.00
@@ -35,7 +35,7 @@ renewBillingCostTransitions:
"1970-01-01T00:00:00.000Z":
currency: "USD"
amount: 11.00
renewGracePeriodLength: 432000000
renewGracePeriodLength: "PT2592000S"
reservedListNames: []
restoreBillingCost:
currency: "USD"
@@ -47,4 +47,4 @@ serverStatusChangeBillingCost:
tldStr: "missingnullablefields"
tldType: "REAL"
tldUnicode: "missingnullablefields"
transferGracePeriodLength: 432000000
transferGracePeriodLength: "PT2592000S"

View File

@@ -18,14 +18,14 @@ reservedListNames: null
premiumListName: null
escrowEnabled: false
dnsPaused: false
addGracePeriodLength: 432000000
anchorTenantAddGracePeriodLength: 2592000000
autoRenewGracePeriodLength: 3888000000
redemptionGracePeriodLength: 2592000000
renewGracePeriodLength: 432000000
transferGracePeriodLength: 432000000
automaticTransferLength: 432000000
pendingDeleteLength: 432000000
addGracePeriodLength: "PT2592000S"
anchorTenantAddGracePeriodLength: "PT2592000S"
autoRenewGracePeriodLength: "PT2592000S"
redemptionGracePeriodLength: "PT2592000S"
renewGracePeriodLength: "PT2592000S"
transferGracePeriodLength: "PT2592000S"
automaticTransferLength: "PT2592000S"
pendingDeleteLength: "PT2592000S"
currency: "USD"
createBillingCost:
currency: "USD"

View File

@@ -5,13 +5,13 @@ reservedListNames: []
dnsPaused: false
tldType: "REAL"
escrowEnabled: false
anchorTenantAddGracePeriodLength: 2592000000
anchorTenantAddGracePeriodLength: "PT2592000S"
dnsNsTtl: null
tldStr: "outoforderfields"
roidSuffix: "TLD"
dnsWriters:
- "VoidDnsWriter"
dnsAPlusAaaaTtl: 900
dnsAPlusAaaaTtl: "PT900S"
dnsDsTtl: null
tldUnicode: "outoforderfields"
driveFolderId: "driveFolder"
@@ -19,13 +19,13 @@ invoicingEnabled: false
tldStateTransitions:
"1970-01-01T00:00:00.000Z": "GENERAL_AVAILABILITY"
premiumListName: "test"
addGracePeriodLength: 432000000
autoRenewGracePeriodLength: 3888000000
redemptionGracePeriodLength: 2592000000
renewGracePeriodLength: 432000000
transferGracePeriodLength: 432000000
automaticTransferLength: 432000000
pendingDeleteLength: 432000000
addGracePeriodLength: "PT2592000S"
autoRenewGracePeriodLength: "PT2592000S"
redemptionGracePeriodLength: "PT2592000S"
renewGracePeriodLength: "PT2592000S"
transferGracePeriodLength: "PT2592000S"
automaticTransferLength: "PT2592000S"
pendingDeleteLength: "PT2592000S"
currency: "USD"
createBillingCost:
currency: "USD"

View File

@@ -1,9 +1,9 @@
addGracePeriodLength: 432000000
addGracePeriodLength: "PT432000S"
allowedFullyQualifiedHostNames: []
allowedRegistrantContactIds: []
anchorTenantAddGracePeriodLength: 2592000000
autoRenewGracePeriodLength: 3888000000
automaticTransferLength: 432000000
anchorTenantAddGracePeriodLength: "PT2592000S"
autoRenewGracePeriodLength: "PT3888000S"
automaticTransferLength: "PT432000S"
claimsPeriodEnd: "294247-01-10T04:00:54.775Z"
createBillingCost:
currency: "USD"
@@ -11,7 +11,7 @@ createBillingCost:
creationTime: "2022-09-01T00:00:00.000Z"
currency: "USD"
defaultPromoTokens: []
dnsAPlusAaaaTtl: 900
dnsAPlusAaaaTtl: "PT900S"
dnsDsTtl: null
dnsNsTtl: null
dnsPaused: false
@@ -27,10 +27,10 @@ idnTables: []
invoicingEnabled: false
lordnUsername: null
numDnsPublishLocks: 1
pendingDeleteLength: 432000000
pendingDeleteLength: "PT432000S"
premiumListName: "test"
pricingEngineClassName: "google.registry.model.pricing.StaticPremiumListPricingEngine"
redemptionGracePeriodLength: 2592000000
redemptionGracePeriodLength: "PT2592000S"
registryLockOrUnlockBillingCost:
currency: "USD"
amount: 0.00
@@ -38,7 +38,7 @@ renewBillingCostTransitions:
"1970-01-01T00:00:00.000Z":
currency: "USD"
amount: 11.00
renewGracePeriodLength: 432000000
renewGracePeriodLength: "PT432000S"
reservedListNames: []
restoreBillingCost:
currency: "USD"
@@ -52,4 +52,4 @@ tldStateTransitions:
tldStr: "tld"
tldType: "REAL"
tldUnicode: "tld"
transferGracePeriodLength: 432000000
transferGracePeriodLength: "PT432000S"

View File

@@ -1,9 +1,9 @@
addGracePeriodLength: 432000000
addGracePeriodLength: "PT432000S"
allowedFullyQualifiedHostNames: []
allowedRegistrantContactIds: []
anchorTenantAddGracePeriodLength: 2592000000
autoRenewGracePeriodLength: 3888000000
automaticTransferLength: 432000000
anchorTenantAddGracePeriodLength: "PT2592000S"
autoRenewGracePeriodLength: "PT3888000S"
automaticTransferLength: "PT432000S"
claimsPeriodEnd: "294247-01-10T04:00:54.775Z"
createBillingCost:
currency: "USD"
@@ -11,7 +11,7 @@ createBillingCost:
creationTime: "2022-09-01T00:00:00.000Z"
currency: "USD"
defaultPromoTokens: []
dnsAPlusAaaaTtl: 900
dnsAPlusAaaaTtl: "PT900S"
dnsDsTtl: null
dnsNsTtl: null
dnsPaused: false
@@ -27,10 +27,10 @@ idnTables: []
invoicingEnabled: false
lordnUsername: null
numDnsPublishLocks: 1
pendingDeleteLength: 432000000
pendingDeleteLength: "PT432000S"
premiumListName: "test"
pricingEngineClassName: "google.registry.model.pricing.StaticPremiumListPricingEngine"
redemptionGracePeriodLength: 2592000000
redemptionGracePeriodLength: "PT2592000S"
registryLockOrUnlockBillingCost:
currency: "USD"
amount: 0.00
@@ -38,7 +38,7 @@ renewBillingCostTransitions:
"1970-01-01T00:00:00.000Z":
currency: "USD"
amount: 11.00
renewGracePeriodLength: 432000000
renewGracePeriodLength: "PT432000S"
reservedListNames: []
restoreBillingCost:
currency: "USD"
@@ -52,4 +52,4 @@ tldStateTransitions:
tldStr: %TLDSTR%
tldType: "REAL"
tldUnicode: %TLDUNICODE%
transferGracePeriodLength: 432000000
transferGracePeriodLength: "PT432000S"