Skip to content

Commit

Permalink
build: generate OpenAPI from grpc (#3357)
Browse files Browse the repository at this point in the history
* build: generate openapi from grpc

Signed-off-by: Roman Dmytrenko <[email protected]>

* added OFREP endpoints to OpenApi

Signed-off-by: Roman Dmytrenko <[email protected]>

* move openapi.yaml to root.
replace flipt:sdk:ignore with api_visibility restriction.
update operation ids and tags

Signed-off-by: Roman Dmytrenko <[email protected]>

---------

Signed-off-by: Roman Dmytrenko <[email protected]>
  • Loading branch information
erka authored Aug 7, 2024
1 parent 39d751b commit 0360ebe
Show file tree
Hide file tree
Showing 33 changed files with 4,627 additions and 1,871 deletions.
11 changes: 11 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
version: v1

plugins:
# core apis
- name: go
out: rpc/flipt
opt:
- paths=source_relative
- Mgnostic/openapi/v3/annotations.proto=github.com/google/gnostic-models/openapiv3
strategy: all
- name: go-grpc
out: rpc/flipt
Expand All @@ -23,3 +25,12 @@ plugins:
- paths=source_relative
- grpc_api_configuration=rpc/flipt/flipt.yaml
strategy: all
- plugin: buf.build/community/google-gnostic-openapi
out: .
opt:
- version=1.47.0
- fq_schema_naming=false
- default_response=false
- title=api
- description=

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/flatbuffers v24.3.25+incompatible // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20240509144519-723abb6459b7 // indirect
github.com/google/s2a-go v0.1.7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/flatbuffers v24.3.25+incompatible h1:CX395cjN9Kke9mmalRoL3d81AtFUxJM+yDthflgJGkI=
github.com/google/flatbuffers v24.3.25+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 h1:0VpGH+cDhbDtdcweoyCVsF3fhN8kejK6rFe/2FFX2nU=
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49/go.mod h1:BkkQ4L1KS1xMt2aWSPStnn55ChGC0DPOn2FQYj+f25M=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand Down
17 changes: 16 additions & 1 deletion internal/cmd/protoc-gen-go-flipt-sdk/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"google.golang.org/genproto/googleapis/api/serviceconfig"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"sigs.k8s.io/yaml"
)
Expand All @@ -32,6 +33,21 @@ func generateHTTP(gen *protogen.Plugin, grpcAPIConfig string) {
}

m := mappings{}

for _, f := range gen.Files {
if !f.Generate {
continue
}
for _, s := range f.Services {
for _, m := range s.Methods {
if rule := proto.GetExtension(m.Desc.Options(), annotations.E_Http).(*annotations.HttpRule); rule != nil {
rule.Selector = string(m.Desc.FullName())
config.Http.Rules = append(config.Http.Rules, rule)
}
}
}
}

for _, r := range config.Http.Rules {
rule := rule{
body: r.Body == "*",
Expand All @@ -53,7 +69,6 @@ func generateHTTP(gen *protogen.Plugin, grpcAPIConfig string) {
default:
fmt.Fprintf(os.Stderr, "unsupported pattern: %T\n", r.Pattern)
}

m[r.Selector] = rule
}

Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/protoc-gen-go-flipt-sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"slices"
"strings"

"google.golang.org/genproto/googleapis/api/visibility"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)

Expand Down Expand Up @@ -415,9 +417,8 @@ func New(t Transport, opts ...Option) SDK {
}`

func shouldIgnoreService(srv *protogen.Service) bool {
if srv.Comments.Leading != "" {
leading := strings.TrimPrefix(string(srv.Comments.Leading), "//")
return strings.TrimSpace(leading) == ignoreDecl
if v := proto.GetExtension(srv.Desc.Options(), visibility.E_ApiVisibility).(*visibility.VisibilityRule); v != nil {
return v.Restriction == ignoreDecl
}
return false
}
Loading

0 comments on commit 0360ebe

Please sign in to comment.