Skip to content

Commit 048bbf5

Browse files
dahyvuunjaydeluca
andauthored
Fix W3CBaggagePropagator to allow empty baggage values per W3C spec (#8468)
Co-authored-by: Jay DeLuca <jaydeluca4@gmail.com>
1 parent 4d974ba commit 048bbf5

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

api/all/src/main/java/io/opentelemetry/api/baggage/propagation/Element.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Element {
3131
}
3232

3333
private final BitSet excluded;
34+
private final boolean allowEmpty;
3435

3536
private boolean leadingSpace;
3637
private boolean readingValue;
@@ -40,20 +41,21 @@ class Element {
4041
@Nullable private String value;
4142

4243
static Element createKeyElement() {
43-
return new Element(EXCLUDED_KEY_CHARS);
44+
return new Element(EXCLUDED_KEY_CHARS, /* allowEmpty= */ false);
4445
}
4546

4647
static Element createValueElement() {
47-
return new Element(EXCLUDED_VALUE_CHARS);
48+
return new Element(EXCLUDED_VALUE_CHARS, /* allowEmpty= */ true);
4849
}
4950

5051
/**
5152
* Constructs element instance.
5253
*
5354
* @param excluded characters that are not allowed for this type of an element
5455
*/
55-
private Element(BitSet excluded) {
56+
private Element(BitSet excluded, boolean allowEmpty) {
5657
this.excluded = excluded;
58+
this.allowEmpty = allowEmpty;
5759
reset(0);
5860
}
5961

@@ -77,8 +79,11 @@ boolean tryTerminating(int index, String header) {
7779
if (this.trailingSpace) {
7880
setValue(header);
7981
return true;
82+
} else if (allowEmpty) {
83+
// no content: empty value is valid (e.g. "key=" or "key= "), empty key is not
84+
this.value = "";
85+
return true;
8086
} else {
81-
// leading spaces - no content, invalid
8287
return false;
8388
}
8489
}

api/all/src/test/java/io/opentelemetry/api/baggage/propagation/W3CBaggagePropagatorTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ void extract_value_empty() {
209209
Context result =
210210
propagator.extract(Context.root(), ImmutableMap.of("baggage", "key1="), getter);
211211

212-
assertThat(Baggage.fromContext(result)).isEqualTo(Baggage.empty());
212+
Baggage expectedBaggage = Baggage.builder().put("key1", "").build();
213+
assertThat(Baggage.fromContext(result)).isEqualTo(expectedBaggage);
213214
}
214215

215216
@Test
@@ -220,7 +221,9 @@ void extract_value_empty_withMetadata() {
220221
propagator.extract(
221222
Context.root(), ImmutableMap.of("baggage", "key1=;metakey=metaval"), getter);
222223

223-
assertThat(Baggage.fromContext(result)).isEqualTo(Baggage.empty());
224+
Baggage expectedBaggage =
225+
Baggage.builder().put("key1", "", BaggageEntryMetadata.create("metakey=metaval")).build();
226+
assertThat(Baggage.fromContext(result)).isEqualTo(expectedBaggage);
224227
}
225228

226229
@Test
@@ -230,7 +233,8 @@ void extract_value_onlySpaces() {
230233
Context result =
231234
propagator.extract(Context.root(), ImmutableMap.of("baggage", "key1= "), getter);
232235

233-
assertThat(Baggage.fromContext(result)).isEqualTo(Baggage.empty());
236+
Baggage expectedBaggage = Baggage.builder().put("key1", "").build();
237+
assertThat(Baggage.fromContext(result)).isEqualTo(expectedBaggage);
234238
}
235239

236240
@Test

0 commit comments

Comments
 (0)