Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use toString for enum serialization #2220

Merged
merged 4 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- SentryOptions.setProfilingTracesIntervalMillis has been deprecated
- Added cpu architecture and default environment in profiles envelope ([#2207](https://github.com/getsentry/sentry-java/pull/2207))
- SentryOptions.setProfilingEnabled has been deprecated in favor of setProfilesSampleRate
- Use toString for enum serialization ([#2220](https://github.com/getsentry/sentry-java/pull/2220))

### Features

Expand Down
2 changes: 2 additions & 0 deletions sentry/src/main/java/io/sentry/JsonObjectSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void serialize(
serializeCollection(writer, logger, Arrays.asList((Object[]) object));
} else if (object instanceof Map) {
serializeMap(writer, logger, (Map<?, ?>) object);
} else if (object.getClass().isEnum()) {
writer.value(object.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHat's about object..name()? Does it return the same thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have to use reflection to invoke .name(). By default .toString() should be the same as .name(). It also allows overriding the value that is serialized.

} else {
try {
Object serializableObject = jsonReflectionObjectSerializer.serialize(object, logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public final class JsonReflectionObjectSerializer {
return object;
} else if (object instanceof String) {
return object;
} else if (object.getClass().isEnum()) {
return object.toString();
} else {
if (visiting.contains(object)) {
logger.log(SentryLevel.INFO, "Cyclic reference detected. Calling toString() on object.");
Expand Down
34 changes: 34 additions & 0 deletions sentry/src/test/java/io/sentry/JsonObjectSerializerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,38 @@ internal class JsonObjectSerializerTest {
verify(fixture.writer).endObject()
}

@Test
fun `serialize enum`() {
fixture.getSUT().serialize(fixture.writer, fixture.logger, DataCategory.Session)
verify(fixture.writer).value("Session")
}

@Test
fun `serialize list of enum`() {
fixture.getSUT().serialize(fixture.writer, fixture.logger, listOf(DataCategory.Session))
verify(fixture.writer).beginArray()
verify(fixture.writer).value("Session")
verify(fixture.writer).endArray()
}

@Test
fun `serialize map of enum`() {
fixture.getSUT().serialize(fixture.writer, fixture.logger, mapOf("key" to DataCategory.Transaction))
verify(fixture.writer).beginObject()
verify(fixture.writer).name("key")
verify(fixture.writer).value("Transaction")
verify(fixture.writer).endObject()
}

@Test
fun `serialize object with enum property`() {
fixture.getSUT().serialize(fixture.writer, fixture.logger, ClassWithEnumProperty(DataCategory.Attachment))
verify(fixture.writer).beginObject()
verify(fixture.writer).name("enumProperty")
verify(fixture.writer).value("Attachment")
verify(fixture.writer).endObject()
}

@Test
fun `serialize unknown object with data`() {
val objectWithPrimitiveFields = UnknownClassWithData(
Expand All @@ -139,3 +171,5 @@ internal class JsonObjectSerializerTest {
private val string: String
)
}

data class ClassWithEnumProperty(val enumProperty: DataCategory)
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ class JsonReflectionObjectSerializerTest {
verify(fixture.logger).log(SentryLevel.INFO, "Max depth exceeded. Calling toString() on object.")
}

@Test
fun `enum`() {
val actual = fixture.getSut().serialize(DataCategory.Error, fixture.logger)
assertEquals("Error", actual)
}

// Helper

class ClassWithPrimitiveFields(
Expand Down