Skip to content

Commit

Permalink
cli: improve openapi union types conversion (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Dec 21, 2024
1 parent b152004 commit ddf3d32
Show file tree
Hide file tree
Showing 33 changed files with 3,999 additions and 571 deletions.
8 changes: 4 additions & 4 deletions connector/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,14 @@ func (client *HTTPClient) evalHTTPResponse(ctx context.Context, span trace.Span,

var result any
switch {
case strings.HasPrefix(contentType, "text/") || strings.HasPrefix(contentType, "image/svg"):
case restUtils.IsContentTypeText(contentType):
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, schema.NewConnectorError(http.StatusInternalServerError, err.Error(), nil)
}

result = string(respBody)
case contentType == rest.ContentTypeXML || strings.HasSuffix(contentType, "+xml"):
case restUtils.IsContentTypeXML(contentType):
field, extractErr := client.extractResultType(resultType)
if extractErr != nil {
return nil, nil, extractErr
Expand All @@ -394,7 +394,7 @@ func (client *HTTPClient) evalHTTPResponse(ctx context.Context, span trace.Span,
if err != nil {
return nil, nil, schema.NewConnectorError(http.StatusInternalServerError, err.Error(), nil)
}
case contentType == rest.ContentTypeJSON || strings.HasSuffix(contentType, "+json"):
case restUtils.IsContentTypeJSON(contentType):
if len(resultType) > 0 {
namedType, err := resultType.AsNamed()
if err == nil && namedType.Name == string(rest.ScalarString) {
Expand Down Expand Up @@ -445,7 +445,7 @@ func (client *HTTPClient) evalHTTPResponse(ctx context.Context, span trace.Span,
}

result = results
case strings.HasPrefix(contentType, "application/") || strings.HasPrefix(contentType, "image/") || strings.HasPrefix(contentType, "video/"):
case restUtils.IsContentTypeBinary(contentType):
rawBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, schema.NewConnectorError(http.StatusInternalServerError, err.Error(), nil)
Expand Down
9 changes: 5 additions & 4 deletions connector/internal/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/hasura/ndc-http/connector/internal/contenttype"
rest "github.com/hasura/ndc-http/ndc-http-schema/schema"
restUtils "github.com/hasura/ndc-http/ndc-http-schema/utils"
"github.com/hasura/ndc-sdk-go/schema"
"github.com/hasura/ndc-sdk-go/utils"
)
Expand Down Expand Up @@ -111,13 +112,13 @@ func (c *RequestBuilder) buildRequestBody(request *RetryableRequest, rawRequest
request.Body = r

return nil
case strings.HasPrefix(contentType, "text/"):
case restUtils.IsContentTypeText(contentType):
r := bytes.NewReader([]byte(fmt.Sprint(bodyData)))
request.ContentLength = r.Size()
request.Body = r

return nil
case strings.HasPrefix(contentType, "multipart/"):
case restUtils.IsContentTypeMultipartForm(contentType):
r, contentType, err := contenttype.NewMultipartFormEncoder(c.Schema, c.Operation, c.Arguments).Encode(bodyData)
if err != nil {
return err
Expand All @@ -137,7 +138,7 @@ func (c *RequestBuilder) buildRequestBody(request *RetryableRequest, rawRequest
request.ContentLength = size

return nil
case contentType == rest.ContentTypeJSON || contentType == "" || strings.HasSuffix(contentType, "+json"):
case contentType == "" || restUtils.IsContentTypeJSON(contentType):
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
Expand All @@ -150,7 +151,7 @@ func (c *RequestBuilder) buildRequestBody(request *RetryableRequest, rawRequest
request.Body = bytes.NewReader(buf.Bytes())

return nil
case contentType == rest.ContentTypeXML || strings.HasSuffix(contentType, "+xml"):
case restUtils.IsContentTypeXML(contentType):
bodyBytes, err := contenttype.NewXMLEncoder(c.Schema).Encode(&bodyInfo, bodyData)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions connector/internal/request_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ func (rqe *RawRequestBuilder) decodeArguments() (*RetryableRequest, error) {

func (rqe *RawRequestBuilder) evalRequestBody(rawBody json.RawMessage, contentType string) (io.ReadSeeker, string, int64, error) {
switch {
case contentType == rest.ContentTypeJSON || strings.HasSuffix(contentType, "+json"):
case restUtils.IsContentTypeJSON(contentType):
if !json.Valid(rawBody) {
return nil, "", 0, fmt.Errorf("invalid json body: %s", string(rawBody))
}

return bytes.NewReader(rawBody), contentType, int64(len(rawBody)), nil
case contentType == rest.ContentTypeXML || strings.HasSuffix(contentType, "+xml"):
case restUtils.IsContentTypeXML(contentType):
var bodyData any
if err := json.Unmarshal(rawBody, &bodyData); err != nil {
return nil, "", 0, fmt.Errorf("invalid body: %w", err)
Expand All @@ -230,14 +230,14 @@ func (rqe *RawRequestBuilder) evalRequestBody(rawBody json.RawMessage, contentTy
}

return bytes.NewReader(bodyBytes), contentType, int64(len(bodyBytes)), nil
case strings.HasPrefix(contentType, "text/"):
case restUtils.IsContentTypeText(contentType):
var bodyData string
if err := json.Unmarshal(rawBody, &bodyData); err != nil {
return nil, "", 0, fmt.Errorf("invalid body: %w", err)
}

return strings.NewReader(bodyData), contentType, int64(len(bodyData)), nil
case strings.HasPrefix(contentType, "multipart/"):
case restUtils.IsContentTypeMultipartForm(contentType):
var bodyData any
if err := json.Unmarshal(rawBody, &bodyData); err != nil {
return nil, "", 0, fmt.Errorf("invalid body: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions connector/testdata/tls/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ files:
value: 10
retry:
times:
value: 1
value: 2
delay:
value: 500
value: 1000
httpStatus: [429, 500, 501, 502]
17 changes: 12 additions & 5 deletions ndc-http-schema/openapi/internal/oas2_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"fmt"
"log/slog"
"net/http"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -325,13 +326,19 @@ func (oc *oas2OperationBuilder) convertResponse(operation *v2.Operation, fieldPa

// return nullable boolean type if the response content is null
if resp == nil || resp.Schema == nil {
scalarName := rest.ScalarJSON
if statusCode == 204 {
scalarName = rest.ScalarBoolean
if statusCode == http.StatusNoContent {
scalarName := rest.ScalarBoolean
oc.builder.schema.AddScalar(string(scalarName), *defaultScalarTypes[scalarName])

return schema.NewNullableNamedType(string(scalarName)), response, nil
}
oc.builder.schema.AddScalar(string(scalarName), *defaultScalarTypes[scalarName])

return schema.NewNullableNamedType(string(scalarName)), response, nil
if contentType != "" {
scalarName := guessScalarResultTypeFromContentType(contentType)
oc.builder.schema.AddScalar(string(scalarName), *defaultScalarTypes[scalarName])

return schema.NewNamedType(string(scalarName)), response, nil
}
}

if resp.Schema == nil {
Expand Down
Loading

0 comments on commit ddf3d32

Please sign in to comment.