Skip to content
This repository was archived by the owner on Apr 19, 2026. It is now read-only.

Commit 3b187da

Browse files
authored
Ignore unknown fields when parsing service config (#69) (#70)
1 parent 308f522 commit 3b187da

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

endpoints-service-config/src/main/java/com/google/api/config/ServiceConfigSupplier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private String fetchLatestServiceVersion(String serviceName) {
191191
private static Service parseHttpResponse(HttpResponse httpResponse) {
192192
try {
193193
Builder builder = Service.newBuilder();
194-
JsonFormat.parser().merge(httpResponse.parseAsString(), builder);
194+
JsonFormat.parser().ignoringUnknownFields().merge(httpResponse.parseAsString(), builder);
195195
return builder.build();
196196
} catch (IOException exception) {
197197
throw new ServiceConfigException(

endpoints-service-config/src/test/java/com/google/api/config/ServiceConfigSupplierTest.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,20 @@
3333
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
3434
import com.google.protobuf.InvalidProtocolBufferException;
3535
import com.google.protobuf.util.JsonFormat;
36-
37-
import org.junit.Before;
38-
import org.junit.Test;
39-
4036
import java.io.IOException;
4137
import java.util.LinkedList;
38+
import org.junit.Before;
39+
import org.junit.Test;
4240

4341
/**
4442
* Tests for {@link ServiceConfigSupplier}.
4543
*/
4644
public final class ServiceConfigSupplierTest {
47-
4845
private static final String SERVICE_NAME = "test-service-name";
4946
private static final String SERVICE_VERSION = "test-service-version";
5047

51-
private static final Service SERVICE = Service.newBuilder()
52-
.setName(SERVICE_NAME)
53-
.setId(SERVICE_VERSION)
54-
.build();
48+
private static final Service SERVICE =
49+
Service.newBuilder().setName(SERVICE_NAME).setId(SERVICE_VERSION).build();
5550

5651
private final Environment mockEnvironment = mock(Environment.class);
5752
private final GoogleCredential mockCredential = mock(GoogleCredential.class);
@@ -72,8 +67,7 @@ public void testServiceNameNotSet() throws IOException {
7267
fail();
7368
} catch (IllegalArgumentException exception) {
7469
assertEquals(
75-
"Environment variable 'ENDPOINTS_SERVICE_NAME' is not set",
76-
exception.getMessage());
70+
"Environment variable 'ENDPOINTS_SERVICE_NAME' is not set", exception.getMessage());
7771
}
7872
}
7973

@@ -94,13 +88,24 @@ public void testServiceVersionNotSet() throws IOException {
9488
public void testFetchSuccessfully() throws InvalidProtocolBufferException {
9589
when(mockEnvironment.getVariable("ENDPOINTS_SERVICE_NAME")).thenReturn(SERVICE_NAME);
9690
when(mockEnvironment.getVariable("ENDPOINTS_SERVICE_VERSION")).thenReturn(SERVICE_VERSION);
97-
9891
String content = JsonFormat.printer().print(SERVICE);
9992
testHttpTransport.addResponse(200, content);
10093

10194
assertEquals(SERVICE, fetcher.get());
10295
}
10396

97+
@Test
98+
public void testFetchWithUnkownFields() throws InvalidProtocolBufferException {
99+
when(mockEnvironment.getVariable("ENDPOINTS_SERVICE_NAME")).thenReturn(SERVICE_NAME);
100+
when(mockEnvironment.getVariable("ENDPOINTS_SERVICE_VERSION")).thenReturn(SERVICE_VERSION);
101+
102+
String unknownField = ",\"unknown_key\":\"unknown_value\"\n";
103+
String contentBase = JsonFormat.printer().print(SERVICE);
104+
String content = contentBase.substring(0,contentBase.length()-1) + unknownField + contentBase.charAt(contentBase.length()-1);
105+
testHttpTransport.addResponse(200, content);
106+
assertEquals(SERVICE, fetcher.get());
107+
}
108+
104109
@Test
105110
public void testFetchConfigWithWrongServiceName() throws InvalidProtocolBufferException {
106111
when(mockEnvironment.getVariable("ENDPOINTS_SERVICE_NAME")).thenReturn(SERVICE_NAME);
@@ -124,10 +129,7 @@ public void testFetchConfigWithWrongServiceVersion() throws InvalidProtocolBuffe
124129
when(mockEnvironment.getVariable("ENDPOINTS_SERVICE_NAME")).thenReturn(SERVICE_NAME);
125130
when(mockEnvironment.getVariable("ENDPOINTS_SERVICE_VERSION")).thenReturn(SERVICE_VERSION);
126131

127-
Service service = Service.newBuilder()
128-
.setName(SERVICE_NAME)
129-
.setId("random-version")
130-
.build();
132+
Service service = Service.newBuilder().setName(SERVICE_NAME).setId("random-version").build();
131133
String content = JsonFormat.printer().print(service);
132134
testHttpTransport.addResponse(200, content);
133135

0 commit comments

Comments
 (0)