mirror of
https://github.com/google/nomulus.git
synced 2025-08-23 01:32:34 +02:00
Refactor the way that the console BE parses POST bodies (#2113)
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.
This commit is contained in:
parent
655f05c58c
commit
9f551eb552
10 changed files with 69 additions and 80 deletions
|
@ -19,10 +19,6 @@ import static com.google.common.base.Preconditions.checkArgument;
|
|||
import com.google.common.collect.AbstractSequentialIterator;
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
import com.google.common.net.InetAddresses;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
@ -481,19 +477,10 @@ public class CidrAddressBlock implements Iterable<InetAddress>, Serializable {
|
|||
return getCidrString(ip, netmask);
|
||||
}
|
||||
|
||||
public static class CidrAddressBlockAdapter extends TypeAdapter<CidrAddressBlock> {
|
||||
public static class CidrAddressBlockAdapter extends StringBaseTypeAdapter<CidrAddressBlock> {
|
||||
@Override
|
||||
public CidrAddressBlock read(JsonReader reader) throws IOException {
|
||||
String stringValue = reader.nextString();
|
||||
if (stringValue.equals("null")) {
|
||||
return null;
|
||||
}
|
||||
protected CidrAddressBlock fromString(String stringValue) {
|
||||
return new CidrAddressBlock(stringValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter writer, CidrAddressBlock cidrAddressBlock) throws IOException {
|
||||
writer.value(cidrAddressBlock.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,28 +14,15 @@
|
|||
|
||||
package google.registry.util;
|
||||
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
|
||||
/** GSON type adapter for Joda {@link DateTime} objects. */
|
||||
public class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
||||
public class DateTimeTypeAdapter extends StringBaseTypeAdapter<DateTime> {
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, DateTime value) throws IOException {
|
||||
out.value(Objects.toString(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public DateTime read(JsonReader in) throws IOException {
|
||||
String stringValue = in.nextString();
|
||||
if (stringValue.equals("null")) {
|
||||
return null;
|
||||
}
|
||||
protected DateTime fromString(String stringValue) throws IOException {
|
||||
return ISODateTimeFormat.dateTime().withZoneUTC().parseDateTime(stringValue);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
// 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.util;
|
||||
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Abstract class for {@link TypeAdapter}s that can convert directly to/from strings. */
|
||||
public abstract class StringBaseTypeAdapter<T> extends TypeAdapter<T> {
|
||||
|
||||
@Override
|
||||
public T read(JsonReader reader) throws IOException {
|
||||
String stringValue = reader.nextString();
|
||||
if (stringValue.equals("null")) {
|
||||
return null;
|
||||
}
|
||||
return fromString(stringValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter writer, T t) throws IOException {
|
||||
writer.value(Objects.toString(t));
|
||||
}
|
||||
|
||||
protected abstract T fromString(String stringValue) throws IOException;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue