Skip to content

Commit

Permalink
WIP: Replace occurences of json.Marshal with MarshalNext everywhere p…
Browse files Browse the repository at this point in the history
…ossible (that's probably too aggressive)
  • Loading branch information
apelisse committed Nov 2, 2022
1 parent d03879a commit 0d4820b
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions pkg/aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type DebugSpec struct {
}

func (d DebugSpec) String() string {
bytes, err := json.MarshalIndent(d.Swagger, "", " ")
bytes, err := d.Swagger.MarshalNext()
if err != nil {
return fmt.Sprintf("DebugSpec.String failed: %s", err)
}
Expand Down Expand Up @@ -1762,7 +1762,7 @@ func BenchmarkMergeSpecsIgnorePathConflictsWithKubeSpec(b *testing.B) {
}
}

specBytes, _ := json.Marshal(sp)
specBytes, _ := sp.MarshalNext()
handler.ToProtoBinary(specBytes)

b.StopTimer()
Expand Down Expand Up @@ -2003,7 +2003,7 @@ func TestCloneSpec(t *testing.T) {
}

func cloneSpec(source *spec.Swagger) (*spec.Swagger, error) {
bytes, err := json.Marshal(source)
bytes, err := source.MarshalNext()
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/builder/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,11 @@ func TestBuildOpenAPISpec(t *testing.T) {
if !assert.NoError(err) {
return
}
expected_json, err := json.Marshal(expected)
expected_json, err := expected.MarshalNext()
if !assert.NoError(err) {
return
}
actual_json, err := json.Marshal(swagger)
actual_json, err := swagger.MarshalNext()
if !assert.NoError(err) {
return
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"compress/gzip"
"crypto/sha512"
"encoding/json"
"fmt"
"mime"
"net/http"
Expand Down Expand Up @@ -117,7 +116,7 @@ func (o *OpenAPIService) UpdateSpec(openapiSpec *spec.Swagger) (err error) {
o.rwMutex.Lock()
defer o.rwMutex.Unlock()
o.jsonCache = o.jsonCache.New(func() ([]byte, error) {
return json.Marshal(openapiSpec)
return openapiSpec.MarshalNext()
})
o.protoCache = o.protoCache.New(func() ([]byte, error) {
json, err := o.jsonCache.Get()
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestRegisterOpenAPIVersionedService(t *testing.T) {
t.Errorf("Unexpected error in unmarshalling SwaggerJSON: %v", err)
}

returnedJSON, err := json.Marshal(s)
returnedJSON, err := s.MarshalNext()
if err != nil {
t.Errorf("Unexpected error in preparing returnedJSON: %v", err)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/openapiconv/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
"k8s.io/kube-openapi/pkg/spec3"
"k8s.io/kube-openapi/pkg/validation/spec"
)
Expand Down Expand Up @@ -51,19 +52,19 @@ func TestConvert(t *testing.T) {
t.Fatal(err)
}

openAPIV2JSONBeforeConversion, err := json.Marshal(swaggerSpec)
openAPIV2JSONBeforeConversion, err := swaggerSpec.MarshalNext()
if err != nil {
t.Fatal(err)
}

convertedV3Spec := ConvertV2ToV3(&swaggerSpec)

openAPIV2JSONAfterConversion, err := json.Marshal(swaggerSpec)
openAPIV2JSONAfterConversion, err := swaggerSpec.MarshalNext()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(openAPIV2JSONBeforeConversion, openAPIV2JSONAfterConversion) {
t.Errorf("Expected OpenAPI V2 to be untouched before and after conversion")
t.Errorf("Expected OpenAPI V2 to be untouched before and after conversion: %v", cmp.Diff(openAPIV2JSONBeforeConversion, openAPIV2JSONAfterConversion))
}

spec3JSON, err := ioutil.ReadFile(filepath.Join("testdata_generated_from_k8s/v3_" + tc.groupVersion + ".json"))
Expand Down
6 changes: 3 additions & 3 deletions pkg/schemamutation/walker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestReplaceReferences(t *testing.T) {
}

// find refs
bs, err := json.Marshal(s)
bs, err := s.MarshalNext()
if err != nil {
t.Fatalf("failed to marshal swagger: %v", err)
}
Expand All @@ -244,7 +244,7 @@ func TestReplaceReferences(t *testing.T) {
}
}

origString, err := json.Marshal(s)
origString, err := s.MarshalNext()
if err != nil {
t.Fatalf("failed to marshal swagger: %v", err)
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestReplaceSchema(t *testing.T) {
}

func cloneSwagger(orig *spec.Swagger) (*spec.Swagger, error) {
bs, err := json.Marshal(orig)
bs, err := orig.MarshalNext()
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/validation/spec/gnostic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func gnosticCommonTest(t testing.TB, fuzzer *fuzz.Fuzzer) {
fuzzer.Fuzz(&expected)

// Convert to gnostic via JSON to compare
jsonBytes, err := json.Marshal(expected)
jsonBytes, err := expected.MarshalNext()
require.NoError(t, err)

t.Log("Specimen", string(jsonBytes))
Expand All @@ -59,7 +59,7 @@ func gnosticCommonTest(t testing.TB, fuzzer *fuzz.Fuzzer) {
t.Fatal(cmp.Diff(expected, actual, SwaggerDiffOptions...))
}

newJsonBytes, err := json.Marshal(actual)
newJsonBytes, err := actual.MarshalNext()
require.NoError(t, err)
if !reflect.DeepEqual(jsonBytes, newJsonBytes) {
t.Fatal(cmp.Diff(string(jsonBytes), string(newJsonBytes), SwaggerDiffOptions...))
Expand Down Expand Up @@ -518,19 +518,19 @@ func TestCommonDataLoss(t *testing.T) {

// Make sure that they were exactly the same, except for the data loss
// by checking JSON encodes the some
badConvertedJSON, err := json.Marshal(badConverted)
badConvertedJSON, err := badConverted.MarshalNext()
if err != nil {
t.Error(err)
return
}

fixedConvertedJSON, err := json.Marshal(fixedConverted)
fixedConvertedJSON, err := fixedConverted.MarshalNext()
if err != nil {
t.Error(err)
return
}

fixedDirectJSON, err := json.Marshal(fixedDirect)
fixedDirectJSON, err := fixedDirect.MarshalNext()
if err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -580,13 +580,13 @@ func TestBadStatusCode(t *testing.T) {

// Make sure that they were exactly the same, except for the data loss
// by checking JSON encodes the some
badConvertedJSON, err := json.Marshal(badConverted)
badConvertedJSON, err := badConverted.MarshalNext()
if err != nil {
t.Error(err)
return
}

droppedConvertedJSON, err := json.Marshal(droppedConverted)
droppedConvertedJSON, err := droppedConverted.MarshalNext()
if err != nil {
t.Error(err)
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/validation/spec/swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const specJSON = `{
func TestSwaggerSpec_Serialize(t *testing.T) {
expected := make(map[string]interface{})
_ = json.Unmarshal([]byte(specJSON), &expected)
b, err := json.MarshalIndent(spec, "", " ")
b, err := spec.MarshalNext()
if assert.NoError(t, err) {
var actual map[string]interface{}
err := json.Unmarshal(b, &actual)
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestSwaggerSpec_ExperimentalUnmarshal(t *testing.T) {
internal.UseOptimizedJSONUnmarshaling = true

// Serialize into JSON again
jsonBytesV2, err := json.Marshal(expected)
jsonBytesV2, err := expected.MarshalNext()
require.NoError(t, err)

t.Log("Specimen V2", string(jsonBytes))
Expand Down

0 comments on commit 0d4820b

Please sign in to comment.