Skip to content

Commit 3349a4d

Browse files
Fix ignore and add cmd
1 parent e2cd8bc commit 3349a4d

File tree

4 files changed

+290
-3
lines changed

4 files changed

+290
-3
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
# Go workspace file
2222
go.work
2323

24-
# executable
25-
protoc-gen-openapi
26-
2724
.idea/
2825

2926
test/openapi.yaml

cmd/protoc-gen-openapi/main.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
6+
"github.com/technicallyjosh/protoc-gen-openapi/internal/generator"
7+
"google.golang.org/protobuf/compiler/protogen"
8+
)
9+
10+
func main() {
11+
var flags flag.FlagSet
12+
13+
conf := generator.Config{
14+
ContentType: flags.String("content_type", "application/json", "Default content-type for all paths."),
15+
DefaultResponse: flags.String("default_response", "", "Default response message to use for API responses not defined."),
16+
Description: flags.String("description", "", "Description of the API."),
17+
Filename: flags.String("filename", "openapi", "Name of the file generated without the extension."),
18+
Host: flags.String("host", "", "Host to be used for all routes."),
19+
Ignore: flags.String("ignore", "", "Packages to ignore."),
20+
Include: flags.String("include", "", "Packages to include. Ignore overrides this."),
21+
JSONOutput: flags.Bool("json_out", false, "Generate a JSON file instead of YAML."),
22+
Title: flags.String("title", "", "Title of the API"),
23+
UseJSONNames: flags.Bool("json_names", false, "Use JSON names instead of the proto names of fields."),
24+
Version: flags.String("version", "0.0.1", "Version of the API."),
25+
}
26+
27+
opts := protogen.Options{
28+
ParamFunc: flags.Set,
29+
}
30+
31+
opts.Run(func(plugin *protogen.Plugin) error {
32+
return generator.New(plugin, conf).Run()
33+
})
34+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package main_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"testing"
8+
9+
jd "github.com/josephburnett/jd/lib"
10+
"github.com/stretchr/testify/suite"
11+
"gopkg.in/yaml.v3"
12+
)
13+
14+
type TestOptions struct {
15+
defaultResponse string
16+
description string
17+
testProto string
18+
title string
19+
version string
20+
include string
21+
ignore string
22+
}
23+
24+
type TestSuite struct {
25+
suite.Suite
26+
rawDoc []byte
27+
options TestOptions
28+
}
29+
30+
func (s *TestSuite) YAMLEqual(expected any, actual any, msgAndArgs ...any) {
31+
var expectedYAML, actualYAML string
32+
33+
switch v := expected.(type) {
34+
case string:
35+
expectedYAML = v
36+
case []byte:
37+
expectedYAML = string(v)
38+
default:
39+
expectedBytes, err := yaml.Marshal(v)
40+
if err != nil {
41+
s.FailNow(fmt.Sprintf("failed to marshal expected: %#v", v), msgAndArgs...)
42+
}
43+
expectedYAML = string(expectedBytes)
44+
}
45+
46+
switch v := actual.(type) {
47+
case string:
48+
actualYAML = v
49+
case []byte:
50+
actualYAML = string(v)
51+
default:
52+
actualBytes, err := yaml.Marshal(v)
53+
if err != nil {
54+
s.FailNow(fmt.Sprintf("failed to marshal actual: %#v", v), msgAndArgs...)
55+
}
56+
actualYAML = string(actualBytes)
57+
}
58+
59+
expectedNode, _ := jd.ReadYamlString(expectedYAML)
60+
actualNode, _ := jd.ReadYamlString(actualYAML)
61+
62+
diff := expectedNode.Diff(actualNode, jd.SET).Render(jd.COLOR)
63+
64+
if diff != "" {
65+
s.Fail(fmt.Sprintf("Not Equal:\nexpected:\n%s\nactual:\n%s\n\n%s", expectedYAML, actualYAML, diff), msgAndArgs...)
66+
}
67+
}
68+
69+
func (s *TestSuite) SetupSuite() {
70+
s.options = TestOptions{
71+
defaultResponse: "test.api.Error",
72+
description: "test description",
73+
testProto: "basic_test.proto",
74+
title: "test title",
75+
version: "1.1.0",
76+
}
77+
}
78+
79+
func (s *TestSuite) BeforeTest(suite, name string) {
80+
var filename string
81+
82+
switch name {
83+
case "TestBasic":
84+
filename = "basic_test.proto"
85+
case "TestFile":
86+
filename = "file_test.proto"
87+
case "TestService":
88+
filename = "service_test.proto"
89+
case "TestMethod":
90+
filename = "method_test.proto"
91+
case "TestField":
92+
filename = "field_test.proto"
93+
default:
94+
s.FailNow("invalid test name")
95+
}
96+
97+
err := exec.Command("rm", "-f", "../../test/openapi.yaml").Run()
98+
if err != nil {
99+
s.FailNow(err.Error())
100+
}
101+
102+
out, err := exec.Command("protoc",
103+
"-I=../../proto/technicallyjosh/oapi",
104+
"-I=../../test",
105+
"--openapi_out=../../test",
106+
"--openapi_opt=version="+s.options.version,
107+
"--openapi_opt=title="+s.options.title,
108+
"--openapi_opt=description="+s.options.description,
109+
"--openapi_opt=default_response="+s.options.defaultResponse,
110+
"--openapi_opt=include="+s.options.include,
111+
"--openapi_opt=ignore="+s.options.ignore,
112+
"../../test/"+filename,
113+
).CombinedOutput()
114+
if err != nil {
115+
s.FailNow(err.Error(), string(out))
116+
}
117+
118+
s.rawDoc, err = os.ReadFile("../../test/openapi.yaml")
119+
if err != nil {
120+
s.FailNow(err.Error())
121+
}
122+
}
123+
124+
func (s *TestSuite) TestBasic() {
125+
s.YAMLEqual(readFile("basic_test_openapi.yaml"), string(s.rawDoc))
126+
}
127+
128+
func (s *TestSuite) TestFile() {
129+
s.YAMLEqual(readFile("file_test_openapi.yaml"), string(s.rawDoc))
130+
}
131+
132+
func (s *TestSuite) TestService() {
133+
s.YAMLEqual(readFile("service_test_openapi.yaml"), string(s.rawDoc))
134+
}
135+
136+
func (s *TestSuite) TestMethod() {
137+
s.YAMLEqual(readFile("method_test_openapi.yaml"), string(s.rawDoc))
138+
}
139+
140+
func (s *TestSuite) TestField() {
141+
s.YAMLEqual(readFile("field_test_openapi.yaml"), string(s.rawDoc))
142+
}
143+
144+
func TestSuites(t *testing.T) {
145+
suite.Run(t, new(TestSuite))
146+
}
147+
148+
func readFile(name string) string {
149+
data, _ := os.ReadFile("../../test/" + name)
150+
151+
return string(data)
152+
}

internal/gen/v2/oapi.pb.go

Lines changed: 104 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)