Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
[HOTFIX] Fix connectivity-adapter json marshalling issue (#1771)
Browse files Browse the repository at this point in the history
* [HOTFIX] Fix connectivity-adapter json marshalling issue

* Update connectivity-adapter version

* Use escape HTML encoder

* Use custom json marshalling and unmarshalling
  • Loading branch information
PetarTodorovv authored Mar 17, 2021
1 parent cd3f148 commit 1e0e09f
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 59 deletions.
2 changes: 1 addition & 1 deletion chart/compass/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ global:
version: "PR-1762"
connectivity_adapter:
dir:
version: "PR-1750"
version: "PR-1771"
pairing_adapter:
dir:
version: "PR-1750"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package service
import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -132,15 +131,14 @@ func (c *converter) DetailsToGraphQLCreateInput(deprecated model.ServiceDetails)
if apiDef.Spec == nil {
apiDef.Spec = &graphql.APISpecInput{}
}
asClob := graphql.CLOB(string(deprecated.Api.Spec))
apiDef.Spec.Data = &asClob
apiDef.Spec.Data = ptrClob(graphql.CLOB(*deprecated.Api.Spec))
if apiDef.Spec.Type == "" {
apiDef.Spec.Type = graphql.APISpecTypeOpenAPI
}

if c.isXML(string(deprecated.Api.Spec)) {
if model.IsXML(string(*deprecated.Api.Spec)) {
apiDef.Spec.Format = graphql.SpecFormatXML
} else if c.isJSON(deprecated.Api.Spec) {
} else if c.isJSON(string(*deprecated.Api.Spec)) {
apiDef.Spec.Format = graphql.SpecFormatJSON
} else {
apiDef.Spec.Format = graphql.SpecFormatYaml
Expand Down Expand Up @@ -229,9 +227,9 @@ func (c *converter) DetailsToGraphQLCreateInput(deprecated model.ServiceDetails)

// TODO add tests
var format graphql.SpecFormat
if c.isXML(string(deprecated.Events.Spec)) {
if model.IsXML(string(*deprecated.Events.Spec)) {
format = graphql.SpecFormatXML
} else if c.isJSON(deprecated.Events.Spec) {
} else if c.isJSON(string(*deprecated.Events.Spec)) {
format = graphql.SpecFormatJSON
} else {
format = graphql.SpecFormatYaml
Expand All @@ -241,7 +239,7 @@ func (c *converter) DetailsToGraphQLCreateInput(deprecated model.ServiceDetails)
&graphql.EventDefinitionInput{
Name: deprecated.Name,
Spec: &graphql.EventSpecInput{
Data: ptrClob(graphql.CLOB(deprecated.Events.Spec)),
Data: ptrClob(graphql.CLOB(*deprecated.Events.Spec)),
Type: graphql.EventSpecTypeAsyncAPI,
Format: format,
},
Expand Down Expand Up @@ -350,7 +348,7 @@ func (c *converter) GraphQLToServiceDetails(in graphql.BundleExt, legacyServiceR
if apiDef.Spec != nil {
outDeprecated.Api.ApiType = string(apiDef.Spec.Type)
if apiDef.Spec.Data != nil {
outDeprecated.Api.Spec = json.RawMessage(*apiDef.Spec.Data)
outDeprecated.Api.Spec = ptrSpecResponse(model.SpecResponse(*apiDef.Spec.Data))
}
}

Expand Down Expand Up @@ -477,7 +475,7 @@ func (c *converter) GraphQLToServiceDetails(in graphql.BundleExt, legacyServiceR

if eventDef.Spec != nil && eventDef.Spec.Data != nil {
outDeprecated.Events = &model.Events{
Spec: []byte(string(*eventDef.Spec.Data)),
Spec: ptrSpecResponse(model.SpecResponse(*eventDef.Spec.Data)),
}
}
//TODO: convert also fetchRequest
Expand Down Expand Up @@ -510,33 +508,16 @@ func (c *converter) ServiceDetailsToService(in model.ServiceDetails, serviceID s
}, nil
}

func ptrClob(in graphql.CLOB) *graphql.CLOB {
return &in
func (c *converter) isJSON(content string) bool {
out := map[string]interface{}{}
err := json.Unmarshal([]byte(content), &out)
return err == nil
}

func (c *converter) isXML(content string) bool {
const snippetLength = 512

if unquoted, err := strconv.Unquote(content); err == nil {
content = unquoted
}

var snippet string
length := len(content)
if length < snippetLength {
snippet = content
} else {
snippet = content[:snippetLength]
}

openingIndex := strings.Index(snippet, "<")
closingIndex := strings.Index(snippet, ">")

return openingIndex == 0 && openingIndex < closingIndex
func ptrClob(in graphql.CLOB) *graphql.CLOB {
return &in
}

func (c *converter) isJSON(content []byte) bool {
out := map[string]interface{}{}
err := json.Unmarshal(content, &out)
return err == nil
func ptrSpecResponse(in model.SpecResponse) *model.SpecResponse {
return &in
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ func TestConverter_DetailsToGraphQLCreateInput(t *testing.T) {
"API with directly spec provided in YAML": {
given: model.ServiceDetails{
Api: &model.API{
Spec: json.RawMessage(`openapi: "3.0.0"`),
Spec: ptrSpecResponse(`openapi: "3.0.0"`),
},
},
expected: graphql.BundleCreateInput{
DefaultInstanceAuth: &graphql.AuthInput{},
APIDefinitions: []*graphql.APIDefinitionInput{
{
Spec: &graphql.APISpecInput{
Data: ptrClob(graphql.CLOB(`openapi: "3.0.0"`)),
Data: ptrClob(`openapi: "3.0.0"`),
Type: graphql.APISpecTypeOpenAPI,
Format: graphql.SpecFormatYaml,
},
Expand All @@ -134,15 +134,15 @@ func TestConverter_DetailsToGraphQLCreateInput(t *testing.T) {
"API with directly spec provided in JSON": {
given: model.ServiceDetails{
Api: &model.API{
Spec: json.RawMessage(`{"spec":"v0.0.1"}`),
Spec: ptrSpecResponse(`{"spec":"v0.0.1"}`),
},
},
expected: graphql.BundleCreateInput{
DefaultInstanceAuth: &graphql.AuthInput{},
APIDefinitions: []*graphql.APIDefinitionInput{
{
Spec: &graphql.APISpecInput{
Data: ptrClob(graphql.CLOB(`{"spec":"v0.0.1"}`)),
Data: ptrClob(`{"spec":"v0.0.1"}`),
Type: graphql.APISpecTypeOpenAPI,
Format: graphql.SpecFormatJSON,
},
Expand All @@ -154,15 +154,15 @@ func TestConverter_DetailsToGraphQLCreateInput(t *testing.T) {
"API with directly spec provided in XML": {
given: model.ServiceDetails{
Api: &model.API{
Spec: json.RawMessage(`<spec></spec>"`),
Spec: ptrSpecResponse(`<spec></spec>"`),
},
},
expected: graphql.BundleCreateInput{
DefaultInstanceAuth: &graphql.AuthInput{},
APIDefinitions: []*graphql.APIDefinitionInput{
{
Spec: &graphql.APISpecInput{
Data: ptrClob(graphql.CLOB(`<spec></spec>"`)),
Data: ptrClob(`<spec></spec>"`),
Type: graphql.APISpecTypeOpenAPI,
Format: graphql.SpecFormatXML,
},
Expand Down Expand Up @@ -458,7 +458,7 @@ func TestConverter_DetailsToGraphQLCreateInput(t *testing.T) {
given: model.ServiceDetails{
Name: "foo",
Events: &model.Events{
Spec: json.RawMessage(`asyncapi: "1.2.0"`),
Spec: ptrSpecResponse(`asyncapi: "1.2.0"`),
},
},
expected: graphql.BundleCreateInput{
Expand Down Expand Up @@ -762,15 +762,53 @@ func TestConverter_GraphQLToServiceDetails(t *testing.T) {
Data: []*graphql.EventAPIDefinitionExt{{
Spec: &graphql.EventAPISpecExt{
EventSpec: graphql.EventSpec{
Data: ptrClob(`asyncapi: "1.2.0"`),
Data: ptrClob(`{"asyncapi": "1.2.0"}`),
},
}},
},
},
},
expected: model.ServiceDetails{
Events: &model.Events{
Spec: json.RawMessage(`asyncapi: "1.2.0"`),
Spec: ptrSpecResponse(`{"asyncapi": "1.2.0"}`),
},
Labels: emptyLabels(),
},
},
"events with XML spec": {
given: graphql.BundleExt{
EventDefinitions: graphql.EventAPIDefinitionPageExt{
Data: []*graphql.EventAPIDefinitionExt{{
Spec: &graphql.EventAPISpecExt{
EventSpec: graphql.EventSpec{
Data: ptrClob(`<xml></xml>`),
},
}},
},
},
},
expected: model.ServiceDetails{
Events: &model.Events{
Spec: ptrSpecResponse(`<xml></xml>`),
},
Labels: emptyLabels(),
},
},
"API with XML spec": {
given: graphql.BundleExt{
APIDefinitions: graphql.APIDefinitionPageExt{
Data: []*graphql.APIDefinitionExt{{
Spec: &graphql.APISpecExt{
APISpec: graphql.APISpec{
Data: ptrClob(`<xml></xml>`),
},
}},
},
},
},
expected: model.ServiceDetails{
Api: &model.API{
Spec: ptrSpecResponse(`<xml></xml>`),
},
Labels: emptyLabels(),
},
Expand All @@ -782,6 +820,8 @@ func TestConverter_GraphQLToServiceDetails(t *testing.T) {
// THEN
require.NoError(t, err)
assert.Equal(t, tc.expected, actual)
_, err = json.Marshal(actual)
require.NoError(t, err)
})
}

Expand Down Expand Up @@ -823,7 +863,7 @@ func TestConverter_ServiceDetailsToService(t *testing.T) {
Name: "name",
Description: "description",
ShortDescription: "short description",
Identifier: "identifie",
Identifier: "identifier",
Labels: &map[string]string{"blalb": "blalba"},
}
id := "id"
Expand Down Expand Up @@ -964,3 +1004,7 @@ func ptrString(in string) *string {
func ptrClob(in graphql.CLOB) *graphql.CLOB {
return &in
}

func ptrSpecResponse(in model.SpecResponse) *model.SpecResponse {
return &in
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package service_test

import "github.com/kyma-incubator/compass/components/connectivity-adapter/pkg/model"
import (
"github.com/kyma-incubator/compass/components/connectivity-adapter/pkg/model"
)

func fixAPIOpenAPIYAML() model.API {
spec := `openapi: 3.0.0
Expand Down Expand Up @@ -29,7 +31,7 @@ paths:
type: string`

return model.API{
Spec: []byte(spec),
Spec: ptrSpecResponse(model.SpecResponse(spec)),
}
}

Expand Down Expand Up @@ -104,7 +106,7 @@ func fixAPIOpenAPIJSON() model.API {
}`

return model.API{
Spec: []byte(spec),
Spec: ptrSpecResponse(model.SpecResponse(spec)),
}
}

Expand Down Expand Up @@ -271,7 +273,7 @@ func fixAPIODataXML() model.API {
</edmx:Edmx>`

return model.API{
Spec: []byte(spec),
Spec: ptrSpecResponse(model.SpecResponse(spec)),
ApiType: "odata",
}
}
Expand Down Expand Up @@ -310,7 +312,7 @@ components:
type: string`

return model.Events{
Spec: []byte(spec),
Spec: ptrSpecResponse(model.SpecResponse(spec)),
}
}

Expand Down Expand Up @@ -340,7 +342,7 @@ func fixEventsAsyncAPIJSON() model.Events {
}`

return model.Events{
Spec: []byte(spec),
Spec: ptrSpecResponse(model.SpecResponse(spec)),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestServiceDetailsValidator(t *testing.T) {
Provider: "provider",
Description: "description",
Events: &model.Events{
Spec: eventsRawSpec,
Spec: ptrSpecResponse(model.SpecResponse(eventsRawSpec)),
},
}

Expand All @@ -78,7 +78,7 @@ func TestServiceDetailsValidator(t *testing.T) {
TargetUrl: "http://target.com",
},
Events: &model.Events{
Spec: eventsRawSpec,
Spec: ptrSpecResponse(model.SpecResponse(eventsRawSpec)),
},
}

Expand Down Expand Up @@ -633,3 +633,7 @@ func TestServiceDetailsValidator_Specification_Basic(t *testing.T) {
assert.Equal(t, apperrors.CodeWrongInput, err.Code())
})
}

func ptrSpecResponse(in model.SpecResponse) *model.SpecResponse {
return &in
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"net/http"
"time"

"github.com/kyma-incubator/compass/components/connectivity-adapter/internal/connectorservice/api/middlewares"

"github.com/gorilla/mux"
"github.com/kyma-incubator/compass/components/connectivity-adapter/internal/connectorservice/api"
"github.com/kyma-incubator/compass/components/connectivity-adapter/internal/connectorservice/api/middlewares"
"github.com/kyma-incubator/compass/components/connectivity-adapter/internal/connectorservice/connector"
"github.com/kyma-incubator/compass/components/connectivity-adapter/internal/connectorservice/director"
"github.com/kyma-incubator/compass/components/connectivity-adapter/pkg/model"
Expand Down
Loading

0 comments on commit 1e0e09f

Please sign in to comment.