Skip to content

Commit 629f88b

Browse files
chore: clean the convertor codes
1 parent f13518d commit 629f88b

File tree

3 files changed

+68
-39
lines changed

3 files changed

+68
-39
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package genopenapiv3
2+
3+
import (
4+
"github.com/getkin/kin-openapi/openapi3"
5+
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv3/options"
6+
)
7+
8+
func convertSecurityRequiremnt(requirements []*options.SecurityRequirement) *openapi3.SecurityRequirements {
9+
oAPIReqs := openapi3.NewSecurityRequirements()
10+
11+
for _, req := range requirements {
12+
oAPISecReq := openapi3.NewSecurityRequirement()
13+
for authenticator, scopes := range req.GetAdditionalProperties() {
14+
oAPISecReq.Authenticate(authenticator, scopes.GetScopes()...)
15+
}
16+
17+
oAPIReqs.With(oAPISecReq)
18+
}
19+
20+
return oAPIReqs
21+
}

protoc-gen-openapiv3/internal/genopenapiv3/file_generator.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/getkin/kin-openapi/openapi3"
1010
"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
11-
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv3/options"
1211
"google.golang.org/grpc/grpclog"
1312
"google.golang.org/protobuf/types/descriptorpb"
1413
)
@@ -342,7 +341,7 @@ func (fg *fileGenerator) generateMethodDoc(meth *descriptor.Method) error {
342341
}
343342

344343
if opOpts.GetSecurity() != nil {
345-
operation.Security = fg.generateSecurity(opOpts.GetSecurity())
344+
operation.Security = convertSecurityRequiremnt(opOpts.GetSecurity())
346345
}
347346

348347
path := fg.convertPathTemplate(binding.PathTmpl.Template)
@@ -373,21 +372,6 @@ func (fg *fileGenerator) generateMethodDoc(meth *descriptor.Method) error {
373372
return nil
374373
}
375374

376-
func (fg *fileGenerator) generateSecurity(requirements []*options.SecurityRequirement) *openapi3.SecurityRequirements {
377-
res := openapi3.NewSecurityRequirements()
378-
379-
for _, req := range requirements {
380-
oAPISecReq := openapi3.NewSecurityRequirement()
381-
for authenticator, scopes := range req.GetAdditionalProperties() {
382-
oAPISecReq.Authenticate(authenticator, scopes.GetScopes()...)
383-
}
384-
385-
res.With(oAPISecReq)
386-
}
387-
388-
return res
389-
}
390-
391375
func (fg *fileGenerator) convertPathTemplate(template string) string {
392376
// TODO: handle /{args=foo/*}
393377
return template

protoc-gen-openapiv3/internal/genopenapiv3/file_options.go

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,75 @@ import (
77
"google.golang.org/protobuf/proto"
88
)
99

10-
func (g *generator) extractFileOptions(target *descriptor.File) (*openapi3.T, bool) {
10+
func (g *generator) convertFileOptions(target *descriptor.File) (*openapi3.T, bool) {
1111
if openAPIAno := proto.GetExtension(target.GetOptions(), options.E_Openapiv3Document).(*options.OpenAPI); openAPIAno != nil {
1212
doc := &openapi3.T{
13-
OpenAPI: OpenAPIVersion,
13+
OpenAPI: OpenAPIVersion,
14+
Info: g.convertInfo(openAPIAno.GetInfo()),
15+
Security: *convertSecurityRequiremnt(openAPIAno.GetSecurity()),
16+
Servers: g.convertServers(openAPIAno.GetServers()),
1417
}
15-
doc.Info = g.extractInfo(openAPIAno.GetInfo())
1618
// TODO: implement other openapi file annotation fields
1719
return doc, true
1820
}
1921

2022
return nil, false
2123
}
2224

23-
func (g *generator) extractInfo(openAPIInfo *options.Info) *openapi3.Info {
25+
func (g *generator) convertServers(servers []*options.Server) openapi3.Servers {
26+
oAPIservers := make(openapi3.Servers, len(servers))
27+
28+
for i, srv := range servers {
29+
vars := map[string]*openapi3.ServerVariable{}
30+
31+
for k, v := range srv.GetVariables() {
32+
vars[k] = &openapi3.ServerVariable{
33+
Enum: v.GetEnum(),
34+
Default: v.GetDefault(),
35+
Description: v.GetDescription(),
36+
}
37+
}
38+
39+
oAPIservers[i] = &openapi3.Server{
40+
URL: srv.GetUrl(),
41+
Description: srv.GetDescription(),
42+
Variables: vars,
43+
}
44+
}
45+
46+
return oAPIservers
47+
}
48+
49+
func (g *generator) convertInfo(openAPIInfo *options.Info) *openapi3.Info {
2450
return &openapi3.Info{
25-
Title: openAPIInfo.GetTitle(),
26-
Description: openAPIInfo.GetDescription(),
27-
Version: openAPIInfo.GetVersion(),
51+
Title: openAPIInfo.GetTitle(),
52+
Description: openAPIInfo.GetDescription(),
53+
Version: openAPIInfo.GetVersion(),
2854
TermsOfService: openAPIInfo.GetTermsOfService(),
29-
Contact: g.extractContact(openAPIInfo.GetContact()),
30-
License: g.extractLicense(openAPIInfo.GetLicense()),
55+
Contact: g.convertContact(openAPIInfo.GetContact()),
56+
License: g.convertLicense(openAPIInfo.GetLicense()),
3157
}
3258
}
3359

34-
func (g *generator) extractContact(contactOption *options.Contact) *openapi3.Contact {
60+
func (g *generator) convertContact(contactOption *options.Contact) *openapi3.Contact {
3561
if contactOption == nil {
3662
return nil
3763
}
3864

39-
contact := &openapi3.Contact{}
40-
contact.Name = contactOption.GetName()
41-
contact.URL = contactOption.GetUrl()
42-
contact.Email = contactOption.GetEmail()
43-
44-
return contact
65+
return &openapi3.Contact{
66+
Name: contactOption.GetName(),
67+
URL: contactOption.GetUrl(),
68+
Email: contactOption.GetEmail(),
69+
}
4570
}
4671

47-
func (g *generator) extractLicense(licenseOption *options.License) *openapi3.License {
72+
func (g *generator) convertLicense(licenseOption *options.License) *openapi3.License {
4873
if licenseOption == nil {
4974
return nil
5075
}
5176

52-
license := &openapi3.License{}
53-
license.Name = licenseOption.GetName()
54-
license.URL = licenseOption.GetUrl()
55-
56-
return license
77+
return &openapi3.License{
78+
Name: licenseOption.GetName(),
79+
URL: licenseOption.GetUrl(),
80+
}
5781
}

0 commit comments

Comments
 (0)