Skip to content

Commit c622f0f

Browse files
fix: generation path and encoder indentation
1 parent e40f7b6 commit c622f0f

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ import (
66
"strings"
77

88
"github.com/getkin/kin-openapi/openapi3"
9-
"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
10-
gen "github.com/grpc-ecosystem/grpc-gateway/v2/internal/generator"
11-
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv3/options"
129
"google.golang.org/genproto/googleapis/api/visibility"
1310
statuspb "google.golang.org/genproto/googleapis/rpc/status"
1411
"google.golang.org/protobuf/proto"
1512
"google.golang.org/protobuf/reflect/protodesc"
1613
"google.golang.org/protobuf/types/descriptorpb"
1714
"google.golang.org/protobuf/types/known/anypb"
1815
"google.golang.org/protobuf/types/pluginpb"
16+
17+
"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
18+
gen "github.com/grpc-ecosystem/grpc-gateway/v2/internal/generator"
19+
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv3/options"
1920
)
2021

2122
type generator struct {
2223
reg *descriptor.Registry
2324
format Format
2425
}
2526

26-
2727
func NewGenerator(reg *descriptor.Registry, format Format) gen.Generator {
2828
return &generator{
2929
reg: reg,
@@ -37,6 +37,10 @@ func (g *generator) Generate(targets []*descriptor.File) ([]*descriptor.Response
3737
return nil, fmt.Errorf("could not load prequisite proto files in registry: %w", err)
3838
}
3939

40+
if len(targets) == 0 {
41+
return nil, nil
42+
}
43+
4044
respFiles := make([]*descriptor.ResponseFile, 0, len(targets))
4145
docs := make([]*openapi3.T, 0, len(targets))
4246
for _, t := range targets {
@@ -50,12 +54,14 @@ func (g *generator) Generate(targets []*descriptor.File) ([]*descriptor.Response
5054
}
5155

5256
base := filepath.Base(t.GetName())
57+
fileDir := filepath.Dir(t.GetName())
5358
ext := filepath.Ext(base)
5459
fileName := fmt.Sprintf("%s.openapiv3.%s", base[:len(base)-len(ext)], g.format)
60+
docPath := filepath.Join(fileDir, fileName)
5561

5662
respFiles = append(respFiles, &descriptor.ResponseFile{
5763
CodeGeneratorResponse_File: &pluginpb.CodeGeneratorResponse_File{
58-
Name: proto.String(fileName),
64+
Name: proto.String(docPath),
5965
Content: proto.String(string(contentBytes)),
6066
},
6167
})
@@ -72,9 +78,12 @@ func (g *generator) Generate(targets []*descriptor.File) ([]*descriptor.Response
7278
return nil, err
7379
}
7480

81+
mergedDir := filepath.Dir(targets[0].GetName())
82+
mergedPath := filepath.Join(mergedDir, fmt.Sprintf("merged.openapiv3.%s", g.format))
83+
7584
respFiles = append(respFiles, &descriptor.ResponseFile{
7685
CodeGeneratorResponse_File: &pluginpb.CodeGeneratorResponse_File{
77-
Name: proto.String(fmt.Sprintf("merged.openapiv3.%s", g.format)),
86+
Name: proto.String(mergedPath),
7887
Content: proto.String(string(contentBytes)),
7988
},
8089
})

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package genopenapiv3
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"errors"
7+
"fmt"
58

69
"github.com/getkin/kin-openapi/openapi3"
710
"google.golang.org/grpc/grpclog"
@@ -17,21 +20,38 @@ const (
1720

1821
var ErrUnSupportedFormat = errors.New("unsupported format provided")
1922

23+
type Encoder interface {
24+
Encode(v any) error
25+
}
2026

2127
func (f Format) MarshalOpenAPIDoc(doc *openapi3.T) ([]byte, error) {
28+
29+
openapiDoc, err := doc.MarshalYAML()
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
var buf bytes.Buffer
35+
var encoder Encoder
36+
2237
switch f {
2338
case FormatJSON:
24-
return doc.MarshalJSON()
39+
encoder = json.NewEncoder(&buf)
40+
encoder.(*json.Encoder).SetIndent("", " ")
2541
case FormatYAML:
26-
openapiDoc, err := doc.MarshalYAML()
27-
if err != nil {
28-
return nil, err
29-
}
30-
return yaml.Marshal(openapiDoc)
42+
encoder = yaml.NewEncoder(&buf)
43+
encoder.(*yaml.Encoder).SetIndent(2)
3144
default:
3245
grpclog.Errorf("unsupported format: %s\n", f)
3346
return nil, ErrUnSupportedFormat
3447
}
48+
49+
err = encoder.Encode(openapiDoc)
50+
if err != nil {
51+
return nil, fmt.Errorf("failed to encode OpenAPI document: %w", err)
52+
}
53+
54+
return buf.Bytes(), nil
3555
}
3656

3757
type OneOfStrategy string

protoc-gen-openapiv3/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import (
66
"os"
77
"strings"
88

9+
"google.golang.org/grpc/grpclog"
10+
"google.golang.org/protobuf/proto"
11+
"google.golang.org/protobuf/types/pluginpb"
12+
913
"github.com/grpc-ecosystem/grpc-gateway/v2/internal/codegenerator"
1014
"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
1115
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv3/internal/genopenapiv3"
1216
"github.com/grpc-ecosystem/grpc-gateway/v2/utilities"
13-
"google.golang.org/grpc/grpclog"
14-
"google.golang.org/protobuf/proto"
15-
"google.golang.org/protobuf/types/pluginpb"
1617
)
1718

1819
var (
1920
file = flag.String("file", "-", "where to load data from")
20-
openAPIConfiguration = flag.String("openapi_configuration", "", "path to file which describes the OpenAPI Configuration in YAML format")
2121
oneOfStrategy = flag.String("oneof_strategy", "oneOf", "how to handle oneofs")
2222
outputFormat = flag.String("output_format", string(genopenapiv3.FormatJSON), fmt.Sprintf("output content format. Allowed values are: `%s`, `%s`", genopenapiv3.FormatJSON, genopenapiv3.FormatYAML))
2323
visibilityRestrictionSelectors = utilities.StringArrayFlag(flag.CommandLine, "visibility_restriction_selectors", "list of `google.api.VisibilityRule` visibility labels to include in the generated output when a visibility annotation is defined. Repeat this option to supply multiple values. Elements without visibility annotations are unaffected by this setting.")

0 commit comments

Comments
 (0)