Supply Pretty Name for API Enumeration Type

I'd like to provide a pretty name for an enumeration used in my extension point config.

Overriding toString will show my pretty name in the UI and allow for selection + save, but the dropdown box doesn't load the current value correctly. I think this is because the list of options is derived from the string values which are different than the values stored in the config.json. For example, selecting SHA-256 will store SHA256 into the config.json.

Should I be doing something smarter with my Enumeration.Provider?

    @FormCategoryKey("Snmp.config.category.Authentication")
    @Label("Authentication Protocol *")
    @DescriptionKey("Snmp.config.Authentication.AuthProtocol.Description")
    @FormField(FormFieldType.SELECT)
    @Enumeration(AuthenticationProtocol.Provider::class)
    @Required
    val protocol: AuthenticationProtocol,
enum class AuthenticationProtocol(val mappedProtocol: AuthGeneric?, val prettyName: String) {
    NONE(null, "None"),
    MD5(AuthMD5(), "MD-5"),
    SHA1(AuthSHA(), "SHA-1"),
    SHA224(AuthHMAC128SHA224(), "SHA-224"),
    SHA256(AuthHMAC192SHA256(), "SHA-256"),
    SHA384(AuthHMAC256SHA384(), "SHA-384"),
    SHA512(AuthHMAC384SHA512(), "SHA-512");

    override fun toString(): String {
        return prettyName
    }

    class Provider : Enumeration.Provider {
        override fun values(): Array<out Any> {
            return AuthenticationProtocol.entries.toTypedArray()
        }
    }
}

I suspect this is only used somehow by the openapi / schema generation, and not by these config pages, and that it may simply not work at all right now if you try to override toString(). There are no usages of this in any of our config classes that I can see.

There's a lot of annotations in the openapi.annotations package and I think only a subset of them have any effect on the extension point config pages.

1 Like

The Enumeration.Provider interface seems to be used to do a dynamic lookup of something that may vary at runtime but is being represented as an enumeration in the openapi schema.

Its only use right now seems to be enumerating the ContactTypes that are currently available.

Yeah I'm figuring that out through trial and error :laughing:.

It looks like you can do this using FormChoices with:

  • ids = the name of the enum entry
  • labels = the toString() value of the entry
    @FormCategoryKey("Snmp.config.category.Authentication")
    @Label("Authentication Protocol *")
    @DescriptionKey("Snmp.config.Authentication.AuthProtocol.Description")
    @FormField(FormFieldType.SELECT)
    @FormChoices(
        ids = ["NONE", "MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512"],
        labels = ["None", "MD-5", "SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512"],
    )
    @Required
    val protocol: AuthenticationProtocol,
4 Likes