Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions internal/codegenerator/supported_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@ package codegenerator

import (
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
)

func supportedCodeGeneratorFeatures() uint64 {
// Enable support for optional keyword in proto3.
return uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
// Enable support for Protobuf Editions
return uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL | pluginpb.CodeGeneratorResponse_FEATURE_SUPPORTS_EDITIONS)
}

func supportedEditions() (descriptorpb.Edition, descriptorpb.Edition) {
// Declare support for edition 2023 only
return descriptorpb.Edition_EDITION_2023, descriptorpb.Edition_EDITION_2023
}

// SetSupportedFeaturesOnPluginGen sets supported proto3 features
// on protogen.Plugin.
func SetSupportedFeaturesOnPluginGen(gen *protogen.Plugin) {
gen.SupportedFeatures = supportedCodeGeneratorFeatures()
gen.SupportedEditionsMinimum, gen.SupportedEditionsMaximum = supportedEditions()
}

// SetSupportedFeaturesOnCodeGeneratorResponse sets supported proto3 features
// on pluginpb.CodeGeneratorResponse.
func SetSupportedFeaturesOnCodeGeneratorResponse(resp *pluginpb.CodeGeneratorResponse) {
sf := supportedCodeGeneratorFeatures()
resp.SupportedFeatures = &sf
minE, maxE := supportedEditions()
minEN, maxEN := int32(minE.Number()), int32(maxE.Number())
resp.MinimumEdition, resp.MaximumEdition = &minEN, &maxEN
}
10 changes: 7 additions & 3 deletions protoc-gen-grpc-gateway/internal/gengateway/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"go/format"
"path"

"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
gen "github.com/grpc-ecosystem/grpc-gateway/v2/internal/generator"
"google.golang.org/grpc/grpclog"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"

"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
gen "github.com/grpc-ecosystem/grpc-gateway/v2/internal/generator"
)

var errNoTargetService = errors.New("no target service defined in the file")
Expand All @@ -22,11 +23,12 @@ type generator struct {
registerFuncSuffix string
allowPatchFeature bool
standalone bool
useOpaqueAPI bool
}

// New returns a new generator which generates grpc gateway files.
func New(reg *descriptor.Registry, useRequestContext bool, registerFuncSuffix string,
allowPatchFeature, standalone bool) gen.Generator {
allowPatchFeature, standalone bool, useOpaqueAPI bool) gen.Generator {
var imports []descriptor.GoPackage
for _, pkgpath := range []string{
"context",
Expand Down Expand Up @@ -66,6 +68,7 @@ func New(reg *descriptor.Registry, useRequestContext bool, registerFuncSuffix st
registerFuncSuffix: registerFuncSuffix,
allowPatchFeature: allowPatchFeature,
standalone: standalone,
useOpaqueAPI: useOpaqueAPI,
}
}

Expand Down Expand Up @@ -132,6 +135,7 @@ func (g *generator) generate(file *descriptor.File) (string, error) {
UseRequestContext: g.useRequestContext,
RegisterFuncSuffix: g.registerFuncSuffix,
AllowPatchFeature: g.allowPatchFeature,
UseOpaqueAPI: g.useOpaqueAPI,
}
if g.reg != nil {
params.OmitPackageDoc = g.reg.GetOmitPackageDoc()
Expand Down
17 changes: 16 additions & 1 deletion protoc-gen-grpc-gateway/internal/gengateway/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package gengateway
import (
"testing"

"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"

"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
)

func newExampleFileDescriptorWithGoPkg(gp *descriptor.GoPackage, filenamePrefix string) *descriptor.File {
Expand Down Expand Up @@ -76,8 +77,22 @@ func newExampleFileDescriptorWithGoPkg(gp *descriptor.GoPackage, filenamePrefix
}

func TestGenerator_Generate(t *testing.T) {

// Test with Open Struct API (default)
t.Run("OpenStructAPI", func(t *testing.T) {
testGeneratorGenerate(t, false)
})

// Test with Opaque API
t.Run("OpaqueAPI", func(t *testing.T) {
testGeneratorGenerate(t, true)
})
}

func testGeneratorGenerate(t *testing.T, useOpaqueAPI bool) {
g := new(generator)
g.reg = descriptor.NewRegistry()
g.useOpaqueAPI = useOpaqueAPI
result, err := g.Generate([]*descriptor.File{
crossLinkFixture(newExampleFileDescriptorWithGoPkg(&descriptor.GoPackage{
Path: "example.com/path/to/example",
Expand Down
Loading