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 <gbrodman@google.com>
This commit is contained in:
Ben McIlwain 2023-08-18 12:30:35 -04:00 committed by GitHub
parent 67cb411c99
commit b38e0efe9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 66 additions and 25 deletions

View file

@ -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<Money> {
public MoneyDeserializer() {
this(null);
}
@ -127,6 +126,7 @@ public class TldYamlUtils {
/** A custom JSON deserializer for {@link CurrencyUnit}. */
public static class CurrencyDeserializer extends StdDeserializer<CurrencyUnit> {
public CurrencyDeserializer() {
this(null);
}

View file

@ -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();
}
}

View file

@ -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;

View file

@ -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<String> 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)));
}
}
}

View file

@ -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,

View file

@ -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);

View file

@ -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<GetTldCommand> {
@BeforeEach
void beforeEach() {
command.objectMapper = EntityYamlUtils.createObjectMapper();
}
@Test
void testSuccess() throws Exception {
createTld("xn--q9jyb4c");