Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Nov 22, 2024
1 parent 8afb27d commit 7b38e34
Show file tree
Hide file tree
Showing 15 changed files with 461 additions and 443 deletions.
17 changes: 12 additions & 5 deletions connector/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -363,15 +364,15 @@ func (client *HTTPClient) evalHTTPResponse(ctx context.Context, span trace.Span,
}

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

result = string(respBody)
case rest.ContentTypeXML:
case contentType == rest.ContentTypeXML:
field, err := client.extractResultType(resultType)
if err != nil {
return nil, nil, schema.NewConnectorError(http.StatusInternalServerError, "failed to extract forwarded headers response: "+err.Error(), nil)
Expand All @@ -381,7 +382,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 rest.ContentTypeJSON:
case contentType == rest.ContentTypeJSON:
if len(resultType) > 0 {
namedType, err := resultType.AsNamed()
if err == nil && namedType.Name == string(rest.ScalarString) {
Expand All @@ -408,7 +409,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 rest.ContentTypeNdJSON:
case contentType == rest.ContentTypeNdJSON:
var results []any
decoder := json.NewDecoder(resp.Body)
for decoder.More() {
Expand All @@ -421,6 +422,12 @@ 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/"):
rawBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, schema.NewConnectorError(http.StatusInternalServerError, err.Error(), nil)
}
result = base64.StdEncoding.EncodeToString(rawBytes)
default:
return nil, nil, schema.NewConnectorError(http.StatusInternalServerError, "failed to evaluate response", map[string]any{
"cause": "unsupported content type " + contentType,
Expand Down
21 changes: 11 additions & 10 deletions connector/internal/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (c *XMLDecoder) Decode(r io.Reader, resultType schema.Type) (any, error) {
}

if se, ok := token.(xml.StartElement); ok {
result, _, err := c.evalXMLField(se, rest.ObjectField{
result, _, err := c.evalXMLField(se, "", rest.ObjectField{
ObjectField: schema.ObjectField{
Type: resultType,
},
Expand All @@ -253,30 +253,30 @@ func (c *XMLDecoder) Decode(r io.Reader, resultType schema.Type) (any, error) {
return nil, nil
}

func (c *XMLDecoder) evalXMLField(token xml.StartElement, field rest.ObjectField, fieldPaths []string) (any, xml.Token, error) {
func (c *XMLDecoder) evalXMLField(token xml.StartElement, fieldName string, field rest.ObjectField, fieldPaths []string) (any, xml.Token, error) {
rawType, err := field.Type.InterfaceT()
if err != nil {
return nil, nil, err
}

switch t := rawType.(type) {
case *schema.NullableType:
return c.evalXMLField(token, rest.ObjectField{
return c.evalXMLField(token, fieldName, rest.ObjectField{
ObjectField: schema.ObjectField{
Type: t.UnderlyingType,
},
HTTP: field.HTTP,
}, fieldPaths)
case *schema.ArrayType:
return c.evalXMLArrayField(token, field, t, fieldPaths)
return c.evalXMLArrayField(token, fieldName, field, t, fieldPaths)
case *schema.NamedType:
return c.evalXMLNamedField(token, t, fieldPaths)
return c.evalXMLNamedField(token, fieldName, t, fieldPaths)
default:
return nil, nil, err
}
}

func (c *XMLDecoder) evalXMLNamedField(token xml.StartElement, t *schema.NamedType, fieldPaths []string) (any, xml.Token, error) {
func (c *XMLDecoder) evalXMLNamedField(token xml.StartElement, fieldName string, t *schema.NamedType, fieldPaths []string) (any, xml.Token, error) {
if scalarType, ok := c.schema.ScalarTypes[t.Name]; ok {
nextToken, err := c.decoder.Token()
if err != nil {
Expand All @@ -302,7 +302,8 @@ func (c *XMLDecoder) evalXMLNamedField(token xml.StartElement, t *schema.NamedTy
return nil, nil, fmt.Errorf("%s: invalid response type", strings.Join(fieldPaths, "."))
}

if objectType.XML == nil || objectType.XML.Name != token.Name.Local {
// the root field may have a different tag name, we can skip the validation
if len(fieldPaths) > 0 && (fieldName != "" && token.Name.Local != fieldName) && (objectType.XML == nil || objectType.XML.Name != token.Name.Local) {
return nil, nil, fmt.Errorf("%s:invalid token, expected: %s, got: %+v", strings.Join(fieldPaths, "."), token.Name.Local, objectType.XML)
}
nextToken, err := c.decoder.Token()
Expand Down Expand Up @@ -333,7 +334,7 @@ L:
continue
}

fieldResult, nt, err := c.evalXMLField(nt, objectField, append(fieldPaths, key))
fieldResult, nt, err := c.evalXMLField(nt, key, objectField, append(fieldPaths, key))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -371,7 +372,7 @@ L:
return result, nextToken, nil
}

func (c *XMLDecoder) evalXMLArrayField(token xml.StartElement, field rest.ObjectField, t *schema.ArrayType, fieldPaths []string) (any, xml.Token, error) {
func (c *XMLDecoder) evalXMLArrayField(token xml.StartElement, fieldName string, field rest.ObjectField, t *schema.ArrayType, fieldPaths []string) (any, xml.Token, error) {
var wrapped bool
var nextToken xml.Token = token
var itemTokenName string
Expand Down Expand Up @@ -448,7 +449,7 @@ LA:
break LA
}

item, tok, err := c.evalXMLField(nt, fieldItem, append(fieldPaths, strconv.FormatInt(i, 10)))
item, tok, err := c.evalXMLField(nt, fieldName, fieldItem, append(fieldPaths, strconv.FormatInt(i, 10)))
if err != nil {
return nil, nil, err
}
Expand Down
43 changes: 43 additions & 0 deletions connector/internal/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package internal

import (
"bytes"
"strings"
"testing"

rest "github.com/hasura/ndc-http/ndc-http-schema/schema"
"github.com/hasura/ndc-sdk-go/schema"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -61,3 +63,44 @@ func TestCreateXMLForm(t *testing.T) {
})
}
}

func TestDecodeXML(t *testing.T) {
testCases := []struct {
Name string
Body string
Type schema.Type
Expected map[string]any
}{
{
Name: "array_nowrap",
Type: schema.NewNamedType("GetArchitecturesResult").Encode(),
Body: `<xml><count>4</count><entry><name>aarch64</name></entry><entry><name>armv7l</name></entry><entry><name>s390x</name></entry><entry><name>x86_64</name></entry></xml>`,
Expected: map[string]any{
"count": int64(4),
"entry": []any{
map[string]any{
"name": "aarch64",
},
map[string]any{
"name": "armv7l",
},
map[string]any{
"name": "s390x",
},
map[string]any{
"name": "x86_64",
},
},
},
},
}

ndcSchema := createMockSchema(t)
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
result, err := NewXMLDecoder(ndcSchema).Decode(strings.NewReader(tc.Body), tc.Type)
assert.NilError(t, err)
assert.DeepEqual(t, tc.Expected, result)
})
}
}
7 changes: 7 additions & 0 deletions ndc-http-schema/openapi/internal/oas3.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ func (oc *OAS3Builder) convertComponentSchemas(schemaItem orderedmap.Pair[string
typeName = getNamedType(typeEncoder, true, "")
}

if schemaResult.XML == nil {
schemaResult.XML = &rest.XMLSchema{}
}
if schemaResult.XML.Name == "" {
schemaResult.XML.Name = typeKey
}

cacheKey := "#/components/schemas/" + typeKey
// treat no-property objects as a Arbitrary JSON scalar
if typeEncoder == nil || typeName == string(rest.ScalarJSON) {
Expand Down
7 changes: 6 additions & 1 deletion ndc-http-schema/openapi/internal/oas3_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func newOAS3OperationBuilder(builder *OAS3Builder, pathKey string, method string

// BuildFunction build a HTTP NDC function information from OpenAPI v3 operation
func (oc *oas3OperationBuilder) BuildFunction(itemGet *v3.Operation) (*rest.OperationInfo, string, error) {
if oc.builder.ConvertOptions.NoDeprecation && itemGet.Deprecated != nil && *itemGet.Deprecated {
return nil, "", nil
}

start := time.Now()
funcName := buildUniqueOperationName(oc.builder.schema, itemGet.OperationId, oc.pathKey, oc.method, oc.builder.ConvertOptions)

Expand Down Expand Up @@ -80,9 +84,10 @@ func (oc *oas3OperationBuilder) BuildFunction(itemGet *v3.Operation) (*rest.Oper
}

func (oc *oas3OperationBuilder) BuildProcedure(operation *v3.Operation) (*rest.OperationInfo, string, error) {
if operation == nil {
if operation == nil || (oc.builder.ConvertOptions.NoDeprecation && operation.Deprecated != nil && *operation.Deprecated) {
return nil, "", nil
}

start := time.Now()
procName := buildUniqueOperationName(oc.builder.schema, operation.OperationId, oc.pathKey, oc.method, oc.builder.ConvertOptions)

Expand Down
15 changes: 3 additions & 12 deletions ndc-http-schema/openapi/internal/oas3_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,17 @@ func (oc *oas3SchemaBuilder) getSchemaType(typeSchema *base.Schema, fieldPaths [
return oc.builder.buildScalarJSON(), typeResult, nil
}

xmlSchema := typeResult.XML
if xmlSchema == nil {
xmlSchema = &rest.XMLSchema{}
}

if xmlSchema.Name == "" {
xmlSchema.Name = fieldPaths[0]
}

object := rest.ObjectType{
Fields: make(map[string]rest.ObjectField),
XML: xmlSchema,
XML: typeResult.XML,
}
readObject := rest.ObjectType{
Fields: make(map[string]rest.ObjectField),
XML: xmlSchema,
XML: typeResult.XML,
}
writeObject := rest.ObjectType{
Fields: make(map[string]rest.ObjectField),
XML: xmlSchema,
XML: typeResult.XML,
}

if description != "" {
Expand Down
4 changes: 2 additions & 2 deletions ndc-http-schema/openapi/oas3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestOpenAPIv3ToRESTSchema(t *testing.T) {
EnvPrefix: "PET_STORE",
},
},
// go run ./ndc-http-schema convert -f ./ndc-http-schema/openapi/testdata/onesignal/source.json -o ./ndc-http-schema/openapi/testdata/onesignal/expected.json --spec openapi3
// go run ./ndc-http-schema convert -f ./ndc-http-schema/openapi/testdata/onesignal/source.json -o ./ndc-http-schema/openapi/testdata/onesignal/schema.json --pure --spec openapi3
// go run ./ndc-http-schema convert -f ./ndc-http-schema/openapi/testdata/onesignal/source.json -o ./ndc-http-schema/openapi/testdata/onesignal/expected.json --spec openapi3 --no-deprecation
// go run ./ndc-http-schema convert -f ./ndc-http-schema/openapi/testdata/onesignal/source.json -o ./ndc-http-schema/openapi/testdata/onesignal/schema.json --pure --spec openapi3 --no-deprecation
{
Name: "onesignal",
Source: "testdata/onesignal/source.json",
Expand Down
30 changes: 0 additions & 30 deletions ndc-http-schema/openapi/testdata/onesignal/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@
]
}
}
},
"xml": {
"name": "CancelNotificationSuccessResponse"
}
},
"CreateNotificationSuccessResponse": {
Expand Down Expand Up @@ -254,9 +251,6 @@
]
}
}
},
"xml": {
"name": "CreateNotificationSuccessResponse"
}
},
"DeliveryData": {
Expand Down Expand Up @@ -336,9 +330,6 @@
]
}
}
},
"xml": {
"name": "DeliveryData"
}
},
"Filter": {
Expand Down Expand Up @@ -397,9 +388,6 @@
]
}
}
},
"xml": {
"name": "Filter"
}
},
"GetNotificationHistoryBody": {
Expand Down Expand Up @@ -448,9 +436,6 @@
]
}
}
},
"xml": {
"name": "get_notification_history"
}
},
"NotificationHistorySuccessResponse": {
Expand Down Expand Up @@ -483,9 +468,6 @@
]
}
}
},
"xml": {
"name": "NotificationHistorySuccessResponse"
}
},
"NotificationInput": {
Expand Down Expand Up @@ -670,9 +652,6 @@
]
}
}
},
"xml": {
"name": "NotificationSlice"
}
},
"NotificationWithMeta": {
Expand Down Expand Up @@ -1030,9 +1009,6 @@
]
}
}
},
"xml": {
"name": "OutcomeData"
}
},
"PlatformDeliveryData": {
Expand Down Expand Up @@ -1150,9 +1126,6 @@
]
}
}
},
"xml": {
"name": "PlatformDeliveryData"
}
},
"StringMap": {
Expand All @@ -1172,9 +1145,6 @@
]
}
}
},
"xml": {
"name": "StringMap"
}
}
},
Expand Down
Loading

0 comments on commit 7b38e34

Please sign in to comment.