Skip to content

Commit

Permalink
disable http escape when encoding json
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Dec 14, 2024
1 parent 434576f commit 569477c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
7 changes: 6 additions & 1 deletion connector/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ func (client *HTTPClient) sendSingle(ctx context.Context, request *RetryableRequ

span.SetStatus(codes.Error, "received error from remote server")

return nil, nil, schema.NewConnectorError(resp.StatusCode, resp.Status, details)
statusCode := resp.StatusCode
if statusCode < 500 {
statusCode = http.StatusUnprocessableEntity
}

return nil, nil, schema.NewConnectorError(statusCode, resp.Status, details)
}

result, headers, evalErr := client.evalHTTPResponse(ctx, span, resp, contentType, selection, logger)
Expand Down
10 changes: 6 additions & 4 deletions connector/internal/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,16 @@ func (c *RequestBuilder) buildRequestBody(request *RetryableRequest, rawRequest

return nil
case contentType == rest.ContentTypeJSON || contentType == "":
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)

bodyBytes, err := json.Marshal(bodyData)
if err != nil {
if err := enc.Encode(bodyData); err != nil {
return err
}

request.ContentLength = int64(len(bodyBytes))
request.Body = bytes.NewReader(bodyBytes)
request.ContentLength = int64(buf.Len())
request.Body = bytes.NewReader(buf.Bytes())

return nil
case contentType == rest.ContentTypeXML:
Expand Down
8 changes: 8 additions & 0 deletions ndc-http-schema/configuration/argument_presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func evalTypeRepresentationFromJSONPath(httpSchema *rest.NDCHttpSchema, jsonPath
return nil, err
}

if typeRep == nil {
return nil, nil
}

// if the json path selects the root field only, remove the argument field
if len(segments) == 1 && isGlobal {
delete(operation.Arguments, rootSelector)
Expand All @@ -115,6 +119,10 @@ func evalArgumentFromJSONPath(httpSchema *rest.NDCHttpSchema, typeSchema schema.
return nil, nil, err
}

if underlyingType == nil {
return nil, nil, nil
}

if _, ok := underlyingType.(*schema.NullableType); ok {
return underlyingType, typeRep, nil
}
Expand Down
1 change: 1 addition & 0 deletions ndc-http-schema/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func MarshalSchema(content any, format schema.SchemaFileFormat) ([]byte, error)
case schema.SchemaFileJSON:
encoder := json.NewEncoder(&fileBuffer)
encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)

if err := encoder.Encode(content); err != nil {
return nil, fmt.Errorf("failed to encode NDC HTTP schema: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions ndc-http-schema/utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"fmt"
"regexp"
"slices"
"strings"
"unicode"
"unicode/utf8"
Expand Down Expand Up @@ -218,6 +219,9 @@ func RemoveYAMLSpecialCharacters(input []byte) string {
u := getu4(input[i:])
if u > -1 {
i += 5
if slices.Contains([]rune{'<', '>', '&'}, u) {
sb.WriteRune(u)
}
} else {
sb.WriteRune(r)
i++
Expand Down

0 comments on commit 569477c

Please sign in to comment.