Skip to content

Commit 2c9c52e

Browse files
committed
fix(core): handle nil serializable and config, remove duplicate return
- Add nil check for serializable in payload() to use default serialization - Add nil check for config in Request() to return error and initialize missing components - Remove duplicate return statement in AtUser() method - Update struct tags in model builders to omit JSON fields - Expand test mock structs to include additional fields
1 parent c065273 commit 2c9c52e

File tree

6 files changed

+59
-28
lines changed

6 files changed

+59
-28
lines changed

card/card_test.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"net/http"
2121
"testing"
2222

23-
"github.com/larksuite/oapi-sdk-go/v3/core"
24-
"github.com/larksuite/oapi-sdk-go/v3/event"
23+
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
24+
larkevent "github.com/larksuite/oapi-sdk-go/v3/event"
2525
)
2626

2727
func TestVerifyUrlOk(t *testing.T) {
@@ -71,10 +71,15 @@ func mockEventReq(token string) *larkevent.EventReq {
7171
TenantKey: "d32004232",
7272
Token: token,
7373
Action: &struct {
74-
Value map[string]interface{} `json:"value"`
75-
Tag string `json:"tag"`
76-
Option string `json:"option"`
77-
Timezone string `json:"timezone"`
74+
Value map[string]interface{} `json:"value"`
75+
Tag string `json:"tag"`
76+
Option string `json:"option"`
77+
Timezone string `json:"timezone"`
78+
Name string `json:"name"`
79+
FormValue map[string]interface{} `json:"form_value"`
80+
InputValue string `json:"input_value"`
81+
Options []string `json:"options"`
82+
Checked bool `json:"checked"`
7883
}{
7984
Value: value,
8085
Tag: "button",
@@ -219,10 +224,15 @@ func mockCardAction() *CardAction {
219224
OpenMessageID: "om_abcdefg1234567890",
220225
TenantKey: "d32004232",
221226
Action: &struct {
222-
Value map[string]interface{} `json:"value"`
223-
Tag string `json:"tag"`
224-
Option string `json:"option"`
225-
Timezone string `json:"timezone"`
227+
Value map[string]interface{} `json:"value"`
228+
Tag string `json:"tag"`
229+
Option string `json:"option"`
230+
Timezone string `json:"timezone"`
231+
Name string `json:"name"`
232+
FormValue map[string]interface{} `json:"form_value"`
233+
InputValue string `json:"input_value"`
234+
Options []string `json:"options"`
235+
Checked bool `json:"checked"`
226236
}{
227237
Value: value,
228238
Tag: "button",
@@ -245,10 +255,15 @@ func TestDoHandleResultCardOk(t *testing.T) {
245255
OpenMessageID: "om_abcdefg1234567890",
246256
TenantKey: "d32004232",
247257
Action: &struct {
248-
Value map[string]interface{} `json:"value"`
249-
Tag string `json:"tag"`
250-
Option string `json:"option"`
251-
Timezone string `json:"timezone"`
258+
Value map[string]interface{} `json:"value"`
259+
Tag string `json:"tag"`
260+
Option string `json:"option"`
261+
Timezone string `json:"timezone"`
262+
Name string `json:"name"`
263+
FormValue map[string]interface{} `json:"form_value"`
264+
InputValue string `json:"input_value"`
265+
Options []string `json:"options"`
266+
Checked bool `json:"checked"`
252267
}{
253268
Value: value,
254269
Tag: "button",

core/httpserverext/httpserverext_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
"net/http"
2121
"testing"
2222

23-
"github.com/larksuite/oapi-sdk-go/v3/card"
24-
"github.com/larksuite/oapi-sdk-go/v3/core"
23+
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
24+
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
2525
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
26-
"github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
26+
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
2727
)
2828

2929
func TestStartHttpServer(t *testing.T) {
@@ -66,10 +66,15 @@ func mockRequest() *http.Request {
6666
TenantKey: "d32004232",
6767
Token: token,
6868
Action: &struct {
69-
Value map[string]interface{} `json:"value"`
70-
Tag string `json:"tag"`
71-
Option string `json:"option"`
72-
Timezone string `json:"timezone"`
69+
Value map[string]interface{} `json:"value"`
70+
Tag string `json:"tag"`
71+
Option string `json:"option"`
72+
Timezone string `json:"timezone"`
73+
Name string `json:"name"`
74+
FormValue map[string]interface{} `json:"form_value"`
75+
InputValue string `json:"input_value"`
76+
Options []string `json:"options"`
77+
Checked bool `json:"checked"`
7378
}{
7479
Value: value,
7580
Tag: "button",

core/httptransport.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ func doSend(ctx context.Context, rawRequest *http.Request, httpClient HttpClient
169169
}
170170

171171
func Request(ctx context.Context, req *ApiReq, config *Config, options ...RequestOptionFunc) (*ApiResp, error) {
172+
if config == nil {
173+
return nil, &IllegalParamError{msg: "config is nil"}
174+
}
175+
NewHttpClient(config)
176+
NewSerialization(config)
177+
if config.Logger == nil {
178+
NewLogger(config)
179+
}
180+
172181
option := &RequestOption{}
173182
for _, optionFunc := range options {
174183
optionFunc(option)

core/reqtranslator.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ func (translator *ReqTranslator) payload(body interface{}, serializable Serializ
226226
if body == nil {
227227
return contentType, nil, nil
228228
}
229+
if serializable == nil {
230+
serializable = &DefaultSerialization{}
231+
}
229232
bs, err := serializable.Serialize(body)
230233
return contentType, bs, err
231234
}

service/ext/model.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ type AuthenAccessTokenReqBody struct {
3131
}
3232

3333
type AuthenAccessTokenReqBodyBuilder struct {
34-
grantType string `json:"grant_type,omitempty"`
35-
code string `json:"code,omitempty"`
34+
grantType string
35+
code string
3636
}
3737

3838
func NewAuthenAccessTokenReqBodyBuilder() *AuthenAccessTokenReqBodyBuilder {
@@ -124,8 +124,8 @@ type RefreshAuthenAccessTokenReqBody struct {
124124
}
125125

126126
type RefreshAuthenAccessTokenReqBodyBuilder struct {
127-
grantType string `json:"grant_type,omitempty"`
128-
refreshToken string `json:"refresh_token,omitempty"`
127+
grantType string
128+
refreshToken string
129129
}
130130

131131
func NewRefreshAuthenAccessTokenReqBodyBuilder() *RefreshAuthenAccessTokenReqBodyBuilder {
@@ -263,8 +263,8 @@ type CreateFileReqBody struct {
263263
}
264264

265265
type CreateFileReqBodyBuilder struct {
266-
title string `json:"title,omitempty"`
267-
type_ string `json:"type,omitempty"`
266+
title string
267+
type_ string
268268
}
269269

270270
func NewCreateFileReqBodyBuilder() *CreateFileReqBodyBuilder {

service/im/v1/ext_model.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ func (t *MessageText) AtUser(userId, name string) *MessageText {
268268
t.builder.WriteString(name)
269269
t.builder.WriteString("</at>")
270270
return t
271-
return t
272271
}
273272

274273
func (t *MessageText) AtAll() *MessageText {

0 commit comments

Comments
 (0)