Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Fixes to allow gson to correctly serialise constants and enums #72

Merged
merged 2 commits into from
Oct 24, 2024

Conversation

eamansour
Copy link
Member

Why?

Related to findings as part of galasa-dev/projectmanagement#1467

Currently, generated Java bean classes declare constants as static fields, but gson doesn't serialise static fields, so the resulting JSON doesn't include these constants.

When converting an enum value to JSON, it doesn't use the enum's human-readable string values and instead, uses the enum's constant value as it is declared.

Changes

  • Changed declaration of constants to non-static fields (i.e. public final String MY_CONSTANT = "ConstantValue") so that gson can serialise such fields
  • Added @SerializedName annotations to generated enum values so that enums are correctly serialised into their string representations
    • For example, given an enum value USERNAME_PASSWORD("UsernamePassword") converting this value to JSON will be rendered as UsernamePassword instead of USERNAME_PASSWORD
  • Removed space in generated constructors (e.g. public MyBean () now becomes public MyBean())
  • Removed space after enum values (e.g. MY_VALUE ("MyValue") now becomes MY_VALUE("MyValue"))

Copy link

@aashir21 aashir21 left a comment

Choose a reason for hiding this comment

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

👍🏻

@eamansour eamansour merged commit 01cd188 into main Oct 24, 2024
6 checks passed
@eamansour eamansour deleted the iss1467-serialize-constants branch October 24, 2024 09:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants