Files
go-telegram/api/unionparam_test.go
T
lukaszraczylo 6ab80c27e1 fix(api): emit bare interface for all sealed-interface unions
Optional fields whose type was a sealed-interface union without an
auto-decode discriminator (BotCommandScope, InputMedia, InputPaidMedia,
InputProfilePhoto, InputStoryContent, InputMessageContent,
InputPollMedia, InputPollOptionMedia, InlineQueryResult,
PassportElementError) were emitted as *<Union> — pointer to interface,
which is a Go anti-pattern: interfaces are already nil-able, and
callers were forced to take addresses of concrete variants.

The codegen path goType() guarded against pointer-wrapping using
knownDiscriminators (only 13 unions with auto-decode dispatch), missing
the 10 marker-only sealed interfaces. New package var
knownInterfaceTypes is built from buildUnionTypeSet at emitter
construction and covers both kinds. goType() now consults that.

Net effect:
- api/methods.gen.go: 3 *BotCommandScope and 2 *InputPollMedia fields
  become bare interface
- api/types.gen.go: 14 *InputMessageContent and 1 *InputPollOptionMedia
  fields become bare interface

Regression tests in api/unionparam_test.go cover both shapes:
direct concrete-variant assignment, and nil omitempty on the bare
interface field.
2026-05-09 19:06:59 +01:00

78 lines
2.7 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{Type: "all_private_chats"},
})
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")
}