mirror of
https://github.com/google/nomulus.git
synced 2025-07-25 20:18: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
|
@ -12,12 +12,13 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
import { Injectable } from '@angular/core';
|
|
||||||
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
||||||
import { Observable, catchError, of } from 'rxjs';
|
import { Injectable } from '@angular/core';
|
||||||
import { Contact } from '../../settings/contact/contact.service';
|
import { catchError, Observable, of } from 'rxjs';
|
||||||
import { SecuritySettingsBackendModel } from 'src/app/settings/security/security.service';
|
import { SecuritySettingsBackendModel } from 'src/app/settings/security/security.service';
|
||||||
|
|
||||||
|
import { Contact } from '../../settings/contact/contact.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BackendService {
|
export class BackendService {
|
||||||
constructor(private http: HttpClient) {}
|
constructor(private http: HttpClient) {}
|
||||||
|
@ -55,7 +56,7 @@ export class BackendService {
|
||||||
): Observable<Contact[]> {
|
): Observable<Contact[]> {
|
||||||
return this.http.post<Contact[]>(
|
return this.http.post<Contact[]>(
|
||||||
`/console-api/settings/contacts?registrarId=${registrarId}`,
|
`/console-api/settings/contacts?registrarId=${registrarId}`,
|
||||||
{ contacts }
|
contacts
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +86,7 @@ export class BackendService {
|
||||||
): Observable<SecuritySettingsBackendModel> {
|
): Observable<SecuritySettingsBackendModel> {
|
||||||
return this.http.post<SecuritySettingsBackendModel>(
|
return this.http.post<SecuritySettingsBackendModel>(
|
||||||
`/console-api/settings/security?registrarId=${registrarId}`,
|
`/console-api/settings/security?registrarId=${registrarId}`,
|
||||||
{ registrar: securitySettings }
|
securitySettings
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,26 +14,17 @@
|
||||||
|
|
||||||
package google.registry.model.adapters;
|
package google.registry.model.adapters;
|
||||||
|
|
||||||
import com.google.gson.TypeAdapter;
|
|
||||||
import com.google.gson.stream.JsonReader;
|
|
||||||
import com.google.gson.stream.JsonWriter;
|
|
||||||
import google.registry.model.adapters.CurrencyUnitAdapter.UnknownCurrencyException;
|
import google.registry.model.adapters.CurrencyUnitAdapter.UnknownCurrencyException;
|
||||||
|
import google.registry.util.StringBaseTypeAdapter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import org.joda.money.CurrencyUnit;
|
import org.joda.money.CurrencyUnit;
|
||||||
|
|
||||||
public class CurrencyJsonAdapter extends TypeAdapter<CurrencyUnit> {
|
public class CurrencyJsonAdapter extends StringBaseTypeAdapter<CurrencyUnit> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, CurrencyUnit value) throws IOException {
|
protected CurrencyUnit fromString(String stringValue) throws IOException {
|
||||||
String currency = CurrencyUnitAdapter.convertFromCurrency(value);
|
|
||||||
out.value(currency);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CurrencyUnit read(JsonReader in) throws IOException {
|
|
||||||
String currency = in.nextString();
|
|
||||||
try {
|
try {
|
||||||
return CurrencyUnitAdapter.convertFromString(currency);
|
return CurrencyUnitAdapter.convertFromString(stringValue);
|
||||||
} catch (UnknownCurrencyException e) {
|
} catch (UnknownCurrencyException e) {
|
||||||
throw new IOException("Unknown currency");
|
throw new IOException("Unknown currency");
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ import com.google.common.io.CharStreams;
|
||||||
import com.google.common.net.MediaType;
|
import com.google.common.net.MediaType;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
import dagger.Provides;
|
import dagger.Provides;
|
||||||
|
@ -263,11 +263,10 @@ public final class RequestModule {
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@OptionalJsonPayload
|
@OptionalJsonPayload
|
||||||
public static Optional<JsonObject> provideJsonBody(HttpServletRequest req, Gson gson) {
|
public static Optional<JsonElement> provideJsonBody(HttpServletRequest req, Gson gson) {
|
||||||
try {
|
try {
|
||||||
JsonObject body = gson.fromJson(req.getReader(), JsonObject.class);
|
return Optional.of(gson.fromJson(req.getReader(), JsonElement.class));
|
||||||
return Optional.of(body);
|
} catch (IOException e) {
|
||||||
} catch (Exception e) {
|
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ import static google.registry.request.RequestParameters.extractRequiredParameter
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonElement;
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
import dagger.Provides;
|
import dagger.Provides;
|
||||||
import google.registry.model.registrar.Registrar;
|
import google.registry.model.registrar.Registrar;
|
||||||
|
@ -172,15 +172,8 @@ public final class RegistrarConsoleModule {
|
||||||
@Provides
|
@Provides
|
||||||
@Parameter("contacts")
|
@Parameter("contacts")
|
||||||
public static Optional<ImmutableSet<RegistrarPoc>> provideContacts(
|
public static Optional<ImmutableSet<RegistrarPoc>> provideContacts(
|
||||||
Gson gson, @OptionalJsonPayload Optional<JsonObject> payload) {
|
Gson gson, @OptionalJsonPayload Optional<JsonElement> payload) {
|
||||||
|
return payload.map(s -> ImmutableSet.copyOf(gson.fromJson(s, RegistrarPoc[].class)));
|
||||||
if (payload.isPresent() && payload.get().has("contacts")) {
|
|
||||||
return Optional.of(
|
|
||||||
ImmutableSet.copyOf(
|
|
||||||
gson.fromJson(payload.get().get("contacts").getAsJsonArray(), RegistrarPoc[].class)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
|
@ -192,11 +185,7 @@ public final class RegistrarConsoleModule {
|
||||||
@Provides
|
@Provides
|
||||||
@Parameter("registrar")
|
@Parameter("registrar")
|
||||||
public static Optional<Registrar> provideRegistrar(
|
public static Optional<Registrar> provideRegistrar(
|
||||||
Gson gson, @OptionalJsonPayload Optional<JsonObject> payload) {
|
Gson gson, @OptionalJsonPayload Optional<JsonElement> payload) {
|
||||||
if (payload.isPresent() && payload.get().has("registrar")) {
|
return payload.map(s -> gson.fromJson(s, Registrar.class));
|
||||||
return Optional.of(gson.fromJson(payload.get().get("registrar"), Registrar.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -251,9 +251,7 @@ class RegistrarsActionTest {
|
||||||
passcodeGenerator);
|
passcodeGenerator);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
doReturn(
|
doReturn(new BufferedReader(new StringReader(registrarParamMap.toString())))
|
||||||
new BufferedReader(
|
|
||||||
new StringReader("{\"registrar\":" + registrarParamMap.toString() + "}")))
|
|
||||||
.when(request)
|
.when(request)
|
||||||
.getReader();
|
.getReader();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -238,8 +238,7 @@ class ContactActionTest {
|
||||||
if (method.equals(Action.Method.GET)) {
|
if (method.equals(Action.Method.GET)) {
|
||||||
return new ContactAction(request, authResult, response, GSON, registrarId, Optional.empty());
|
return new ContactAction(request, authResult, response, GSON, registrarId, Optional.empty());
|
||||||
} else {
|
} else {
|
||||||
when(request.getReader())
|
when(request.getReader()).thenReturn(new BufferedReader(new StringReader(contacts)));
|
||||||
.thenReturn(new BufferedReader(new StringReader("{\"contacts\":" + contacts + "}")));
|
|
||||||
Optional<ImmutableSet<RegistrarPoc>> maybeContacts =
|
Optional<ImmutableSet<RegistrarPoc>> maybeContacts =
|
||||||
RegistrarConsoleModule.provideContacts(
|
RegistrarConsoleModule.provideContacts(
|
||||||
GSON, RequestModule.provideJsonBody(request, GSON));
|
GSON, RequestModule.provideJsonBody(request, GSON));
|
||||||
|
|
|
@ -116,12 +116,9 @@ class SecurityActionTest {
|
||||||
|
|
||||||
private SecurityAction createAction(AuthResult authResult, String registrarId)
|
private SecurityAction createAction(AuthResult authResult, String registrarId)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
doReturn(new BufferedReader(new StringReader("{\"registrar\":" + jsonRegistrar1 + "}")))
|
doReturn(new BufferedReader(new StringReader(jsonRegistrar1))).when(request).getReader();
|
||||||
.when(request)
|
Optional<Registrar> maybeRegistrar =
|
||||||
.getReader();
|
RegistrarConsoleModule.provideRegistrar(GSON, RequestModule.provideJsonBody(request, GSON));
|
||||||
Optional<Registrar> maybeRegistrar =
|
|
||||||
RegistrarConsoleModule.provideRegistrar(
|
|
||||||
GSON, RequestModule.provideJsonBody(request, GSON));
|
|
||||||
return new SecurityAction(
|
return new SecurityAction(
|
||||||
authResult,
|
authResult,
|
||||||
response,
|
response,
|
||||||
|
|
|
@ -19,10 +19,6 @@ import static com.google.common.base.Preconditions.checkArgument;
|
||||||
import com.google.common.collect.AbstractSequentialIterator;
|
import com.google.common.collect.AbstractSequentialIterator;
|
||||||
import com.google.common.flogger.FluentLogger;
|
import com.google.common.flogger.FluentLogger;
|
||||||
import com.google.common.net.InetAddresses;
|
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.io.Serializable;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
@ -481,19 +477,10 @@ public class CidrAddressBlock implements Iterable<InetAddress>, Serializable {
|
||||||
return getCidrString(ip, netmask);
|
return getCidrString(ip, netmask);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class CidrAddressBlockAdapter extends TypeAdapter<CidrAddressBlock> {
|
public static class CidrAddressBlockAdapter extends StringBaseTypeAdapter<CidrAddressBlock> {
|
||||||
@Override
|
@Override
|
||||||
public CidrAddressBlock read(JsonReader reader) throws IOException {
|
protected CidrAddressBlock fromString(String stringValue) {
|
||||||
String stringValue = reader.nextString();
|
|
||||||
if (stringValue.equals("null")) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new CidrAddressBlock(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;
|
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.io.IOException;
|
||||||
import java.util.Objects;
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.joda.time.format.ISODateTimeFormat;
|
import org.joda.time.format.ISODateTimeFormat;
|
||||||
|
|
||||||
/** GSON type adapter for Joda {@link DateTime} objects. */
|
/** GSON type adapter for Joda {@link DateTime} objects. */
|
||||||
public class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
|
public class DateTimeTypeAdapter extends StringBaseTypeAdapter<DateTime> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, DateTime value) throws IOException {
|
protected DateTime fromString(String stringValue) 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;
|
|
||||||
}
|
|
||||||
return ISODateTimeFormat.dateTime().withZoneUTC().parseDateTime(stringValue);
|
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