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

Make extensible enums actually extensible #213

Open
atollk opened this issue May 17, 2023 · 3 comments
Open

Make extensible enums actually extensible #213

atollk opened this issue May 17, 2023 · 3 comments

Comments

@atollk
Copy link
Contributor

atollk commented May 17, 2023

Right now, using x-extensible-enum to declare an enum instead of the enum keyword does not actually generate "extensible enums", as the name suggests. Instead, it's just an alias for "enum". For example, this enum model here:

enum class ExtensibleEnumObject(
    @JsonValue
    val value: String
) {
    ACTIVE("active"),

    INACTIVE("inactive");

    companion object {
        private val mapping: Map<String, ExtensibleEnumObject> =
            values().associateBy(ExtensibleEnumObject::value)

        fun fromValue(value: String): ExtensibleEnumObject? = mapping[value]
    }
}

should actually look more like this when marked as "extensible": (untested code)

enum class ExtensibleEnumObject(
    @JsonValue
    val value: String
) {
    ACTIVE("active"),

    INACTIVE("inactive"),

    UNKNOWN("");

    companion object {
        private val mapping: Map<String, ExtensibleEnumObject> =
            values().filter { it != UNKNOWN }.associateBy(ExtensibleEnumObject::value)

        @JsonCreator
        fun fromValue(value: String): ExtensibleEnumObject = mapping[value] ?: UNKNOWN
    }
}
@atollk atollk changed the title "Fix" extensible enums Make extensible enums actually extensible May 17, 2023
@cjbooms
Copy link
Owner

cjbooms commented May 17, 2023

Interesting idea. The way we use this feature is we opt-in to the alias when generating the server-side but leave it disabled for client side. That way we get the strict typing server side, and our clients don't break when we extend with new values.

Your proposal might be a nice improvement though.

Side note, I've a relatively clear calendar tomorrow and will try and catch up fabrikt issues and PRs

@atollk
Copy link
Contributor Author

atollk commented May 17, 2023

Interesting idea. The way we use this feature is we opt-in to the alias when generating the server-side but leave it disabled for client side. That way we get the strict typing server side, and our clients don't break when we extend with new values.

So you're mainly using it for extensibility in responses (i.e. the client might have to deal with unknown values) and my proposed change would offer extensibility in requests (i.e. the server might have to deal with unknown values). Correct?

Side note, I've a relatively clear calendar tomorrow and will try and catch up fabrikt issues and PRs

Nice, thank you :)

@cjbooms
Copy link
Owner

cjbooms commented May 17, 2023

Yes, at Zalando x-extensible-enum is used to communicate to clients that: these are the current options, but new options may be added in the future

To my knowledge, it is never used to allow clients to make requests using undefined enum values, only to prepare clients to gracefully handle responses with unexpected new enum values as the API evolves over time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants