diff --git a/go.mod b/go.mod index 59c837d1d92..25d39867bb6 100644 --- a/go.mod +++ b/go.mod @@ -13,8 +13,7 @@ require ( google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd google.golang.org/grpc v1.46.2 google.golang.org/protobuf v1.28.0 - gopkg.in/yaml.v2 v2.4.0 - sigs.k8s.io/yaml v1.3.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( diff --git a/go.sum b/go.sum index 1feee5cd1dd..9e6d0199673 100644 --- a/go.sum +++ b/go.sum @@ -48,8 +48,6 @@ github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWH github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= 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/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -410,9 +408,9 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= @@ -423,5 +421,3 @@ honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9 rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= -sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= -sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= diff --git a/internal/descriptor/BUILD.bazel b/internal/descriptor/BUILD.bazel index bd79674baa2..04553bc1a05 100644 --- a/internal/descriptor/BUILD.bazel +++ b/internal/descriptor/BUILD.bazel @@ -21,7 +21,7 @@ go_library( "//protoc-gen-openapiv2/options", "@com_github_golang_glog//:glog", "@go_googleapis//google/api:annotations_go_proto", - "@io_k8s_sigs_yaml//:yaml", + "@in_gopkg_yaml_v3//:yaml_v3", "@org_golang_google_protobuf//compiler/protogen", "@org_golang_google_protobuf//encoding/protojson", "@org_golang_google_protobuf//proto", diff --git a/internal/descriptor/grpc_api_configuration.go b/internal/descriptor/grpc_api_configuration.go index 2e90e46a1a0..a3ae3b83ad1 100644 --- a/internal/descriptor/grpc_api_configuration.go +++ b/internal/descriptor/grpc_api_configuration.go @@ -1,19 +1,26 @@ package descriptor import ( + "encoding/json" "fmt" "io/ioutil" "strings" "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor/apiconfig" "google.golang.org/protobuf/encoding/protojson" - "sigs.k8s.io/yaml" + "gopkg.in/yaml.v3" ) func loadGrpcAPIServiceFromYAML(yamlFileContents []byte, yamlSourceLogName string) (*apiconfig.GrpcAPIService, error) { - jsonContents, err := yaml.YAMLToJSON(yamlFileContents) + var yamlContents interface{} + err := yaml.Unmarshal(yamlFileContents, &yamlContents) if err != nil { - return nil, fmt.Errorf("failed to convert gRPC API Configuration from YAML in '%v' to JSON: %v", yamlSourceLogName, err) + return nil, fmt.Errorf("failed to parse gRPC API Configuration from YAML in '%v': %v", yamlSourceLogName, err) + } + + jsonContents, err := json.Marshal(yamlContents) + if err != nil { + return nil, err } // As our GrpcAPIService is incomplete, accept unknown fields. diff --git a/internal/descriptor/grpc_api_configuration_test.go b/internal/descriptor/grpc_api_configuration_test.go index 09cf0a80c53..4d8231ec35c 100644 --- a/internal/descriptor/grpc_api_configuration_test.go +++ b/internal/descriptor/grpc_api_configuration_test.go @@ -67,8 +67,8 @@ http: t.Fatal(err) } - if !strings.Contains(err.Error(), "line 7") { - t.Errorf("Expected yaml error to be detected in line 7. Got other error: %v", err) + if !strings.Contains(err.Error(), "line 6") { + t.Errorf("Expected yaml error to be detected in line 6. Got other error: %v", err) } if service != nil { @@ -147,3 +147,42 @@ http: t.Errorf("some.other.service has %v additional bindings when it should not have any. Got: %v", len(second.GetAdditionalBindings()), second.GetAdditionalBindings()) } } + +func TestLoadGrpcAPIServiceFromYAMLUnknownKeys(t *testing.T) { + service, err := loadGrpcAPIServiceFromYAML([]byte(` +type: google.api.Service +config_version: 3 + +very: key +much: 1 + +http: + rules: + - selector: some.other.service + delete: foo + invalidkey: yes +`), "example") + if err != nil { + t.Fatalf("Failed to load service description from YAML: %v", err) + } + + if service == nil { + t.Fatal("No service returned") + } + + if service.Http == nil { + t.Fatal("HTTP is empty") + } + + if len(service.Http.GetRules()) != 1 { + t.Fatalf("%v service(s) returned when two were expected. Got: %v", len(service.Http.GetRules()), service.Http) + } + + first := service.Http.GetRules()[0] + if first.GetSelector() != "some.other.service" { + t.Errorf("first.selector has unexpected selector '%v'", first.GetSelector()) + } + if first.GetDelete() != "foo" { + t.Errorf("first.selector has unexpected delete '%v'", first.GetPost()) + } +} diff --git a/internal/descriptor/openapi_configuration.go b/internal/descriptor/openapi_configuration.go index d5b4934f8c5..3487c06d64b 100644 --- a/internal/descriptor/openapi_configuration.go +++ b/internal/descriptor/openapi_configuration.go @@ -1,18 +1,25 @@ package descriptor import ( + "encoding/json" "fmt" "io/ioutil" "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor/openapiconfig" "google.golang.org/protobuf/encoding/protojson" - "sigs.k8s.io/yaml" + "gopkg.in/yaml.v3" ) func loadOpenAPIConfigFromYAML(yamlFileContents []byte, yamlSourceLogName string) (*openapiconfig.OpenAPIConfig, error) { - jsonContents, err := yaml.YAMLToJSON(yamlFileContents) + var yamlContents interface{} + err := yaml.Unmarshal(yamlFileContents, &yamlContents) if err != nil { - return nil, fmt.Errorf("failed to convert OpenAPI Configuration from YAML in '%v' to JSON: %v", yamlSourceLogName, err) + return nil, fmt.Errorf("failed to parse gRPC API Configuration from YAML in '%v': %v", yamlSourceLogName, err) + } + + jsonContents, err := json.Marshal(yamlContents) + if err != nil { + return nil, err } // Reject unknown fields because OpenAPIConfig is only used here diff --git a/internal/descriptor/openapi_configuration_test.go b/internal/descriptor/openapi_configuration_test.go index 977d7e94e1c..94953c859fe 100644 --- a/internal/descriptor/openapi_configuration_test.go +++ b/internal/descriptor/openapi_configuration_test.go @@ -28,8 +28,8 @@ file: t.Fatal(err) } - if !strings.Contains(err.Error(), "line 4") { - t.Errorf("Expected yaml error to be detected in line 4. Got other error: %v", err) + if !strings.Contains(err.Error(), "line 3") { + t.Errorf("Expected yaml error to be detected in line 3. Got other error: %v", err) } if config != nil { @@ -112,3 +112,20 @@ openapiOptions: t.Fatalf("expected name to be X-API-Key but got %s", secOpt.Name) } } + +func TestLoadOpenAPIConfigFromYAMLUnknownKeys(t *testing.T) { + _, err := loadOpenAPIConfigFromYAML([]byte(` +closedapiOptions: + get: it? +openapiOptions: + file: + - file: test.proto + option: + schemes: + - HTTP +`), "openapi_options") + if err == nil { + t.Errorf("Expected invalid key error") + } + +} diff --git a/protoc-gen-openapiv2/internal/genopenapi/BUILD.bazel b/protoc-gen-openapiv2/internal/genopenapi/BUILD.bazel index 3ba883314d8..20bda429d9d 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/BUILD.bazel +++ b/protoc-gen-openapiv2/internal/genopenapi/BUILD.bazel @@ -25,7 +25,7 @@ go_library( "@go_googleapis//google/api:annotations_go_proto", "@go_googleapis//google/api:visibility_go_proto", "@go_googleapis//google/rpc:status_go_proto", - "@in_gopkg_yaml_v2//:yaml_v2", + "@in_gopkg_yaml_v3//:yaml_v3", "@io_bazel_rules_go//proto/wkt:any_go_proto", "@org_golang_google_protobuf//encoding/protojson", "@org_golang_google_protobuf//proto", @@ -58,7 +58,7 @@ go_test( "@com_github_google_go_cmp//cmp", "@go_googleapis//google/api:annotations_go_proto", "@go_googleapis//google/api:visibility_go_proto", - "@in_gopkg_yaml_v2//:yaml_v2", + "@in_gopkg_yaml_v3//:yaml_v3", "@io_bazel_rules_go//proto/wkt:field_mask_go_proto", "@org_golang_google_protobuf//proto", "@org_golang_google_protobuf//reflect/protodesc", diff --git a/protoc-gen-openapiv2/internal/genopenapi/format.go b/protoc-gen-openapiv2/internal/genopenapi/format.go index e957accc933..52c932e2e85 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/format.go +++ b/protoc-gen-openapiv2/internal/genopenapi/format.go @@ -5,7 +5,7 @@ import ( "errors" "io" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" ) type Format string diff --git a/protoc-gen-openapiv2/internal/genopenapi/format_test.go b/protoc-gen-openapiv2/internal/genopenapi/format_test.go index 9c3682c3d98..564aea5d214 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/format_test.go +++ b/protoc-gen-openapiv2/internal/genopenapi/format_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopenapi" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" ) func TestFormatValidate(t *testing.T) { diff --git a/protoc-gen-openapiv2/internal/genopenapi/generator_test.go b/protoc-gen-openapiv2/internal/genopenapi/generator_test.go index b7ca09dff08..5df383c9b8c 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/generator_test.go +++ b/protoc-gen-openapiv2/internal/genopenapi/generator_test.go @@ -5,7 +5,7 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor" "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopenapi" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/descriptorpb" diff --git a/protoc-gen-openapiv2/internal/genopenapi/types.go b/protoc-gen-openapiv2/internal/genopenapi/types.go index c5cf59e9d6d..91cc1617b90 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/types.go +++ b/protoc-gen-openapiv2/internal/genopenapi/types.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor" - "gopkg.in/yaml.v2" ) type param struct { @@ -257,16 +256,13 @@ type keyVal struct { type openapiSchemaObjectProperties []keyVal func (p openapiSchemaObjectProperties) MarshalYAML() (interface{}, error) { - ms := make(yaml.MapSlice, len(p)) + m := make(map[string]interface{}) - for i, v := range p { - ms[i] = yaml.MapItem{ - Key: v.Key, - Value: v.Value, - } + for _, v := range p { + m[v.Key] = v.Value } - return ms, nil + return m, nil } func (op openapiSchemaObjectProperties) MarshalJSON() ([]byte, error) { diff --git a/protoc-gen-openapiv2/internal/genopenapi/types_test.go b/protoc-gen-openapiv2/internal/genopenapi/types_test.go index c6488fb8a78..849cfbfbb4a 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/types_test.go +++ b/protoc-gen-openapiv2/internal/genopenapi/types_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" ) func newSpaceReplacer() *strings.Replacer { diff --git a/repositories.bzl b/repositories.bzl index f54bab6ec71..e937bad5d9d 100644 --- a/repositories.bzl +++ b/repositories.bzl @@ -185,8 +185,8 @@ def go_repositories(): go_repository( name = "com_github_davecgh_go_spew", importpath = "github.com/davecgh/go-spew", - sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=", - version = "v1.1.1", + sum = "h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=", + version = "v1.1.0", ) go_repository( name = "com_github_dgrijalva_jwt_go", @@ -1007,14 +1007,14 @@ def go_repositories(): go_repository( name = "in_gopkg_yaml_v2", importpath = "gopkg.in/yaml.v2", - sum = "h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=", - version = "v2.4.0", + sum = "h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=", + version = "v2.2.3", ) go_repository( name = "in_gopkg_yaml_v3", importpath = "gopkg.in/yaml.v3", - sum = "h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=", - version = "v3.0.0-20200313102051-9f266ea9e77c", + sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=", + version = "v3.0.1", ) go_repository( name = "io_etcd_go_bbolt", @@ -1022,12 +1022,6 @@ def go_repositories(): sum = "h1:Z/90sZLPOeCy2PwprqkFa25PdkusRzaj9P8zm/KNyvk=", version = "v1.3.2", ) - go_repository( - name = "io_k8s_sigs_yaml", - importpath = "sigs.k8s.io/yaml", - sum = "h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=", - version = "v1.3.0", - ) go_repository( name = "io_opencensus_go",