Skip to content

Better handling of unknown enum values in responses #1921

@stof

Description

@stof

Currently, our generator code says that our getter returns FooEnum::*, but the code parsing results assigns the string value returned by the backend without checking whether it is a known value. This means that the getter can actually return any string in case AWS adds new values in those endpoints.
Psalm actually detects this mismatch (that's 90% of the content of our baseline). phpstan does not report it because it is a rule checked at level 7 while we run it only at level 6 currently (when trying to enable level 7, it also detects it).

The AWS guidelines for their SDK require that SDK don't fail at runtime when receiving such values. Most official SDKs (maybe even all) implement this by generating an extra case in the enum (sdkUnknown in Kotlin, $unknown in JS, UNKNOWN_TO_SDK in Java, etc...) that is used by parsers when populating results if the case is unknown.
This approach forces projects to decide how to handle that case when they do a match on the enum (maybe they decide to fail at runtime with an exception asking to upgrade the SDK, maybe they have a graceful fallback, depending on what is appropriate).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions