Files
go-telegram/api/unionparam_test.go
T
lukaszraczylo 370c9c0802 refactor(api): auto-inject discriminator value via generated MarshalJSON
Sealed-interface union variants now hardcode their wire discriminator
inside a generated MarshalJSON method instead of forcing callers to set
the field on every struct literal. Drops a class of silent-rejection
bugs where a typo in the discriminator slipped past the type checker
and through to Telegram, which then rejected the request with no
Go-side signal.

The discriminator field stays exported so incoming-message decoding,
type switches and debugging still see it. MarshalJSON wraps via a
function-local type alias and emits an outer field with the same json
tag; encoding/json (and goccy/go-json) resolve the outer field as the
shallower one and override whatever the caller wrote.

99 variants get MarshalJSON. 7 are skipped because their unions
dispatch structurally rather than by a string field: Message and
InaccessibleMessage (MaybeInaccessibleMessage, dispatched on date),
and the InputMessageContent family (InputTextMessageContent,
InputLocationMessageContent, InputVenueMessageContent,
InputContactMessageContent, InputInvoiceMessageContent — Telegram
identifies these by the presence of message_text / latitude /
phone_number / title etc.).

Discriminator extraction lives in the emitter (cmd/genapi/emitter.go).
Resolution: knownDiscriminators reverse-lookup for the 13 auto-decode
unions, then doc-string analysis ("must be X" / "always “X”")
of the variant's first required string field for marker-only unions
(BotCommandScope, InputMedia, InputPaidMedia, InputProfilePhoto,
InputStoryContent, InputPollMedia, InputPollOptionMedia,
InlineQueryResult, PassportElementError). Variants the emitter cannot
resolve a discriminator for are skipped silently rather than emitting
broken code.

Internal call-site cleanups: 4 manual discriminator assignments
removed (api/unionparam_test.go,
dispatch/filters/message/message_test.go, examples/inline/main.go ×2).
Regression tests added in api/marshaljson_variants_test.go covering
type-keyed variants, source-keyed variants, the override-user-typo
guarantee, round-trip preservation through UnmarshalChatMember, the
no-discriminator InputMessageContent path, and ride-along of
non-discriminator fields.

regen-from-fixture is deterministic across two consecutive runs;
go test -race / go vet / staticcheck all clean.
2026-05-09 19:27:33 +01:00

78 lines
2.6 KiB
Go

package api
import (
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/lukaszraczylo/go-telegram/client"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// TestSetMyCommands_BotCommandScope_NoPointerToInterface is a regression
// test for the bug where sealed-interface union types without an
// auto-decode discriminator (BotCommandScope, InputMedia, etc.) were
// emitted as `*<Union>` (pointer-to-interface) when used as optional
// fields. Pointer-to-interface is a Go anti-pattern: the interface is
// already nil-able, and callers were forced to write
// `Scope: &someConcreteScope` instead of `Scope: someConcreteScope`.
//
// This test confirms the field is now bare-interface-typed: a concrete
// variant assigns directly, and a nil scope omits the field via
// omitempty.
func TestSetMyCommands_BotCommandScope_NoPointerToInterface(t *testing.T) {
var captured string
m := &mockDoer{}
m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
if r.Body != nil {
b, _ := io.ReadAll(r.Body)
captured = string(b)
}
return true
})).Return(&http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{"ok":true,"result":true}`)),
Header: http.Header{"Content-Type": []string{"application/json"}},
}, nil)
bot := client.New("test:token", client.WithHTTPClient(m))
// Direct assignment of a concrete variant — only possible when Scope
// is `BotCommandScope` (interface), not `*BotCommandScope`.
ok, err := SetMyCommands(context.Background(), bot, &SetMyCommandsParams{
Commands: []BotCommand{{Command: "start", Description: "begin"}},
Scope: &BotCommandScopeAllPrivateChats{},
})
require.NoError(t, err)
require.True(t, ok)
require.Contains(t, captured, `"scope":{"type":"all_private_chats"}`)
}
// TestSetMyCommands_NilScope_OmitsField confirms omitempty works on the
// bare-interface field when the caller doesn't supply a scope.
func TestSetMyCommands_NilScope_OmitsField(t *testing.T) {
var captured string
m := &mockDoer{}
m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
if r.Body != nil {
b, _ := io.ReadAll(r.Body)
captured = string(b)
}
return true
})).Return(&http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{"ok":true,"result":true}`)),
Header: http.Header{"Content-Type": []string{"application/json"}},
}, nil)
bot := client.New("test:token", client.WithHTTPClient(m))
_, err := SetMyCommands(context.Background(), bot, &SetMyCommandsParams{
Commands: []BotCommand{{Command: "start", Description: "begin"}},
})
require.NoError(t, err)
require.NotContains(t, captured, `"scope"`, "nil scope must be omitted from JSON")
}