From 5c7759fe5719d69cf901610eef0babf5754520da Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Fri, 25 Oct 2019 06:20:40 +0200 Subject: [PATCH 1/3] replaced stdlib encoding/json by ccy/go-json Signed-off-by: Frederic BIDON --- circular_test.go | 2 +- contact_info.go | 3 +-- contact_info_test.go | 14 +++++++------- expander.go | 3 ++- expander_test.go | 2 +- go.mod | 1 + go.sum | 2 ++ header.go | 2 +- header_test.go | 2 +- helpers_spec_test.go | 2 +- helpers_test.go | 2 +- info.go | 2 +- info_test.go | 6 +++--- items.go | 2 +- items_test.go | 2 +- license.go | 3 +-- license_test.go | 6 +++--- operation.go | 2 +- operation_test.go | 2 +- parameter.go | 2 +- parameters_test.go | 2 +- path_item.go | 3 +-- path_item_test.go | 2 +- paths.go | 2 +- paths_test.go | 2 +- properties.go | 3 ++- ref.go | 2 +- ref_test.go | 2 +- resolver_test.go | 2 +- response.go | 3 +-- response_test.go | 2 +- responses.go | 2 +- responses_test.go | 2 +- schema.go | 2 +- schema_loader.go | 7 ++++++- schema_loader_test.go | 2 +- schema_test.go | 2 +- security_scheme.go | 3 +-- spec.go | 8 ++++---- structs_test.go | 2 +- swagger.go | 2 +- swagger_test.go | 2 +- tag.go | 3 +-- xml_object_test.go | 2 +- 44 files changed, 65 insertions(+), 61 deletions(-) diff --git a/circular_test.go b/circular_test.go index c064b61..915477f 100644 --- a/circular_test.go +++ b/circular_test.go @@ -1,7 +1,6 @@ package spec import ( - "encoding/json" "net/http" "net/http/httptest" "os" @@ -9,6 +8,7 @@ import ( "testing" "time" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/contact_info.go b/contact_info.go index 2f7bb21..68bf4f1 100644 --- a/contact_info.go +++ b/contact_info.go @@ -15,9 +15,8 @@ package spec import ( - "encoding/json" - "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // ContactInfo contact information for the exposed API. diff --git a/contact_info_test.go b/contact_info_test.go index fedd81a..3179f87 100644 --- a/contact_info_test.go +++ b/contact_info_test.go @@ -15,18 +15,18 @@ package spec import ( - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) const contactInfoJSON = `{ - "name": "wordnik api team", - "url": "http://developer.wordnik.com", - "email": "some@mailayada.dkdkd", - "x-teams": "test team" + "name": "wordnik api team", + "url": "http://developer.wordnik.com", + "email": "some@mailayada.dkdkd", + "x-teams": "test team" }` var contactInfo = ContactInfo{ContactInfoProps: ContactInfoProps{ @@ -36,9 +36,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) diff --git a/expander.go b/expander.go index 74f91c3..658d0e6 100644 --- a/expander.go +++ b/expander.go @@ -15,8 +15,9 @@ package spec import ( - "encoding/json" "fmt" + + json "github.com/goccy/go-json" ) // ExpandOptions provides options for the spec expander. diff --git a/expander_test.go b/expander_test.go index 1ba1aac..cd59acb 100644 --- a/expander_test.go +++ b/expander_test.go @@ -15,7 +15,6 @@ package spec import ( - "encoding/json" "io" "log" "net/http" @@ -24,6 +23,7 @@ import ( "path/filepath" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/go.mod b/go.mod index 898a3e9..0bddb90 100644 --- a/go.mod +++ b/go.mod @@ -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/goccy/go-json v0.10.2 github.com/stretchr/testify v1.8.4 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 3d2ef0b..82738e8 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ 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/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= 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/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= diff --git a/header.go b/header.go index 9dfd17b..6340c38 100644 --- a/header.go +++ b/header.go @@ -15,11 +15,11 @@ package spec import ( - "encoding/json" "strings" "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) const ( diff --git a/header_test.go b/header_test.go index 67a893a..2a1658b 100644 --- a/header_test.go +++ b/header_test.go @@ -15,10 +15,10 @@ package spec import ( - "encoding/json" "testing" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/helpers_spec_test.go b/helpers_spec_test.go index 4dd12c5..5f75290 100644 --- a/helpers_spec_test.go +++ b/helpers_spec_test.go @@ -1,7 +1,6 @@ package spec_test import ( - "encoding/json" "fmt" "regexp" "strings" @@ -9,6 +8,7 @@ import ( "github.com/go-openapi/spec" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/helpers_test.go b/helpers_test.go index f16a4dd..3f2a5c7 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -1,13 +1,13 @@ package spec import ( - "encoding/json" "fmt" "regexp" "strings" "testing" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/info.go b/info.go index 582f0fd..b9f1a2b 100644 --- a/info.go +++ b/info.go @@ -15,12 +15,12 @@ package spec import ( - "encoding/json" "strconv" "strings" "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // Extensions vendor specific extensions diff --git a/info_test.go b/info_test.go index a8e3fbe..183cb9e 100644 --- a/info_test.go +++ b/info_test.go @@ -15,9 +15,9 @@ package spec import ( - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -57,9 +57,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) { diff --git a/items.go b/items.go index e2afb21..b071b13 100644 --- a/items.go +++ b/items.go @@ -15,11 +15,11 @@ package spec import ( - "encoding/json" "strings" "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) const ( diff --git a/items_test.go b/items_test.go index 4d4e458..043bbd5 100644 --- a/items_test.go +++ b/items_test.go @@ -15,10 +15,10 @@ package spec import ( - "encoding/json" "testing" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/license.go b/license.go index b42f803..fd294cf 100644 --- a/license.go +++ b/license.go @@ -15,9 +15,8 @@ package spec import ( - "encoding/json" - "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // License information for the exposed API. diff --git a/license_test.go b/license_test.go index 398f0d8..40b8bb5 100644 --- a/license_test.go +++ b/license_test.go @@ -15,9 +15,9 @@ package spec import ( - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -36,9 +36,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) diff --git a/operation.go b/operation.go index a69cca8..6aedabd 100644 --- a/operation.go +++ b/operation.go @@ -17,11 +17,11 @@ package spec import ( "bytes" "encoding/gob" - "encoding/json" "sort" "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) func init() { diff --git a/operation_test.go b/operation_test.go index 495f609..a14c5d5 100644 --- a/operation_test.go +++ b/operation_test.go @@ -17,9 +17,9 @@ package spec import ( "bytes" "encoding/gob" - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/parameter.go b/parameter.go index bd4f1cd..e5fec5f 100644 --- a/parameter.go +++ b/parameter.go @@ -15,11 +15,11 @@ package spec import ( - "encoding/json" "strings" "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // QueryParam creates a query parameter diff --git a/parameters_test.go b/parameters_test.go index 702d867..623b3aa 100644 --- a/parameters_test.go +++ b/parameters_test.go @@ -15,10 +15,10 @@ package spec import ( - "encoding/json" "testing" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/path_item.go b/path_item.go index 68fc8e9..21be1fe 100644 --- a/path_item.go +++ b/path_item.go @@ -15,10 +15,9 @@ package spec import ( - "encoding/json" - "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // PathItemProps the path item specific properties diff --git a/path_item_test.go b/path_item_test.go index c3507a0..fdaafdd 100644 --- a/path_item_test.go +++ b/path_item_test.go @@ -15,9 +15,9 @@ package spec import ( - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/paths.go b/paths.go index 9dc82a2..41472b7 100644 --- a/paths.go +++ b/paths.go @@ -15,11 +15,11 @@ package spec import ( - "encoding/json" "fmt" "strings" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // Paths holds the relative paths to the individual endpoints. diff --git a/paths_test.go b/paths_test.go index 4592818..b1ee023 100644 --- a/paths_test.go +++ b/paths_test.go @@ -15,9 +15,9 @@ package spec import ( - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/properties.go b/properties.go index 91d2435..81871a4 100644 --- a/properties.go +++ b/properties.go @@ -2,9 +2,10 @@ package spec import ( "bytes" - "encoding/json" "reflect" "sort" + + json "github.com/goccy/go-json" ) // OrderSchemaItem holds a named schema (e.g. from a property of an object) diff --git a/ref.go b/ref.go index b0ef9bd..eadf635 100644 --- a/ref.go +++ b/ref.go @@ -17,12 +17,12 @@ package spec import ( "bytes" "encoding/gob" - "encoding/json" "net/http" "os" "path/filepath" "github.com/go-openapi/jsonreference" + json "github.com/goccy/go-json" ) // Refable is a struct for things that accept a $ref property diff --git a/ref_test.go b/ref_test.go index 6f4c0de..8f47653 100644 --- a/ref_test.go +++ b/ref_test.go @@ -17,9 +17,9 @@ package spec import ( "bytes" "encoding/gob" - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/resolver_test.go b/resolver_test.go index b1c977a..7a3e143 100644 --- a/resolver_test.go +++ b/resolver_test.go @@ -1,7 +1,6 @@ package spec import ( - "encoding/json" "net/http" "net/http/httptest" "os" @@ -9,6 +8,7 @@ import ( "testing" "github.com/go-openapi/jsonpointer" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/response.go b/response.go index 0340b60..06dd2ae 100644 --- a/response.go +++ b/response.go @@ -15,10 +15,9 @@ package spec import ( - "encoding/json" - "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // ResponseProps properties specific to a response diff --git a/response_test.go b/response_test.go index d03720c..640f4bc 100644 --- a/response_test.go +++ b/response_test.go @@ -15,9 +15,9 @@ package spec import ( - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/responses.go b/responses.go index 16c3076..ecc5aec 100644 --- a/responses.go +++ b/responses.go @@ -15,13 +15,13 @@ package spec import ( - "encoding/json" "fmt" "reflect" "strconv" "strings" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // Responses is a container for the expected responses of an operation. diff --git a/responses_test.go b/responses_test.go index fc626bf..dff05f6 100644 --- a/responses_test.go +++ b/responses_test.go @@ -15,9 +15,9 @@ package spec import ( - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/schema.go b/schema.go index 4e9be85..6936ad6 100644 --- a/schema.go +++ b/schema.go @@ -15,12 +15,12 @@ package spec import ( - "encoding/json" "fmt" "strings" "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // BooleanProperty creates a boolean property diff --git a/schema_loader.go b/schema_loader.go index 0059b99..6662823 100644 --- a/schema_loader.go +++ b/schema_loader.go @@ -15,7 +15,6 @@ package spec import ( - "encoding/json" "fmt" "log" "net/url" @@ -23,6 +22,7 @@ import ( "strings" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // PathLoader is a function to use when loading remote refs. @@ -77,6 +77,7 @@ type schemaLoader struct { options *ExpandOptions cache ResolutionCache context *resolverContext + loadDoc func(string) (json.RawMessage, error) } func (r *schemaLoader) transitiveResolver(basePath string, ref Ref) *schemaLoader { @@ -327,5 +328,9 @@ func defaultSchemaLoader( options: expandOptions, cache: cache, context: context, + loadDoc: func(path string) (json.RawMessage, error) { + debugLog("fetching document at %q", path) + return PathLoader(path) + }, } } diff --git a/schema_loader_test.go b/schema_loader_test.go index 67a1e79..d2e45d8 100644 --- a/schema_loader_test.go +++ b/schema_loader_test.go @@ -1,10 +1,10 @@ package spec import ( - "encoding/json" "path/filepath" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/require" ) diff --git a/schema_test.go b/schema_test.go index 038284c..c17a8e8 100644 --- a/schema_test.go +++ b/schema_test.go @@ -15,10 +15,10 @@ package spec import ( - "encoding/json" "testing" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/security_scheme.go b/security_scheme.go index 9d0bdae..81481dc 100644 --- a/security_scheme.go +++ b/security_scheme.go @@ -15,10 +15,9 @@ package spec import ( - "encoding/json" - "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) const ( diff --git a/spec.go b/spec.go index 876aa12..c44a717 100644 --- a/spec.go +++ b/spec.go @@ -14,15 +14,15 @@ package spec -import ( - "encoding/json" -) - //go:generate curl -L --progress -o ./schemas/v2/schema.json http://swagger.io/v2/schema.json //go:generate curl -L --progress -o ./schemas/jsonschema-draft-04.json http://json-schema.org/draft-04/schema //go:generate go-bindata -pkg=spec -prefix=./schemas -ignore=.*\.md ./schemas/... //go:generate perl -pi -e s,Json,JSON,g bindata.go +import ( + json "github.com/goccy/go-json" +) + const ( // SwaggerSchemaURL the url for the swagger 2.0 schema to validate specs SwaggerSchemaURL = "http://swagger.io/v2/schema.json#" diff --git a/structs_test.go b/structs_test.go index c8e191a..f5de6d4 100644 --- a/structs_test.go +++ b/structs_test.go @@ -15,10 +15,10 @@ package spec import ( - "encoding/json" "reflect" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" ) diff --git a/swagger.go b/swagger.go index 1590fd1..6294253 100644 --- a/swagger.go +++ b/swagger.go @@ -17,12 +17,12 @@ package spec import ( "bytes" "encoding/gob" - "encoding/json" "fmt" "strconv" "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // Swagger this is the root document object for the API specification. diff --git a/swagger_test.go b/swagger_test.go index cb288e6..abbae7e 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -15,9 +15,9 @@ package spec import ( - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/tag.go b/tag.go index faa3d3d..70ad299 100644 --- a/tag.go +++ b/tag.go @@ -15,10 +15,9 @@ package spec import ( - "encoding/json" - "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" + json "github.com/goccy/go-json" ) // TagProps describe a tag entry in the top level tags section of a swagger spec diff --git a/xml_object_test.go b/xml_object_test.go index 8573e82..0faed0b 100644 --- a/xml_object_test.go +++ b/xml_object_test.go @@ -15,9 +15,9 @@ package spec import ( - "encoding/json" "testing" + json "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) From d8542b1563f84b982cedaa84e70fde6e242e2ee1 Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Wed, 10 Jan 2024 15:30:01 +0100 Subject: [PATCH 2/3] added pool for internal maps Signed-off-by: Frederic BIDON --- schema.go | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/schema.go b/schema.go index 6936ad6..92e71c3 100644 --- a/schema.go +++ b/schema.go @@ -17,6 +17,7 @@ package spec import ( "fmt" "strings" + "sync" "github.com/go-openapi/jsonpointer" "github.com/go-openapi/swag" @@ -131,7 +132,10 @@ func (r SchemaURL) MarshalJSON() ([]byte, error) { // UnmarshalJSON unmarshal this from JSON func (r *SchemaURL) UnmarshalJSON(data []byte) error { - var v map[string]interface{} + v := poolOfMaps.BorrowMap() + defer func() { + poolOfMaps.RedeemMap(v) + }() if err := json.Unmarshal(data, &v); err != nil { return err } @@ -610,7 +614,11 @@ func (s *Schema) UnmarshalJSON(data []byte) error { SwaggerSchemaProps: props.SwaggerSchemaProps, } - var d map[string]interface{} + //var d map[string]interface{} + d := poolOfMaps.BorrowMap() + defer func() { + poolOfMaps.RedeemMap(d) + }() if err := json.Unmarshal(data, &d); err != nil { return err } @@ -643,3 +651,29 @@ func (s *Schema) UnmarshalJSON(data []byte) error { return nil } + +type mapPool struct { + *sync.Pool +} + +var poolOfMaps = mapPool{ + Pool: &sync.Pool{ + New: func() any { + return make(map[string]any) + }, + }, +} + +func (p mapPool) BorrowMap() map[string]any { + m := p.Get().(map[string]any) + // go1.21 clear(m) + for k := range m { + delete(m, k) + } + + return m +} + +func (p mapPool) RedeemMap(m map[string]any) { + p.Put(m) +} From b5f844b2ece08e30592b926eeb22729e60c74f41 Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Wed, 10 Jan 2024 18:44:57 +0100 Subject: [PATCH 3/3] more pooling Signed-off-by: Frederic BIDON --- info.go | 5 ++++- ref.go | 9 ++++++--- responses.go | 2 +- schema.go | 1 - 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/info.go b/info.go index b9f1a2b..19ea9c4 100644 --- a/info.go +++ b/info.go @@ -118,7 +118,10 @@ func (v VendorExtensible) MarshalJSON() ([]byte, error) { // UnmarshalJSON for this extensible object func (v *VendorExtensible) UnmarshalJSON(data []byte) error { - var d map[string]interface{} + d := poolOfMaps.BorrowMap() + defer func() { + poolOfMaps.RedeemMap(d) + }() if err := json.Unmarshal(data, &d); err != nil { return err } diff --git a/ref.go b/ref.go index eadf635..96fa345 100644 --- a/ref.go +++ b/ref.go @@ -145,7 +145,10 @@ func (r Ref) MarshalJSON() ([]byte, error) { // UnmarshalJSON unmarshals this ref from a JSON object func (r *Ref) UnmarshalJSON(d []byte) error { - var v map[string]interface{} + v := poolOfMaps.BorrowMap() + defer func() { + poolOfMaps.RedeemMap(v) + }() if err := json.Unmarshal(d, &v); err != nil { return err } @@ -154,7 +157,7 @@ func (r *Ref) UnmarshalJSON(d []byte) error { // GobEncode provides a safe gob encoder for Ref func (r Ref) GobEncode() ([]byte, error) { - var b bytes.Buffer + var b bytes.Buffer // TODO: grow raw, err := r.MarshalJSON() if err != nil { return nil, err @@ -165,7 +168,7 @@ func (r Ref) GobEncode() ([]byte, error) { // GobDecode provides a safe gob decoder for Ref func (r *Ref) GobDecode(b []byte) error { - var raw []byte + var raw []byte // TODO buf := bytes.NewBuffer(b) err := gob.NewDecoder(buf).Decode(&raw) if err != nil { diff --git a/responses.go b/responses.go index ecc5aec..8a67116 100644 --- a/responses.go +++ b/responses.go @@ -97,7 +97,7 @@ type ResponsesProps struct { // MarshalJSON marshals responses as JSON func (r ResponsesProps) MarshalJSON() ([]byte, error) { - toser := map[string]Response{} + toser := make(map[string]Response, len(r.StatusCodeResponses)+1) if r.Default != nil { toser["default"] = *r.Default } diff --git a/schema.go b/schema.go index 92e71c3..f845f7a 100644 --- a/schema.go +++ b/schema.go @@ -614,7 +614,6 @@ func (s *Schema) UnmarshalJSON(data []byte) error { SwaggerSchemaProps: props.SwaggerSchemaProps, } - //var d map[string]interface{} d := poolOfMaps.BorrowMap() defer func() { poolOfMaps.RedeemMap(d)