Skip to content

Optional enum set to undefined serializes as -1 and breaks unpack() #4

Description

@mirismaili

Description

When an optional enum field is explicitly set to undefined during pack(), it is serialized as enum index -1. The resulting payload is accepted by pack() but cannot be read back by unpack().

Omitting the same optional property entirely works correctly.

Versions

  • gramio: 0.8.3
  • @gramio/callback-data: 0.1.0

Minimal reproduction

import {CallbackData} from '@gramio/callback-data'

const action = new CallbackData('example-action')
  .string('itemId')
  .enum('deliveryMethod', ['email', 'sms'], {optional: true})

const packedWithUndefined = action.pack({
  itemId: '123',
  deliveryMethod: undefined,
})

console.log(packedWithUndefined)
action.unpack(packedWithUndefined)

Actual behavior

pack() serializes the optional enum as -1. Then unpack() throws:

Error: Invalid index -1 for enum 'deliveryMethod'

By contrast, this round-trip succeeds:

const packedWithoutProperty = action.pack({itemId: '123'})
console.log(action.unpack(packedWithoutProperty))

Expected behavior

Explicit undefined for an optional field should ideally be treated the same as an omitted property. Alternatively, pack() should reject it immediately with a clear error instead of producing a payload that unpack() cannot deserialize.

This is related to, but distinct from, #3: that issue concerns warning output for a valid omitted optional field, while this one produces an invalid payload and a runtime exception.

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