Class EventDataObjectDeserializer
StripeObject
and handle failure due to schema
incompatibility between the data object and the model classes. Event data object by default
corresponds to the schema at API version tied to your Stripe account at the event creation time.
This event version is in Event.getApiVersion()
. The model classes for deserialization,
however, corresponds to a specific version pinned to this library Stripe.API_VERSION
.
Thus, only data object with same API versions is guaranteed to deserialize safely.
To avoid this problem of API version mismatch, create a new webhook endpoint `api_versions`
corresponding to Stripe.API_VERSION
. For more information, see API reference
In practice, each API version update only affects specific set of classes, so event data
object for the unaffected classes can still be serialized successfully -- even when the API
versions do not match. (Although it is considered unsafe by the API version comparison.) In that
case, you can use deserializeUnsafe()
Old events from Event.retrieve(String)
or Event.list(Map)
will have immutable
API versions on them, and there is currently no support for rendering it at different API
versions. If you find failure from reading these events, consider defining your own custom EventDataObjectDeserializer.CompatibilityTransformer
to transform the raw JSON to one with schema compatible with this
current model classes.
En event integration from webhook may look like the example below. Assuming that you have the event api version matching this library, you should safely find deserialized object from the deserializer.
Event event = Webhook.constructEvent(payload, sigHeader, secret); EventDataObjectDeserializer dataObjectDeserializer = event.getDataObjectDeserializer(); if (dataObjectDeserializer.getObject().isPresent()) { StripeObject stripeObject = dataObjectDeserializer.getObject().get(); doSomething(stripeObject); } else { throw new IllegalStateException( String.format("Unable to deserialize event data object for %s", event)); }
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Definition of event data object JSON transformation to be compatible to API version of the library. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
Force deserialize raw JSON toStripeObject
.Deserialize JSON that has been processed byEventDataObjectDeserializer.CompatibilityTransformer.transform(JsonObject, String, String)
intoStripeObject
.boolean
Gets anOptional
of data event object.Get raw JSON string for the data object.int
hashCode()
-
Method Details
-
getObject
Gets anOptional
of data event object. When the optional is present, the deserializedStripeObject
preserves high data integrity because of correspondence between schema of the PI response and the model class (the underlying concrete class for abstractStripeObject
) schema. This is whenEvent.getApiVersion()
matchesStripe.API_VERSION
. Otherwise, the optional is empty.- Returns:
Optional
of stripe object when deserialization is safe.
-
getRawJson
Get raw JSON string for the data object. This is the same data available inEventDataObjectDeserializationException.getRawJson()
upon deserialization failure.- Returns:
- JSON string the event data object.
-
deserializeUnsafe
Force deserialize raw JSON toStripeObject
. The deserialized data is not guaranteed to fully represent the JSON. For example, events of new API version having fields that are not captured by current model class will be lost. Similarly, events of old API version having fields that should be translated into the new fields, like field rename, will be lost.Upon deserialization failure, consider making the JSON compatible to the current model classes and recover from failure with
deserializeUnsafeWith(CompatibilityTransformer)
.- Returns:
- Object with no guarantee on full representation of its original raw JSON response.
- Throws:
EventDataObjectDeserializationException
- exception that contains the message error and the raw JSON response of theStripeObject
to be deserialized.
-
deserializeUnsafeWith
public StripeObject deserializeUnsafeWith(EventDataObjectDeserializer.CompatibilityTransformer transformer) Deserialize JSON that has been processed byEventDataObjectDeserializer.CompatibilityTransformer.transform(JsonObject, String, String)
intoStripeObject
. This deserialization method should only be used to handle events with schema incompatible to model class schema of this library. ThrowsJsonParseException
when the transformed JSON remains incompatible with the model classes.- Returns:
- deserialized
StripeObject
from user-supplied compatible JSON.
-
equals
-
canEqual
-
hashCode
public int hashCode()
-