Skip to content

Commit

Permalink
[TT-12440] Clean up gojsonschema import surface, phase out internal f…
Browse files Browse the repository at this point in the history
…ork (#6836)

### **User description**
<details open>
<summary><a href="https://tyktech.atlassian.net/browse/TT-12440"
title="TT-12440" target="_blank">TT-12440</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
      <td>Support for latest json-schema version</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Story"
src="https://tyktech.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10315?size=medium"
/>
        Story
      </td>
    </tr>
    <tr>
      <th>Status</th>
      <td>In Dev</td>
    </tr>
    <tr>
      <th>Points</th>
      <td>N/A</td>
    </tr>
    <tr>
      <th>Labels</th>
<td><a
href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%202025lts%20ORDER%20BY%20created%20DESC"
title="2025lts">2025lts</a>, <a
href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20jira_escalated%20ORDER%20BY%20created%20DESC"
title="jira_escalated">jira_escalated</a></td>
    </tr>
  </table>
</details>
<!--
  do not remove this marker as it will break jira-lint's functionality.
  added_by_jira_lint
-->

---

https://tyktech.atlassian.net/browse/TT-12440


___

### **PR Type**
Enhancement, Tests


___

### **Description**
- Replaced `gojsonschema` with internal
`tyk/internal/service/gojsonschema`.

- Updated test cases to use the new schema loader.

- Added a new `clean` task in `Taskfile.yml` for cleaning test files.

- Removed `gojsonschema` dependency from `go.mod` and `go.sum`.


___



### **Changes walkthrough** 📝
<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><details><summary>4
files</summary><table>
<tr>
<td><strong>api_definitions.go</strong><dd><code>Replaced `gojsonschema`
with internal implementation</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
</dd></td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6836/files#diff-9961ccc89a48d32db5b47ba3006315ef52f6e5007fb4b09f8c5d6d299c669d67">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
<td><strong>validator.go</strong><dd><code>Migrated to internal
`gojsonschema` package</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6836/files#diff-462103c9f2f33bbe3bd4a21e46b5614fa0a4bfc3c3774f6c5f2ef858ae3fbb3f">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
<td><strong>api_definition.go</strong><dd><code>Switched to internal
`gojsonschema` package</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6836/files#diff-0cf80174bbafb36f6d4f4308ebbd971b2833b76a936bad568220aa1a4ba0ee8b">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
<td><strong>mw_validate_json.go</strong><dd><code>Replaced
`gojsonschema` with internal implementation</code>&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6836/files#diff-0f0c6b9ac40c5e01908a5b24b1d03111c8d8b4dbc1ddc0251d17c3c1b5328ab5">+1/-7</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

</table></details></td></tr><tr><td><strong>Tests</strong></td><td><details><summary>2
files</summary><table>
<tr>
<td><strong>api_definitions_test.go</strong><dd><code>Updated tests to
use internal `gojsonschema`</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6836/files#diff-6af57c2148f42dce2ee2b93b77d65412024a802ddbd26b63f1d8bd339f4ef760">+7/-7</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
<td><strong>mw_validate_json_test.go</strong><dd><code>Enhanced test
validation with internal schema loader</code>&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; </dd></td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6836/files#diff-76c8aca17cc638d404acc6d77394f55307a795b855f28011fb9fdccfaa7c5250">+9/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></details></td></tr><tr><td><strong>Configuration
changes</strong></td><td><details><summary>1 files</summary><table>
<tr>
<td><strong>Taskfile.yml</strong><dd><code>Added `clean` task for
cleaning test files</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
</dd></td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6836/files#diff-cd2d359855d0301ce190f1ec3b4c572ea690c83747f6df61c9340720e3d2425e">+6/-0</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

</table></details></td></tr><tr><td><strong>Dependencies</strong></td><td><details><summary>2
files</summary><table>
<tr>
<td><strong>go.mod</strong><dd><code>Removed `gojsonschema`
dependency</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6836/files#diff-33ef32bf6c23acb95f5902d7097b7a1d5128ca061167ec0716715b0b9eeaa5f6">+0/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
<td><strong>go.sum</strong><dd><code>Cleaned up `gojsonschema`
references</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6836/files#diff-3295df7234525439d778f1b282d146a4f1ff6b415248aaac074e8042d9f42d63">+0/-2</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></details></td></tr></tr></tbody></table>

___

> 💡 **PR-Agent usage**: Comment `/help "your question"` on any pull
request to receive relevant information

---------

Co-authored-by: Tit Petric <[email protected]>
  • Loading branch information
titpetric and Tit Petric authored Jan 21, 2025
1 parent b7431a0 commit 68a2deb
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 21 deletions.
6 changes: 6 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ tasks:
cmds:
- task: deps:default
- lefthook install

clean:
desc: "Clean test files"
cmds:
- find -name '*.so' -delete -print
- find -name '*.test' -delete -print
2 changes: 1 addition & 1 deletion apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/TykTechnologies/graphql-go-tools/pkg/execution/datasource"

"github.com/TykTechnologies/gojsonschema"
"github.com/TykTechnologies/tyk/internal/service/gojsonschema"

"github.com/TykTechnologies/tyk/regexp"

Expand Down
14 changes: 7 additions & 7 deletions apidef/api_definitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (

"github.com/stretchr/testify/assert"

schema "github.com/xeipuuv/gojsonschema"
"github.com/TykTechnologies/tyk/internal/service/gojsonschema"
)

func TestSchema(t *testing.T) {
schemaLoader := schema.NewBytesLoader([]byte(Schema))
schemaLoader := gojsonschema.NewBytesLoader([]byte(Schema))

spec := DummyAPI()
goLoader := schema.NewGoLoader(spec)
result, err := schema.Validate(schemaLoader, goLoader)
goLoader := gojsonschema.NewGoLoader(spec)
result, err := gojsonschema.Validate(schemaLoader, goLoader)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -97,14 +97,14 @@ func TestDecodeFromDB(t *testing.T) {
}

func TestSchemaGraphqlConfig(t *testing.T) {
schemaLoader := schema.NewBytesLoader([]byte(Schema))
schemaLoader := gojsonschema.NewBytesLoader([]byte(Schema))

spec := DummyAPI()
spec.GraphQL.ExecutionMode = ""

goLoader := schema.NewGoLoader(spec)
goLoader := gojsonschema.NewGoLoader(spec)

result, err := schema.Validate(schemaLoader, goLoader)
result, err := gojsonschema.Validate(schemaLoader, goLoader)
if err != nil {
t.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion apidef/streams/bento/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/hashicorp/go-multierror"

"github.com/TykTechnologies/gojsonschema"
tykerrors "github.com/TykTechnologies/tyk/internal/errors"
"github.com/TykTechnologies/tyk/internal/service/gojsonschema"
)

type ConfigValidator interface {
Expand Down
2 changes: 1 addition & 1 deletion gateway/api_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (

circuit "github.com/TykTechnologies/circuitbreaker"

"github.com/TykTechnologies/gojsonschema"
"github.com/TykTechnologies/tyk/internal/service/gojsonschema"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
Expand Down
8 changes: 1 addition & 7 deletions gateway/mw_validate_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"io"
"net/http"

"github.com/TykTechnologies/gojsonschema"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/internal/service/gojsonschema"
)

type ValidateJSON struct {
Expand Down Expand Up @@ -43,12 +43,6 @@ func (k *ValidateJSON) ProcessRequest(w http.ResponseWriter, r *http.Request, _
return errors.New("no schemas to validate against"), http.StatusInternalServerError
}

if val, exists := vPathMeta.Schema["$schema"]; exists {
if val != "http://json-schema.org/draft-04/schema#" {
return errors.New("unsupported schema, unable to validate"), http.StatusInternalServerError
}
}

nopCloseRequestBody(r)
// Load input body into gojsonschema
bodyBytes, err := io.ReadAll(r.Body)
Expand Down
10 changes: 9 additions & 1 deletion gateway/mw_validate_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"testing"

"github.com/TykTechnologies/tyk/apidef"
Expand Down Expand Up @@ -59,7 +60,14 @@ func TestValidateJSONSchema(t *testing.T) {

_, _ = ts.Run(t, []test.TestCase{
{Method: http.MethodPost, Path: "/without_validation", Data: "{not_valid}", Code: http.StatusOK},
{Method: http.MethodPost, Path: "/v", Data: `{"age":23}`, BodyMatch: `firstName: firstName is required; lastName: lastName is required`, Code: http.StatusUnprocessableEntity},
{Method: http.MethodPost, Path: "/v", Data: `{"age":23}`, BodyMatchFunc: func(b []byte) bool {
var body = string(b)
var result = true
result = result && strings.Contains(body, "firstName is required")
result = result && strings.Contains(body, "lastName is required")
result = result && strings.Contains(body, `"error":`)
return result
}, Code: http.StatusUnprocessableEntity},
{Method: http.MethodPost, Path: "/v", Data: `[]`, BodyMatch: `Expected: object, given: array`, Code: http.StatusUnprocessableEntity},
{Method: http.MethodPost, Path: "/v", Data: `not_json`, Code: http.StatusBadRequest},
{Method: http.MethodPost, Path: "/v", Data: `{"age":23, "firstName": "Harry", "lastName": "Potter"}`, Code: http.StatusOK},
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/TykTechnologies/circuitbreaker v2.2.2+incompatible
github.com/TykTechnologies/drl v0.0.0-20231218155806-88e4363884a2
github.com/TykTechnologies/goautosocket v0.0.0-20190430121222-97bfa5e7e481
github.com/TykTechnologies/gojsonschema v0.0.0-20170222154038-dcb3e4bb7990
github.com/TykTechnologies/gorpc v0.0.0-20241016124253-606484472fbb
github.com/TykTechnologies/goverify v0.0.0-20220808203004-1486f89e7708
github.com/TykTechnologies/graphql-go-tools v1.6.2-0.20241212110213-7724a3b64bb2
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ github.com/TykTechnologies/exp/pkg/limiters v0.0.0-20231219151617-0c4f9315fe5c h
github.com/TykTechnologies/exp/pkg/limiters v0.0.0-20231219151617-0c4f9315fe5c/go.mod h1:7zNtrBmhNOcnBcW7HzjQcwh0HbgslY2VKS4nQI5xmX0=
github.com/TykTechnologies/goautosocket v0.0.0-20190430121222-97bfa5e7e481 h1:fPcSnu5/IBgyqf73GHA99QuwMDbfWH+L4BEX9EZ5kUo=
github.com/TykTechnologies/goautosocket v0.0.0-20190430121222-97bfa5e7e481/go.mod h1:CtF8OunV123VfKa8Z9kKcIPHgcd67hSAwFMLlS7FvS4=
github.com/TykTechnologies/gojsonschema v0.0.0-20170222154038-dcb3e4bb7990 h1:CJRTgg13M3vJG9S7k7kpnvDRMGMywm5OsN6eUE8VwJE=
github.com/TykTechnologies/gojsonschema v0.0.0-20170222154038-dcb3e4bb7990/go.mod h1:SQT0NBrY4/pMikBgwFIrWCjcHBxg015Y8is0kAnMtug=
github.com/TykTechnologies/gorpc v0.0.0-20241016124253-606484472fbb h1:4ZQmRdKvOgE/KSlwT7Ze/imlWaC/z9kynUL4lADCz3Y=
github.com/TykTechnologies/gorpc v0.0.0-20241016124253-606484472fbb/go.mod h1:v6v7Mlj08+EmEcXOfpuTxGt2qYU9yhqqtv4QF9Wf50E=
github.com/TykTechnologies/goverify v0.0.0-20220808203004-1486f89e7708 h1:cmXjlMzcexhc/Cg+QB/c2CPUVs1ux9xn6162qaf/LC4=
Expand Down
12 changes: 12 additions & 0 deletions internal/service/gojsonschema/gojsonschema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package gojsonschema

import (
"github.com/xeipuuv/gojsonschema"
)

type JSONLoader = gojsonschema.JSONLoader
type ResultError = gojsonschema.ResultError

var NewBytesLoader = gojsonschema.NewBytesLoader
var NewGoLoader = gojsonschema.NewGoLoader
var Validate = gojsonschema.Validate

0 comments on commit 68a2deb

Please sign in to comment.