File tree 2 files changed +18
-3
lines changed
2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -94,15 +94,19 @@ public byte[] parseBytes(byte[] serialized) {
94
94
* Simple metadata marshaller that encodes strings as is.
95
95
*
96
96
* <p>This should be used with ASCII strings that only contain the characters listed in the class
97
- * comment of {@link AsciiMarshaller}. Otherwise the output may be considered invalid and
98
- * discarded by the transport, or the call may fail .
97
+ * comment of {@link AsciiMarshaller}. Otherwise an {@link IllegalArgumentException} will be
98
+ * thrown .
99
99
*/
100
100
public static final AsciiMarshaller <String > ASCII_STRING_MARSHALLER =
101
101
new AsciiMarshaller <String >() {
102
102
103
103
@ Override
104
104
public String toAsciiString (String value ) {
105
- return value ;
105
+ checkArgument (
106
+ value .chars ().allMatch (c -> c >= 0x20 && c <= 0x7E ),
107
+ "String \" %s\" contains non-printable ASCII characters" ,
108
+ value );
109
+ return value .trim ();
106
110
}
107
111
108
112
@ Override
Original file line number Diff line number Diff line change @@ -478,6 +478,17 @@ public void createFromPartial() {
478
478
assertSame (anotherSalmon , h2 .get (KEY_IMMUTABLE ));
479
479
}
480
480
481
+ @ Test
482
+ public void failNonPrintableAsciiCharacters () {
483
+ String value = "José" ;
484
+
485
+ thrown .expect (IllegalArgumentException .class );
486
+ thrown .expectMessage ("String \" " + value + "\" contains non-printable ASCII characters" );
487
+
488
+ Metadata metadata = new Metadata ();
489
+ metadata .put (Metadata .Key .of ("test-non-printable" , Metadata .ASCII_STRING_MARSHALLER ), value );
490
+ }
491
+
481
492
private static final class Fish {
482
493
private String name ;
483
494
You can’t perform that action at this time.
0 commit comments