New unittest case to validate Java parseJson behavior with 1-element arrays.

In Java, it seems like the existing behavior is that we allow
single-element arrays to be parsed into a non-repeated field.

In `public final class JsonArray extends JsonElement implements Iterable<JsonElement>`:
```
  @Override
  public String getAsString() {
    return getAsSingleElement().getAsString();
  }
```

TBH I don't know if this behavior is necessarily _desired_, but here is
a unit test to validate this behavior so that we don't accidentally
break it without intending to.

PiperOrigin-RevId: 947320631
This commit is contained in:
Tony Liao 2026-07-13 17:01:48 -07:00 committed by Copybara-Service
parent 9b0d074ea6
commit e5cfe139bc

View file

@ -1963,6 +1963,16 @@ public class JsonFormatTest {
assertThat(actualBuilder.build()).isEqualTo(expected);
}
// TODO: b/534418787 - Investigate this behavior further, especially in conformance tests.
@Test
public void testParserSingleElementArrayForNonRepeatedEnum() throws Exception {
TestAllTypes.Builder actualBuilder = TestAllTypes.newBuilder();
mergeFromJson("{\"optionalNestedEnum\": [\"FOO\"]}", actualBuilder);
TestAllTypes expected = TestAllTypes.newBuilder().setOptionalNestedEnum(NestedEnum.FOO).build();
assertThat(actualBuilder.build()).isEqualTo(expected);
}
@Test
public void testCustomJsonName() throws Exception {
TestCustomJsonName message = TestCustomJsonName.newBuilder().setValue(12345).build();