Skip to content

Commit 5f73500

Browse files
committed
use web-safe base64 encoding for byte arrays
1 parent 6fab00d commit 5f73500

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

endpoints-framework/src/main/java/com/google/api/server/spi/ObjectMapperUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.api.server.spi.config.annotationreader.ApiAnnotationIntrospector;
1919
import com.google.api.server.spi.config.model.ApiSerializationConfig;
2020

21+
import com.fasterxml.jackson.core.Base64Variants;
2122
import com.fasterxml.jackson.core.JsonGenerator;
2223
import com.fasterxml.jackson.core.JsonParser;
2324
import com.fasterxml.jackson.databind.AnnotationIntrospector;
@@ -76,6 +77,7 @@ public static ObjectMapper createStandardObjectMapper(ApiSerializationConfig con
7677
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
7778
.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
7879
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
80+
.setBase64Variant(Base64Variants.MODIFIED_FOR_URL)
7981
.setSerializerFactory(
8082
BeanSerializerFactory.instance.withSerializerModifier(new DeepEmptyCheckingModifier()));
8183
AnnotationIntrospector pair = AnnotationIntrospector.pair(
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.api.server.spi;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
22+
import org.junit.Test;
23+
24+
/**
25+
* Tests for {@link ObjectMapperUtil}
26+
*/
27+
public class ObjectMapperUtilTest {
28+
@Test
29+
public void createStandardObjectMapper_base64Variant() throws Exception {
30+
byte[] bytes = new byte[] {(byte) 0xff, (byte) 0xef};
31+
ObjectMapper mapper = ObjectMapperUtil.createStandardObjectMapper();
32+
assertThat(mapper.writeValueAsString(bytes)).isEqualTo("\"_-8\"");
33+
assertThat(mapper.readValue("\"_-8\"", byte[].class)).isEqualTo(bytes);
34+
}
35+
}

0 commit comments

Comments
 (0)