GHUser even when event comes from Organization type
Describe the bug
A GHUser type is returned even when in the payload the account type is Organization.
More precisely in the event installation.created when the installation.account.type is set to Organization (app installed in organization) the installation.account in the @Installation.Created payload returns a GHUser.
{
"action": "created",
"installation": {
"id": 12345,
"account": {
"login": "org-name",
<...>
"type": "Organization",
"site_admin": false
},
<...>
fun onInstall(@Installation.Created installationPayload: GHEventPayload.Installation) {
val tenantGHOrganization = installationPayload.installation.account // <-- GHUser instance
}
Also tried to use the installationPayload.organization object but it always returned null (probably it's used in other events):
fun onInstall(@Installation.Created installationPayload: GHEventPayload.Installation) {
LOG.info("org: " + installationPayload.organization.name) // <-- Cannot invoke "org.kohsuke.github.GHOrganization.getName()" because the return value of "org.kohsuke.github.GHEventPayload$Installation.getOrganization()" is null
}
To Reproduce Steps to reproduce the behavior:
- Create an event with
installation.createdin an organization (install app in an organization) - Catch the event
- Retrieve the
installationPayload.installation.accountobject - Or retrieve the
installationPayload.organizationobject
Expected behavior
If the payload has the installation.account.type set to Organization the installationPayload.installation.accountshould return a GHOrganization object or the installationPayload.organization should not return null.
Desktop (please complete the following information):
- OS: macOS Monterey v12.4 (Apple M1)
Additional context Using Kotlin and Quarkus GitHub App
I checked the GHAppInstallation.java file and checked that the field is indeed a GHUser:
public class GHAppInstallation extends GHObject {
private GHUser account;
<...>
/**
* Gets account.
*
* @return the account
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHUser getAccount() {
return account;
}
As this is Jackson binding, I don't think we can do a simple if statement. Probably change the account to GHPerson and add the code to change to GHUser or GHOrganization later?
Probably a similar problem to https://github.com/hub4j/github-api/issues/126 .
As this is Jackson binding, I don't think we can do a simple if statement.
We don't want to deserialize aa GHPerson because we'd end up losing data - GHUser and GHIOrganization have fields not present on GHPerson.
Jackson has functionality to allow you to switch types based on a field, but I've never used it. The SO answer below has an example of how to do this.
https://stackoverflow.com/questions/30362446/deserialize-json-with-jackson-into-polymorphic-types-a-complete-example-is-giv
You're completely right. Hmm I may have a look at it. Thanks @bitwiseman!