From b38e0efe9af1d0cd09bc14d7cb4c3b010707af71 Mon Sep 17 00:00:00 2001 From: Ben McIlwain Date: Fri, 18 Aug 2023 12:30:35 -0400 Subject: [PATCH] Refactor the way that the console BE parses POST bodies (#2113) (#2109) This includes two changes: 1. Creating a base string-type adapter for use parsing to/from JSON classes that are represented as simple strings 2. Changing the object-provider methods so that the POST bodies should contain precisely the expected object(s) and nothing else. This way, it's easier for the frontend and backend to agree that, for instance, one POST endpoint might accept exactly a Registrar object, or a list of Contact objects. Co-Authored-By: gbrodman --- ...TldYamlUtils.java => EntityYamlUtils.java} | 14 ++++----- .../google/registry/model/ModelModule.java | 30 +++++++++++++++++++ .../java/google/registry/model/tld/Tld.java | 18 +++++------ .../google/registry/tools/GetTldCommand.java | 7 +++-- .../registry/tools/RegistryToolComponent.java | 7 +++-- .../google/registry/model/tld/TldTest.java | 8 ++--- .../registry/tools/GetTldCommandTest.java | 7 +++++ 7 files changed, 66 insertions(+), 25 deletions(-) rename core/src/main/java/google/registry/model/{tld/TldYamlUtils.java => EntityYamlUtils.java} (96%) create mode 100644 core/src/main/java/google/registry/model/ModelModule.java diff --git a/core/src/main/java/google/registry/model/tld/TldYamlUtils.java b/core/src/main/java/google/registry/model/EntityYamlUtils.java similarity index 96% rename from core/src/main/java/google/registry/model/tld/TldYamlUtils.java rename to core/src/main/java/google/registry/model/EntityYamlUtils.java index d6a969de0..a08455ebf 100644 --- a/core/src/main/java/google/registry/model/tld/TldYamlUtils.java +++ b/core/src/main/java/google/registry/model/EntityYamlUtils.java @@ -11,7 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -package google.registry.model.tld; +package google.registry.model; import static com.google.common.collect.ImmutableSortedMap.toImmutableSortedMap; import static com.google.common.collect.Ordering.natural; @@ -27,7 +27,6 @@ import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; -import google.registry.model.CreateAutoTimestamp; import google.registry.model.common.TimedTransitionProperty; import google.registry.model.domain.token.AllocationToken; import google.registry.model.tld.Tld.TldState; @@ -44,14 +43,13 @@ import org.joda.money.Money; import org.joda.time.DateTime; import org.joda.time.Duration; -/** A collection of static utility classes and functions for TLD YAML conversions. */ -public class TldYamlUtils { +/** A collection of static utility classes/functions to convert entities to/from YAML files. */ +public class EntityYamlUtils { /** - * Returns an {@link ObjectMapper} object that can be used to convert a {@link Tld} object to and - * from YAML. + * Returns a new {@link ObjectMapper} object that can be used to convert an entity to/from YAML. */ - public static ObjectMapper getObjectMapper() { + public static ObjectMapper createObjectMapper() { SimpleModule module = new SimpleModule(); module.addSerializer(Money.class, new MoneySerializer()); module.addDeserializer(Money.class, new MoneyDeserializer()); @@ -86,6 +84,7 @@ public class TldYamlUtils { /** A custom JSON deserializer for {@link Money}. */ public static class MoneyDeserializer extends StdDeserializer { + public MoneyDeserializer() { this(null); } @@ -127,6 +126,7 @@ public class TldYamlUtils { /** A custom JSON deserializer for {@link CurrencyUnit}. */ public static class CurrencyDeserializer extends StdDeserializer { + public CurrencyDeserializer() { this(null); } diff --git a/core/src/main/java/google/registry/model/ModelModule.java b/core/src/main/java/google/registry/model/ModelModule.java new file mode 100644 index 000000000..8e75e61ab --- /dev/null +++ b/core/src/main/java/google/registry/model/ModelModule.java @@ -0,0 +1,30 @@ +// Copyright 2023 The Nomulus Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package google.registry.model; + +import com.fasterxml.jackson.databind.ObjectMapper; +import dagger.Module; +import dagger.Provides; + +/** Dagger module for the entity (model) classes. */ +@Module +public final class ModelModule { + + /** Returns an {@link ObjectMapper} object that can be used to convert an entity to/from YAML. */ + @Provides + public static ObjectMapper provideObjectMapper() { + return EntityYamlUtils.createObjectMapper(); + } +} diff --git a/core/src/main/java/google/registry/model/tld/Tld.java b/core/src/main/java/google/registry/model/tld/Tld.java index 4178f0e7e..1af964a52 100644 --- a/core/src/main/java/google/registry/model/tld/Tld.java +++ b/core/src/main/java/google/registry/model/tld/Tld.java @@ -45,6 +45,15 @@ import com.google.common.net.InternetDomainName; import google.registry.model.Buildable; import google.registry.model.CacheUtils; import google.registry.model.CreateAutoTimestamp; +import google.registry.model.EntityYamlUtils.CreateAutoTimestampDeserializer; +import google.registry.model.EntityYamlUtils.CurrencyDeserializer; +import google.registry.model.EntityYamlUtils.CurrencySerializer; +import google.registry.model.EntityYamlUtils.OptionalDurationSerializer; +import google.registry.model.EntityYamlUtils.OptionalStringSerializer; +import google.registry.model.EntityYamlUtils.TimedTransitionPropertyMoneyDeserializer; +import google.registry.model.EntityYamlUtils.TimedTransitionPropertyTldStateDeserializer; +import google.registry.model.EntityYamlUtils.TokenVKeyListDeserializer; +import google.registry.model.EntityYamlUtils.TokenVKeyListSerializer; import google.registry.model.ImmutableObject; import google.registry.model.UnsafeSerializable; import google.registry.model.common.TimedTransitionProperty; @@ -52,15 +61,6 @@ import google.registry.model.domain.fee.BaseFee.FeeType; import google.registry.model.domain.fee.Fee; import google.registry.model.domain.token.AllocationToken; import google.registry.model.domain.token.AllocationToken.TokenType; -import google.registry.model.tld.TldYamlUtils.CreateAutoTimestampDeserializer; -import google.registry.model.tld.TldYamlUtils.CurrencyDeserializer; -import google.registry.model.tld.TldYamlUtils.CurrencySerializer; -import google.registry.model.tld.TldYamlUtils.OptionalDurationSerializer; -import google.registry.model.tld.TldYamlUtils.OptionalStringSerializer; -import google.registry.model.tld.TldYamlUtils.TimedTransitionPropertyMoneyDeserializer; -import google.registry.model.tld.TldYamlUtils.TimedTransitionPropertyTldStateDeserializer; -import google.registry.model.tld.TldYamlUtils.TokenVKeyListDeserializer; -import google.registry.model.tld.TldYamlUtils.TokenVKeyListSerializer; import google.registry.model.tld.label.PremiumList; import google.registry.model.tld.label.ReservedList; import google.registry.persistence.VKey; diff --git a/core/src/main/java/google/registry/tools/GetTldCommand.java b/core/src/main/java/google/registry/tools/GetTldCommand.java index b68dafef0..f2c98e092 100644 --- a/core/src/main/java/google/registry/tools/GetTldCommand.java +++ b/core/src/main/java/google/registry/tools/GetTldCommand.java @@ -14,7 +14,6 @@ package google.registry.tools; -import static google.registry.model.tld.TldYamlUtils.getObjectMapper; import static google.registry.model.tld.Tlds.assertTldsExist; import static java.nio.charset.StandardCharsets.UTF_8; @@ -26,6 +25,7 @@ import google.registry.model.tld.Tld; import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.List; +import javax.inject.Inject; /** Command to show a TLD record. */ @Parameters(separators = " =", commandDescription = "Show TLD record(s)") @@ -36,12 +36,13 @@ final class GetTldCommand implements Command { required = true) private List mainParameters; + @Inject ObjectMapper objectMapper; + @Override public void run() throws JsonProcessingException, UnsupportedEncodingException { - ObjectMapper mapper = getObjectMapper(); try (PrintStream printStream = new PrintStream(System.out, false, UTF_8.name())) { for (String tld : assertTldsExist(mainParameters)) { - printStream.println(mapper.writeValueAsString(Tld.get(tld))); + printStream.println(objectMapper.writeValueAsString(Tld.get(tld))); } } } diff --git a/core/src/main/java/google/registry/tools/RegistryToolComponent.java b/core/src/main/java/google/registry/tools/RegistryToolComponent.java index 081056435..3cd155f56 100644 --- a/core/src/main/java/google/registry/tools/RegistryToolComponent.java +++ b/core/src/main/java/google/registry/tools/RegistryToolComponent.java @@ -30,10 +30,12 @@ import google.registry.keyring.KeyringModule; import google.registry.keyring.api.DummyKeyringModule; import google.registry.keyring.api.KeyModule; import google.registry.keyring.secretmanager.SecretManagerKeyringModule; +import google.registry.model.ModelModule; import google.registry.persistence.PersistenceModule; import google.registry.persistence.PersistenceModule.NomulusToolJpaTm; import google.registry.persistence.PersistenceModule.ReadOnlyReplicaJpaTm; import google.registry.persistence.transaction.JpaTransactionManager; +import google.registry.privileges.secretmanager.SecretManagerModule; import google.registry.rde.RdeModule; import google.registry.request.Modules.GsonModule; import google.registry.request.Modules.UrlConnectionServiceModule; @@ -66,13 +68,14 @@ import javax.inject.Singleton; GsonModule.class, KeyModule.class, KeyringModule.class, - SecretManagerKeyringModule.class, LocalCredentialModule.class, + ModelModule.class, PersistenceModule.class, RdeModule.class, RegistryToolDataflowModule.class, RequestFactoryModule.class, - google.registry.privileges.secretmanager.SecretManagerModule.class, + SecretManagerKeyringModule.class, + SecretManagerModule.class, UrlConnectionServiceModule.class, UrlFetchServiceModule.class, UserServiceModule.class, diff --git a/core/src/test/java/google/registry/model/tld/TldTest.java b/core/src/test/java/google/registry/model/tld/TldTest.java index 7d06f6cc8..54b9b0307 100644 --- a/core/src/test/java/google/registry/model/tld/TldTest.java +++ b/core/src/test/java/google/registry/model/tld/TldTest.java @@ -17,6 +17,7 @@ package google.registry.model.tld; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth8.assertThat; +import static google.registry.model.EntityYamlUtils.createObjectMapper; import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects; import static google.registry.model.domain.token.AllocationToken.TokenType.DEFAULT_PROMO; import static google.registry.model.domain.token.AllocationToken.TokenType.SINGLE_USE; @@ -24,7 +25,6 @@ import static google.registry.model.tld.Tld.TldState.GENERAL_AVAILABILITY; import static google.registry.model.tld.Tld.TldState.PREDELEGATION; import static google.registry.model.tld.Tld.TldState.QUIET_PERIOD; import static google.registry.model.tld.Tld.TldState.START_DATE_SUNRISE; -import static google.registry.model.tld.TldYamlUtils.getObjectMapper; import static google.registry.persistence.transaction.TransactionManagerFactory.tm; import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.newTld; @@ -129,7 +129,7 @@ public final class TldTest extends EntityTestCase { .setIdnTables(ImmutableSet.of(IdnTableEnum.JA, IdnTableEnum.EXTENDED_LATIN)) .build(); - ObjectMapper mapper = getObjectMapper(); + ObjectMapper mapper = createObjectMapper(); String yaml = mapper.writeValueAsString(existingTld); assertThat(yaml).isEqualTo(loadFile(getClass(), "tld.yaml")); } @@ -162,7 +162,7 @@ public final class TldTest extends EntityTestCase { .setIdnTables(ImmutableSet.of(IdnTableEnum.JA, IdnTableEnum.EXTENDED_LATIN)) .build(); - ObjectMapper mapper = getObjectMapper(); + ObjectMapper mapper = createObjectMapper(); Tld constructedTld = mapper.readValue(readResourceBytes(getClass(), "tld.yaml").openBufferedStream(), Tld.class); compareTlds(existingTld, constructedTld); @@ -171,7 +171,7 @@ public final class TldTest extends EntityTestCase { @Test void testSuccess_tldYamlRoundtrip() throws Exception { Tld testTld = createTld("test"); - ObjectMapper mapper = getObjectMapper(); + ObjectMapper mapper = createObjectMapper(); String yaml = mapper.writeValueAsString(testTld); Tld constructedTld = mapper.readValue(yaml, Tld.class); compareTlds(testTld, constructedTld); diff --git a/core/src/test/java/google/registry/tools/GetTldCommandTest.java b/core/src/test/java/google/registry/tools/GetTldCommandTest.java index 5ce11fd27..bd5b1aadb 100644 --- a/core/src/test/java/google/registry/tools/GetTldCommandTest.java +++ b/core/src/test/java/google/registry/tools/GetTldCommandTest.java @@ -20,11 +20,18 @@ import static google.registry.testing.TestDataHelper.loadFile; import static org.junit.jupiter.api.Assertions.assertThrows; import com.beust.jcommander.ParameterException; +import google.registry.model.EntityYamlUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** Unit tests for {@link GetTldCommand}. */ class GetTldCommandTest extends CommandTestCase { + @BeforeEach + void beforeEach() { + command.objectMapper = EntityYamlUtils.createObjectMapper(); + } + @Test void testSuccess() throws Exception { createTld("xn--q9jyb4c");