Skip to content

Commit

Permalink
Prevent deserialization failure on unknown enums
Browse files Browse the repository at this point in the history
  • Loading branch information
sgdesmet committed Jan 2, 2024
1 parent dcd64ea commit 180e4b9
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.whatsapp.api.domain.messages.type;
//TODO: implementar mais tipos de mensagens. Não está completo.

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.fasterxml.jackson.annotation.JsonValue;

/**
Expand Down Expand Up @@ -78,7 +79,7 @@ public enum MessageType {
/**
* Unknown message type.
*/
UNKNOWN("unknown"), //
@JsonEnumDefaultValue UNKNOWN( "unknown"), //
/**
* Video message type.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/whatsapp/api/domain/webhook/Change.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.whatsapp.api.domain.webhook;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.whatsapp.api.domain.webhook.type.FieldType;

import static com.fasterxml.jackson.annotation.JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE;

/**
* Changes that triggered the Webhooks call.
*
* @param field A value object. Contains details of the changes related to the specified field.
* @param value Contains the type of notification you are getting on that Webhook. Currently, the only option for this API is “messages”.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonFormat(with = READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
public record Change(
/*
Contains the type of notification you are getting on that Webhook. Currently, the only option for this API is “messages”.
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/whatsapp/api/domain/webhook/Message.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.whatsapp.api.domain.webhook;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.whatsapp.api.domain.messages.type.MessageType;

import java.util.List;

import static com.fasterxml.jackson.annotation.JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE;

/**
* The type Message.
*
Expand All @@ -31,6 +34,7 @@
* @param document A media object with the document information. Added to Webhook if type is document. See {@link Document}
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonFormat(with = READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
public record Message(

@JsonProperty("reaction") Reaction reaction,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.whatsapp.api.domain.webhook;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.whatsapp.api.domain.webhook.type.RestrictionType;

import static com.fasterxml.jackson.annotation.JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE;

/**
* The type Restriction info.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonFormat(with = READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
public record RestrictionInfo(

@JsonProperty("restriction_type") RestrictionType restrictionType,
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/whatsapp/api/domain/webhook/Status.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.whatsapp.api.domain.webhook;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.whatsapp.api.domain.webhook.type.MessageStatus;

import java.util.List;

import static com.fasterxml.jackson.annotation.JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE;

/**
* The type Status.
*
Expand All @@ -18,6 +21,7 @@
* @param errors The errors object in webhooks triggered by v16.0+ request errors now include message and error_data.details properties, and title values have changed for multiple error codes.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonFormat(with = READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
public record Status(

@JsonProperty("id") String id,
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/whatsapp/api/domain/webhook/Value.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.whatsapp.api.domain.webhook;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.whatsapp.api.domain.webhook.type.EventType;

import java.util.List;

import static com.fasterxml.jackson.annotation.JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE;

/**
* The type Value.
*
Expand All @@ -26,6 +29,7 @@
* @param requestedVerifiedName This field displays the name that was sent to be verified.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonFormat(with = READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
public record Value(

@JsonProperty("metadata") Metadata metadata,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.whatsapp.api.domain.webhook.type;

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;

/**
* Used when an event happened in a specific WABA.
* See <a href="https://developers.facebook.com/docs/whatsapp/business-management-api/webhooks/components">Webhooks Components</a>
Expand Down Expand Up @@ -81,6 +83,12 @@ public enum EventType {
/**
* Pin changed event type.
*/
PIN_CHANGED
PIN_CHANGED,

/**
* Fallback value.
*/
@JsonEnumDefaultValue
UNKNOWN;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.whatsapp.api.domain.webhook.type;

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.fasterxml.jackson.annotation.JsonValue;


Expand Down Expand Up @@ -38,7 +39,13 @@ public enum FieldType {
* <li>the two-step verification code is updated</li>
* </ul>
*/
security("security");
SECURITY( "security"),

/**
* Fallback value.
*/
@JsonEnumDefaultValue
UNKNOWN( "unknown");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.whatsapp.api.domain.webhook.type;


import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.fasterxml.jackson.annotation.JsonValue;

/**
Expand All @@ -21,13 +22,18 @@ public enum MessageStatus {
READ("read"),

/**
* Message failed to send.(Red error triangle)
* Message failed to send. (Red error triangle)
*/
FAILED("failed"),
/**
* Message deleted by the user. ( Message is replaced in WhatsApp mobile with the note "This message was deleted".)
* Message deleted by the user. (Message is replaced in WhatsApp mobile with the note "This message was deleted".)
*/
DELETED("deleted");
DELETED("deleted"),

/**
* Fallback value.
*/
@JsonEnumDefaultValue UNKNOWN( "unknown");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.whatsapp.api.domain.webhook.type;

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;

/**
* The enum Restriction type.
*/
Expand All @@ -15,5 +17,11 @@ public enum RestrictionType {
/**
* Restricted customer initiated messaging restriction type.
*/
RESTRICTED_CUSTOMER_INITIATED_MESSAGING
RESTRICTED_CUSTOMER_INITIATED_MESSAGING,

/**
* Fallback value.
*/
@JsonEnumDefaultValue
UNKNOWN;
}

0 comments on commit 180e4b9

Please sign in to comment.