Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions core/src/main/java/io/grpc/internal/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public static Double getNumberAsDouble(Map<String, ?> obj, String key) {
return null;
}
Object value = obj.get(key);
if (value instanceof Double) {
return (Double) value;
if (value instanceof Number) {
return ((Number) value).doubleValue();
}
if (value instanceof String) {
try {
Expand All @@ -132,8 +132,8 @@ public static Float getNumberAsFloat(Map<String, ?> obj, String key) {
return null;
}
Object value = obj.get(key);
if (value instanceof Float) {
return (Float) value;
if (value instanceof Number) {
return ((Number) value).floatValue();
}
if (value instanceof String) {
try {
Expand All @@ -159,11 +159,12 @@ public static Integer getNumberAsInteger(Map<String, ?> obj, String key) {
return null;
}
Object value = obj.get(key);
if (value instanceof Double) {
Double d = (Double) value;
int i = d.intValue();
if (value instanceof Number) {
Number n = (Number) value;
double d = n.doubleValue();
int i = n.intValue();
if (i != d) {
throw new ClassCastException("Number expected to be integer: " + d);
throw new ClassCastException("Number expected to be integer: " + n);
}
return i;
}
Expand All @@ -190,11 +191,12 @@ public static Long getNumberAsLong(Map<String, ?> obj, String key) {
return null;
}
Object value = obj.get(key);
if (value instanceof Double) {
Double d = (Double) value;
long l = d.longValue();
if (value instanceof Number) {
Number n = (Number) value;
double d = n.doubleValue();
long l = n.longValue();
if (l != d) {
throw new ClassCastException("Number expected to be long: " + d);
throw new ClassCastException("Number expected to be long: " + n);
}
return l;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public ManagedChannelImplBuilder defaultServiceConfig(@Nullable Map<String, ?> s
parsedMap.put(key, checkListEntryTypes((List<?>) value));
} else if (value instanceof String) {
parsedMap.put(key, value);
} else if (value instanceof Double) {
} else if (value instanceof Number) {
parsedMap.put(key, value);
} else if (value instanceof Boolean) {
parsedMap.put(key, value);
Expand All @@ -606,7 +606,7 @@ private static List<?> checkListEntryTypes(List<?> list) {
parsedList.add(checkListEntryTypes((List<?>) value));
} else if (value instanceof String) {
parsedList.add(value);
} else if (value instanceof Double) {
} else if (value instanceof Number) {
parsedList.add(value);
} else if (value instanceof Boolean) {
parsedList.add(value);
Expand Down
104 changes: 104 additions & 0 deletions core/src/test/java/io/grpc/internal/ServiceConfigNumericTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2026, gRPC 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 io.grpc.internal;

import static org.junit.Assert.assertNotNull;

import io.grpc.internal.ManagedChannelImplBuilder.FixedPortProvider;
import io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ServiceConfigNumericTypeTest {

@Test
public void defaultServiceConfig_acceptsInteger() {
Map<String, Object> retryPolicy = new HashMap<>();
retryPolicy.put("maxAttempts", 4);
retryPolicy.put("initialBackoff", "0.1s");
retryPolicy.put("maxBackoff", "1s");
retryPolicy.put("backoffMultiplier", 2);
retryPolicy.put("retryableStatusCodes", Collections.singletonList("UNAVAILABLE"));

Map<String, Object> methodConfig = new HashMap<>();
methodConfig.put("name", Collections.singletonList(Collections.emptyMap()));
methodConfig.put("retryPolicy", retryPolicy);

Map<String, Object> serviceConfig = new HashMap<>();
serviceConfig.put("methodConfig", Collections.singletonList(methodConfig));

ManagedChannelImplBuilder builder = new ManagedChannelImplBuilder(
"localhost",
new UnsupportedClientTransportFactoryBuilder(),
new FixedPortProvider(443));
builder.defaultServiceConfig(serviceConfig);
assertNotNull(builder);
}

@Test
public void defaultServiceConfig_acceptsDouble() {
Map<String, Object> retryPolicy = new HashMap<>();
retryPolicy.put("maxAttempts", 4.0);
retryPolicy.put("initialBackoff", "0.1s");
retryPolicy.put("maxBackoff", "1s");
retryPolicy.put("backoffMultiplier", 2.0);
retryPolicy.put("retryableStatusCodes", Collections.singletonList("UNAVAILABLE"));

Map<String, Object> methodConfig = new HashMap<>();
methodConfig.put("name", Collections.singletonList(Collections.emptyMap()));
methodConfig.put("retryPolicy", retryPolicy);

Map<String, Object> serviceConfig = new HashMap<>();
serviceConfig.put("methodConfig", Collections.singletonList(methodConfig));

ManagedChannelImplBuilder builder = new ManagedChannelImplBuilder(
"localhost",
new UnsupportedClientTransportFactoryBuilder(),
new FixedPortProvider(443));
builder.defaultServiceConfig(serviceConfig);
assertNotNull(builder);
}

@Test
public void defaultServiceConfig_acceptsLong() {
Map<String, Object> retryPolicy = new HashMap<>();
retryPolicy.put("maxAttempts", 4L);
retryPolicy.put("initialBackoff", "0.1s");
retryPolicy.put("maxBackoff", "1s");
retryPolicy.put("backoffMultiplier", 2L);
retryPolicy.put("retryableStatusCodes", Collections.singletonList("UNAVAILABLE"));

Map<String, Object> methodConfig = new HashMap<>();
methodConfig.put("name", Collections.singletonList(Collections.emptyMap()));
methodConfig.put("retryPolicy", retryPolicy);

Map<String, Object> serviceConfig = new HashMap<>();
serviceConfig.put("methodConfig", Collections.singletonList(methodConfig));

ManagedChannelImplBuilder builder = new ManagedChannelImplBuilder(
"localhost",
new UnsupportedClientTransportFactoryBuilder(),
new FixedPortProvider(443));
builder.defaultServiceConfig(serviceConfig);
assertNotNull(builder);
}
}
Loading