Fix indention formatter for google.protobuf.Any (#16785)

`google.protobuf.Any` formatting with indentation was somewhat off.

Formatting an `Any` as root object:
```json
{"@type": "type.googleapis.com/protobuf_unittest3.ForeignMessage",
"c": 1
}
```
changes to
```json
{
  "@type": "type.googleapis.com/protobuf_unittest3.ForeignMessage",
  "c": 1
}
```

For messages were `Any` is in a nested field the change makes more of a visual impact.
The `c` field seems to be at the same level as the `anyField`, but it's nested so it should be indented:
```json
{
  "anyField": {"@type": "type.googleapis.com/protobuf_unittest3.ForeignMessage",
  "c": 1
  }
}
```
changes to:
```json
{
  "anyField": {
    "@type": "type.googleapis.com/protobuf_unittest3.ForeignMessage",
    "c": 1
  }
}
```

Closes #16785

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/16785 from q42jaap:main 72deed6ed7
PiperOrigin-RevId: 632506226
pull/16806/head
Jaap Taal 7 months ago committed by Copybara-Service
parent 4b7c2d4381
commit 91b7cf3325
  1. 36
      csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
  2. 5
      csharp/src/Google.Protobuf/JsonFormatter.cs

@ -900,6 +900,42 @@ namespace Google.Protobuf
AssertWriteValue(value, "[\n 1,\n 2,\n 3\n]", JsonFormatter.Settings.Default.WithIndentation()); AssertWriteValue(value, "[\n 1,\n 2,\n 3\n]", JsonFormatter.Settings.Default.WithIndentation());
} }
[Test]
public void WriteValueWithIndentation_Any()
{
var registry = TypeRegistry.FromMessages(ForeignMessage.Descriptor);
var formatter = JsonFormatter.Settings.Default.WithIndentation().WithTypeRegistry(registry);
var nestedMessage = new ForeignMessage { C = 1 };
var value = Any.Pack(nestedMessage);
const string expectedJson = @"
{
'@type': 'type.googleapis.com/protobuf_unittest3.ForeignMessage',
'c': 1
}";
AssertWriteValue(value, expectedJson, formatter);
}
[Test]
public void WriteValueWithIndentation_NestedAny()
{
var registry = TypeRegistry.FromMessages(ForeignMessage.Descriptor);
var formatter = JsonFormatter.Settings.Default.WithIndentation().WithTypeRegistry(registry);
var nestedMessage = new ForeignMessage { C = 1 };
var value = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) };
const string expectedJson = @"
{
'anyField': {
'@type': 'type.googleapis.com/protobuf_unittest3.ForeignMessage',
'c': 1
}
}";
AssertWriteValue(value, expectedJson, formatter);
}
[Test] [Test]
public void WriteValueWithIndentation_CustomIndentation() public void WriteValueWithIndentation_CustomIndentation()
{ {

@ -481,6 +481,7 @@ namespace Google.Protobuf {
} }
IMessage message = descriptor.Parser.ParseFrom(data); IMessage message = descriptor.Parser.ParseFrom(data);
WriteBracketOpen(writer, ObjectOpenBracket); WriteBracketOpen(writer, ObjectOpenBracket);
MaybeWriteValueWhitespace(writer, indentationLevel + 1);
WriteString(writer, AnyTypeUrlField); WriteString(writer, AnyTypeUrlField);
writer.Write(NameValueSeparator); writer.Write(NameValueSeparator);
WriteString(writer, typeUrl); WriteString(writer, typeUrl);
@ -489,9 +490,9 @@ namespace Google.Protobuf {
writer.Write(ValueSeparator); writer.Write(ValueSeparator);
WriteString(writer, AnyWellKnownTypeValueField); WriteString(writer, AnyWellKnownTypeValueField);
writer.Write(NameValueSeparator); writer.Write(NameValueSeparator);
WriteWellKnownTypeValue(writer, descriptor, message, indentationLevel); WriteWellKnownTypeValue(writer, descriptor, message, indentationLevel + 1);
} else { } else {
WriteMessageFields(writer, message, true, indentationLevel); WriteMessageFields(writer, message, true, indentationLevel + 1);
} }
WriteBracketClose(writer, ObjectCloseBracket, true, indentationLevel); WriteBracketClose(writer, ObjectCloseBracket, true, indentationLevel);
} }

Loading…
Cancel
Save