Skip to content

Unable to use Segment with JDK 17 #396

@nidhi-nair

Description

@nidhi-nair

When trying to work with Segment analytics after upgrading to Spring Boot 3.0 + JDK 17, we're receiving the following error whenever our application attempts to publish an event:

com.google.gson.JsonIOException: Failed making field 'java.time.Instant#seconds' accessible; either change its visibility or write a custom TypeAdapter for its declaring type
Caused by: com.google.gson.JsonIOException: Failed making field 'java.time.Instant#seconds' accessible; either change its visibility or write a custom TypeAdapter for its declaring type
	at com.google.gson.internal.reflect.ReflectionHelper.makeAccessible(ReflectionHelper.java:23)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:203)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:112)
	at com.google.gson.Gson.getAdapter(Gson.java:531)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:137)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:211)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:112)
	at com.google.gson.Gson.getAdapter(Gson.java:531)
	at com.google.gson.Gson.getAdapter(Gson.java:629)
	at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:176)
	at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
	at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:207)
	at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:144)
	at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:157)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:287)
	at com.google.gson.Gson.toJson(Gson.java:786)
	at com.google.gson.Gson.toJson(Gson.java:756)
	at com.google.gson.Gson.toJson(Gson.java:711)
	at com.google.gson.Gson.toJson(Gson.java:691)
	at com.segment.analytics.internal.AnalyticsClient.messageSizeInBytes(AnalyticsClient.java:144)
	at com.segment.analytics.internal.AnalyticsClient.enqueue(AnalyticsClient.java:170)
	at com.segment.analytics.Analytics.enqueue(Analytics.java:75)

The Gson error itself seems to be in line with the JPMS changes introduced in project Jigsaw. As mentioned in this issue, the expectation is that the Gson object is provided with TypeAdapters that handle the logic for how the failing types need to be serialized/deserialized.

However, Segment's AnalyticsClient seems to have abstracted away the Gson object that is meant to handle serialization logic for events and we are unable to add Type Adapters to it. Is it possible to expose the creation of this Gson instance?

A workaround we're using in the meantime, that has also been suggested in the above issue, is to provide JVM with the following argument:

--add-opens java.base/java.time=ALL-UNNAMED

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions