Skip to content

Commit f7c83c5

Browse files
fix: Improve json detection
1 parent b8d97be commit f7c83c5

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

internal/pkg/service/stream/mapping/recordctx/utils.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
11
package recordctx
22

33
import (
4-
"strings"
5-
64
jsoniter "github.com/json-iterator/go"
75
"github.com/keboola/go-utils/pkg/orderedmap"
8-
"github.com/umisama/go-regexpcache"
96

107
serviceError "github.com/keboola/keboola-as-code/internal/pkg/service/common/errors"
118
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
9+
"github.com/keboola/keboola-as-code/internal/pkg/utils/httputils"
1210
utilsUrl "github.com/keboola/keboola-as-code/internal/pkg/utils/url"
1311
)
1412

1513
// json - replacement of the standard encoding/json library, it is faster for larger responses.
1614
var json = jsoniter.ConfigCompatibleWithStandardLibrary //nolint:gochecknoglobals
1715

18-
func isContentTypeJSON(t string) bool {
19-
return regexpcache.MustCompile(`^application/([a-zA-Z0-9\.\-]+\+)?json$`).MatchString(t)
20-
}
21-
22-
func isContentTypeForm(t string) bool {
23-
return strings.HasPrefix(t, "application/x-www-form-urlencoded")
24-
}
25-
2616
func parseBody(contentType string, body []byte) (data *orderedmap.OrderedMap, err error) {
2717
// Decode
2818
switch {
29-
case isContentTypeForm(contentType):
19+
case httputils.IsContentTypeForm(contentType):
3020
data, err = utilsUrl.ParseQuery(string(body))
3121
if err != nil {
3222
return nil, serviceError.NewBadRequestError(errors.Errorf("invalid form data: %w", err))
3323
}
34-
case isContentTypeJSON(contentType):
24+
case httputils.IsContentTypeJSON(contentType):
3525
err = json.Unmarshal(body, &data)
3626
if err != nil {
3727
return nil, serviceError.NewBadRequestError(errors.Errorf("invalid JSON: %w", err))

internal/pkg/service/stream/mapping/table/column/renderer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/mapping/jsonnet"
1515
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/mapping/recordctx"
1616
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
17+
"github.com/keboola/keboola-as-code/internal/pkg/utils/httputils"
1718
)
1819

1920
type Renderer struct {
@@ -47,9 +48,9 @@ func (r *Renderer) CSVValue(c Column, ctx recordctx.Context) (string, error) {
4748
case IP:
4849
return ctx.ClientIP().String(), nil
4950
case Path:
50-
contentType := ctx.HeadersMap().GetOrNil("Content-Type")
51+
contentType, ok := ctx.HeadersMap().GetOrNil("Content-Type").(string)
5152

52-
if contentType == "application/json" {
53+
if ok && httputils.IsContentTypeJSON(contentType) {
5354
return r.jsonPathCSVValue(c, ctx)
5455
}
5556

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package httputils
2+
3+
import (
4+
"strings"
5+
6+
"github.com/umisama/go-regexpcache"
7+
)
8+
9+
func IsContentTypeJSON(t string) bool {
10+
return regexpcache.MustCompile(`^application/([a-zA-Z0-9\.\-]+\+)?json$`).MatchString(t)
11+
}
12+
13+
func IsContentTypeForm(t string) bool {
14+
return strings.HasPrefix(t, "application/x-www-form-urlencoded")
15+
}

0 commit comments

Comments
 (0)