Skip to content

WIP: replaced stdlib encoding/json by jsoniter #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package spec

import (
"sync"

jsoniter "github.com/json-iterator/go"
)

// ResolutionCache a cache for resolving urls
Expand Down Expand Up @@ -71,14 +73,19 @@ var (
resCache *simpleCache
onceCache sync.Once

_ ResolutionCache = &simpleCache{}
_ ResolutionCache = &simpleCache{}
json jsoniter.API
)

// initResolutionCache initializes the URI resolution cache. To be wrapped in a sync.Once.Do call.
func initResolutionCache() {
resCache = defaultResolutionCache()
}

func init() {
json = jsoniter.ConfigFastest
}

func defaultResolutionCache() *simpleCache {
return &simpleCache{store: map[string]interface{}{
"http://swagger.io/v2/schema.json": MustLoadSwagger20Schema(),
Expand Down
1 change: 0 additions & 1 deletion circular_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package spec

import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
Expand Down
2 changes: 0 additions & 2 deletions contact_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package spec

import (
"encoding/json"

"github.com/go-openapi/swag"
)

Expand Down
13 changes: 6 additions & 7 deletions contact_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
package spec

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const contactInfoJSON = `{
"name": "wordnik api team",
"url": "http://developer.wordnik.com",
"email": "[email protected]",
"x-teams": "test team"
"name": "wordnik api team",
"url": "http://developer.wordnik.com",
"email": "[email protected]",
"x-teams": "test team"
}`

var contactInfo = ContactInfo{ContactInfoProps: ContactInfoProps{
Expand All @@ -36,9 +35,9 @@ var contactInfo = ContactInfo{ContactInfoProps: ContactInfoProps{
}, VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-teams": "test team"}}}

func TestIntegrationContactInfo(t *testing.T) {
b, err := json.MarshalIndent(contactInfo, "", "\t")
b, err := json.MarshalIndent(contactInfo, "", " ")
require.NoError(t, err)
assert.Equal(t, contactInfoJSON, string(b))
assert.JSONEq(t, contactInfoJSON, string(b))

actual := ContactInfo{}
err = json.Unmarshal([]byte(contactInfoJSON), &actual)
Expand Down
12 changes: 6 additions & 6 deletions expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package spec

import (
"encoding/json"
stdjson "encoding/json"
"fmt"
)

Expand All @@ -28,11 +28,11 @@ import (
//
// PathLoader injects a document loading method. By default, this resolves to the function provided by the SpecLoader package variable.
type ExpandOptions struct {
RelativeBase string // the path to the root document to expand. This is a file, not a directory
SkipSchemas bool // do not expand schemas, just paths, parameters and responses
ContinueOnError bool // continue expanding even after and error is found
PathLoader func(string) (json.RawMessage, error) `json:"-"` // the document loading method that takes a path as input and yields a json document
AbsoluteCircularRef bool // circular $ref remaining after expansion remain absolute URLs
RelativeBase string // the path to the root document to expand. This is a file, not a directory
SkipSchemas bool // do not expand schemas, just paths, parameters and responses
ContinueOnError bool // continue expanding even after and error is found
PathLoader func(string) (stdjson.RawMessage, error) `json:"-"` // the document loading method that takes a path as input and yields a json document
AbsoluteCircularRef bool // circular $ref remaining after expansion remain absolute URLs
}

func optionsOrDefault(opts *ExpandOptions) *ExpandOptions {
Expand Down
4 changes: 2 additions & 2 deletions expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package spec

import (
"encoding/json"
stdjson "encoding/json"
"io"
"log"
"net/http"
Expand All @@ -38,7 +38,7 @@ const (

var (
// PetStoreJSONMessage json raw message for Petstore20
PetStoreJSONMessage = json.RawMessage([]byte(PetStore20))
PetStoreJSONMessage = stdjson.RawMessage([]byte(PetStore20))
specs = filepath.Join("fixtures", "specs")
)

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require (
github.com/go-openapi/jsonpointer v0.20.2
github.com/go-openapi/jsonreference v0.20.4
github.com/go-openapi/swag v0.22.6
github.com/json-iterator/go v1.1.12
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -12,6 +13,8 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)

Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
Expand All @@ -6,15 +7,25 @@ github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdX
github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4=
github.com/go-openapi/swag v0.22.6 h1:dnqg1XfHXL9aBxSbktBqFR5CxVyVI+7fYWhAf1JOeTw=
github.com/go-openapi/swag v0.22.6/go.mod h1:Gl91UqO+btAM0plGGxHqJcQZ1ZTy6jbmridBTsDy8A0=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
1 change: 0 additions & 1 deletion header.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"strings"

"github.com/go-openapi/jsonpointer"
Expand Down
1 change: 0 additions & 1 deletion header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"testing"

"github.com/go-openapi/swag"
Expand Down
6 changes: 3 additions & 3 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package spec

import (
"encoding/json"
stdjson "encoding/json"
"fmt"
"regexp"
"strings"
Expand All @@ -14,12 +14,12 @@ import (

var rex = regexp.MustCompile(`"\$ref":\s*"(.*?)"`)

func jsonDoc(path string) (json.RawMessage, error) {
func jsonDoc(path string) (stdjson.RawMessage, error) {
data, err := swag.LoadFromFileOrHTTP(path)
if err != nil {
return nil, err
}
return json.RawMessage(data), nil
return stdjson.RawMessage(data), nil
}

func docAndOpts(t testing.TB, fixturePath string) ([]byte, *ExpandOptions) {
Expand Down
1 change: 0 additions & 1 deletion info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"strconv"
"strings"

Expand Down
5 changes: 2 additions & 3 deletions info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -57,9 +56,9 @@ var info = Info{
}

func TestIntegrationInfo_Serialize(t *testing.T) {
b, err := json.MarshalIndent(info, "", "\t")
b, err := json.MarshalIndent(info, "", " ")
require.NoError(t, err)
assert.Equal(t, infoJSON, string(b))
assert.JSONEq(t, infoJSON, string(b))
}

func TestIntegrationInfo_Deserialize(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion items.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"strings"

"github.com/go-openapi/jsonpointer"
Expand Down
1 change: 0 additions & 1 deletion items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"testing"

"github.com/go-openapi/swag"
Expand Down
2 changes: 0 additions & 2 deletions license.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package spec

import (
"encoding/json"

"github.com/go-openapi/swag"
)

Expand Down
5 changes: 2 additions & 3 deletions license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -36,9 +35,9 @@ func TestIntegrationLicense(t *testing.T) {

// const licenseYAML = "name: the name\nurl: the url\n"

b, err := json.MarshalIndent(license, "", "\t")
b, err := json.MarshalIndent(license, "", " ")
require.NoError(t, err)
assert.Equal(t, licenseJSON, string(b))
assert.JSONEq(t, licenseJSON, string(b))

actual := License{}
err = json.Unmarshal([]byte(licenseJSON), &actual)
Expand Down
1 change: 0 additions & 1 deletion operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package spec
import (
"bytes"
"encoding/gob"
"encoding/json"
"sort"

"github.com/go-openapi/jsonpointer"
Expand Down
1 change: 0 additions & 1 deletion operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package spec
import (
"bytes"
"encoding/gob"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand Down
1 change: 0 additions & 1 deletion parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"strings"

"github.com/go-openapi/jsonpointer"
Expand Down
1 change: 0 additions & 1 deletion parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"testing"

"github.com/go-openapi/swag"
Expand Down
2 changes: 0 additions & 2 deletions path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package spec

import (
"encoding/json"

"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
)
Expand Down
1 change: 0 additions & 1 deletion path_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand Down
5 changes: 3 additions & 2 deletions paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
package spec

import (
"encoding/json"
"fmt"
"strings"

stdjson "encoding/json"

"github.com/go-openapi/swag"
)

Expand Down Expand Up @@ -46,7 +47,7 @@ func (p Paths) JSONLookup(token string) (interface{}, error) {

// UnmarshalJSON hydrates this items instance with the data from JSON
func (p *Paths) UnmarshalJSON(data []byte) error {
var res map[string]json.RawMessage
var res map[string]stdjson.RawMessage
if err := json.Unmarshal(data, &res); err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand Down
1 change: 0 additions & 1 deletion properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package spec

import (
"bytes"
"encoding/json"
"reflect"
"sort"
)
Expand Down
1 change: 0 additions & 1 deletion ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package spec
import (
"bytes"
"encoding/gob"
"encoding/json"
"net/http"
"os"
"path/filepath"
Expand Down
1 change: 0 additions & 1 deletion ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package spec
import (
"bytes"
"encoding/gob"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand Down
1 change: 0 additions & 1 deletion resolver_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package spec

import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
Expand Down
Loading