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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field not read (is null when JSON-content is not) #615

Open
bmarwell opened this issue Oct 3, 2023 · 0 comments
Open

Field not read (is null when JSON-content is not) #615

bmarwell opened this issue Oct 3, 2023 · 0 comments
Labels
bug Something isn't working right

Comments

@bmarwell
Copy link
Contributor

bmarwell commented Oct 3, 2023

Describe the bug

A field used in an interface with @JsonbTypeInfo and Subtypes as well as a type adapter becomes null in implementing records.

To Reproduce

@JsonbTypeInfo(
    key = "reason",
    value = {
        @JsonbSubtype(alias = "mention", type = AtMentionNotification.class),
        @JsonbSubtype(alias = "follow", type = AtFollowNotification.class)
    })
public sealed interface AtNotification permits AtFollowNotification, AtMentionNotification {
    @JsonbTypeAdapter(AtNotificationReasonAdapter.class)
    @JsonbProperty("reason")
    AtNotificationReason reason();
}

Then the implementing record:


public record AtMentionNotification(
        @JsonbProperty("uri") String uri,
        @JsonbProperty("cid") String cid,
        @JsonbProperty("author") AtNotificationAuthor author,
        @JsonbTypeAdapter(AtNotificationReasonAdapter.class) @JsonbProperty("reason") AtNotificationReason reason,
        @JsonbProperty("record") AtPostNotificationRecord record,
        @JsonbProperty("indexedAt") Instant indexedAt,
        @JsonbProperty("isRead") boolean isRead)
        implements AtNotification {}

Adapter for Reason enum:

public class AtNotificationReasonAdapter implements JsonbAdapter<AtNotificationReason, String> {
    @Override
    public String adaptToJson(final AtNotificationReason obj) throws Exception {
        return obj.getReasonString();
    }

    @Override
    public AtNotificationReason adaptFromJson(final String obj) throws Exception {
        return AtNotificationReason.fromString(obj);
    }
}

Expected behavior

Field "reason" is set.

** Actual behaviour**

[ERROR] Failures: 
[ERROR]   AtNotificationResponseWrapperTest.can_deserialize:93 [List element at index 0] 
Expecting
  AtMentionNotification[uri=at://did:plc:n5o2wksggcs653t3seg5eu6b/app.bsky.feed.post/3kaqpcklzqg2x, cid=bafyreiejmowzo54ffnk4o5ebz3l2jixso6qnezx4h3ctygugp5sjv5z3hu, author=AtNotificationAuthor[did=did:plc:n5o2wksggcs653t3seg5eu6b, handle=bmarwell.de, displayName="", description=""., avatarUri=""], indexedAt=2023-10-02T08:10:24.445Z], reason=null, record=AtPostNotificationRecord[text="", type=POST, lang=null, createdAt=2023-10-02T05:47:58.002Z], indexedAt=2023-10-02T05:47:58.002Z, isRead=false]
to have a property or a field named "reason" with value
  "mention"
but value was:
  null
(static and synthetic fields are ignored)

System information:

  • OS: Linux
  • Java Version:17
  • Yasson Version: any 3.0.x

Additional context

Run ./mvnw test here:
https://github.com/bmarwell/social-metricbot/tree/yasson_bug

@bmarwell bmarwell added the bug Something isn't working right label Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working right
Projects
None yet
Development

No branches or pull requests

1 participant