Skip to content

Commit 510e2d5

Browse files
authored
support proto oneof, multi-line descriptions with yaml, native int schemas, validation markers (#19)
Adds support for the following: - multi-line descriptions for properties when using yaml=true - fixes uint32 schema to use min-max and not int32 format - updates go protobuf packages to use new lib as per https://protobuf.dev/reference/go/faq/#versions - kubebuilder style comment markers for schema validations to generate validation rules Signed-off-by: Shashank Ram <[email protected]>
1 parent 0617ff6 commit 510e2d5

32 files changed

+1238
-146
lines changed

.github/workflows/test.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
7+
jobs:
8+
go:
9+
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Setup Go 1.22
16+
uses: actions/setup-go@v4
17+
with:
18+
go-version: '1.22.x'
19+
20+
- name: Run tests
21+
run: make build-and-test

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ _output/
2525
# Local binary produced by make build
2626
protoc-gen-openapi
2727
out/
28+
_output/

Makefile

+44-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,53 @@
11

22
all: build run
33

4-
build:
5-
go build -o protoc-gen-openapi
4+
build-and-test: build gotest
5+
6+
ROOTDIR := $(shell pwd)
7+
OUTPUTDIR = $(ROOTDIR)/_output
8+
BINDIR = $(OUTPUTDIR)/.bin
9+
10+
.PHONY: install-deps
11+
install-deps: install-protoc
12+
mkdir -p $(BINDIR)
13+
GOBIN=$(BINDIR) go install github.com/golang/protobuf/protoc-gen-go
14+
15+
build: install-deps
16+
mkdir -p $(BINDIR)
17+
go build -o $(BINDIR)/protoc-gen-openapi
618

719
run:
8-
rm -fr out
9-
mkdir out
10-
protoc --plugin=./protoc-gen-openapi --openapi_out=single_file=true,use_ref=true:out/. -Itestdata testdata/testpkg/test1.proto testdata/testpkg/test2.proto testdata/testpkg/test6.proto testdata/testpkg2/test3.proto
20+
rm -fr $(OUTPUTDIR)
21+
mkdir -p $(OUTPUTDIR)
22+
protoc --plugin=./$(BINDIR)/protoc-gen-openapi --openapi_out=single_file=true,use_ref=true:$(OUTPUTDIR)/. -Itestdata testdata/testpkg/test1.proto testdata/testpkg/test2.proto testdata/testpkg/test6.proto testdata/testpkg2/test3.proto
1123

1224
gotest:
13-
go test
25+
PATH=$(BINDIR):$(PATH) go test -v ./...
26+
27+
PROTOC_VERSION:=3.15.8
28+
PROTOC_URL:=https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}
29+
.PHONY: install-protoc
30+
.SILENT: install-protoc
31+
install-protoc:
32+
mkdir -p $(BINDIR)
33+
if [ $(shell ${BINDIR}/protoc --version | grep -c ${PROTOC_VERSION}) -ne 0 ]; then \
34+
echo expected protoc version ${PROTOC_VERSION} already installed ;\
35+
else \
36+
if [ "$(shell uname)" = "Darwin" ]; then \
37+
echo "downloading protoc for osx" ;\
38+
wget $(PROTOC_URL)-osx-x86_64.zip -O $(BINDIR)/protoc-${PROTOC_VERSION}.zip ;\
39+
elif [ "$(shell uname -m)" = "aarch64" ]; then \
40+
echo "downloading protoc for linux aarch64" ;\
41+
wget $(PROTOC_URL)-linux-aarch_64.zip -O $(BINDIR)/protoc-${PROTOC_VERSION}.zip ;\
42+
else \
43+
echo "downloading protoc for linux x86-64" ;\
44+
wget $(PROTOC_URL)-linux-x86_64.zip -O $(BINDIR)/protoc-${PROTOC_VERSION}.zip ;\
45+
fi ;\
46+
unzip $(BINDIR)/protoc-${PROTOC_VERSION}.zip -d $(BINDIR)/protoc-${PROTOC_VERSION} ;\
47+
mv $(BINDIR)/protoc-${PROTOC_VERSION}/bin/protoc $(BINDIR)/protoc ;\
48+
chmod +x $(BINDIR)/protoc ;\
49+
rm -rf $(BINDIR)/protoc-${PROTOC_VERSION} $(BINDIR)/protoc-${PROTOC_VERSION}.zip ;\
50+
fi
1451

1552
clean:
16-
@rm -fr out protoc-gen-openapi
53+
@rm -rf $(OUTPUTDIR)

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,17 @@ on Debian or Ubuntu, you can install it from the package manager:
1515
sudo apt-get install -y golang
1616
```
1717

18-
To build, first ensure you have the protocol compiler (protoc):
19-
20-
```bash
21-
go get github.com/golang/protobuf/proto
22-
```
2318
To build, run the following command from this project directory:
2419

2520
```bash
26-
go build
21+
make build
2722
```
2823

2924
Then ensure the resulting `protoc-gen-openapi` binary is in your `PATH`. A recommended location
3025
is `$HOME/bin`:
3126

3227
```bash
33-
cp protoc-gen-openapi $HOME/bin
28+
cp _output/.bin/protoc-gen-openapi $HOME/bin
3429
```
3530

3631
Since the following is often in your `$HOME/.bashrc` file:
@@ -69,7 +64,13 @@ Other supported options are:
6964
* when set to `true`, the output is in yaml file.
7065
* `include_description`
7166
* when set to `true`, the openapi schema will include descriptions, generated from the proto message comment.
67+
* `multiline_description`
68+
* when set to `true`, the openapi schema will include descriptions, generated from the proto message comment, that can span multiple lines. This can only be used with `yaml=true`.
7269
* `enum_as_int_or_string`
7370
* when set to `true`, the openapi schema will include `x-kubernetes-int-or-string` on enums.
7471
* `additional_empty_schemas`
75-
* a `+` separated list of message names (`core.solo.io.Status`), whose generated schema should be an empty object that accepts all values.
72+
* a `+` separated list of message names (`core.solo.io.Status`), whose generated schema should be an empty object that accepts all values.
73+
* `proto_oneof`
74+
* when set to `true`, the openapi schema will include `oneOf` emulating the behavior of proto `oneof`.
75+
* `int_native`
76+
* when set to `true`, the native openapi schemas will be used for Integer types instead of Solo wrappers that add Kubernetes extension headers to the schema to treat int as strings

changelog/v0.2.0/oneof-misc.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
changelog:
2+
- type: BREAKING_CHANGE
3+
issueLink: https://github.com/solo-io/protoc-gen-openapi/issues/20
4+
resolvesIssue: true
5+
description: |
6+
Adds support for the following:
7+
- encodes proto oneof to OpenAPI oneOf schema
8+
- multi-line descriptions for properties when using yaml=true
9+
- fixes uint32 schema to use min-max and not int32 format
10+
- adds proto for custom field options support
11+
- updates go protobuf packages to use new lib as per https://protobuf.dev/reference/go/faq/#versions
12+
- build changes for proto code generation

go.mod

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
module github.com/solo-io/protoc-gen-openapi
22

3-
go 1.18
3+
go 1.22
44

55
require (
66
github.com/getkin/kin-openapi v0.80.0
77
github.com/ghodss/yaml v1.0.0
8-
github.com/golang/protobuf v1.3.2
8+
github.com/golang/protobuf v1.5.3
9+
google.golang.org/protobuf v1.31.0
10+
sigs.k8s.io/controller-tools v0.14.0
911
)
1012

1113
require (
12-
github.com/go-openapi/jsonpointer v0.19.5 // indirect
13-
github.com/go-openapi/swag v0.19.5 // indirect
14-
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect
15-
gopkg.in/yaml.v2 v2.3.0 // indirect
14+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
15+
github.com/go-openapi/swag v0.22.3 // indirect
16+
github.com/josharian/intern v1.0.0 // indirect
17+
github.com/mailru/easyjson v0.7.7 // indirect
18+
golang.org/x/mod v0.14.0 // indirect
19+
golang.org/x/tools v0.16.1 // indirect
20+
gopkg.in/yaml.v2 v2.4.0 // indirect
21+
gopkg.in/yaml.v3 v3.0.1 // indirect
22+
k8s.io/apimachinery v0.29.0 // indirect
1623
)

go.sum

+67-10
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,91 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
12
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
23
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
34
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
6+
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
47
github.com/getkin/kin-openapi v0.80.0 h1:W/s5/DNnDCR8P+pYyafEWlGk4S7/AfQUWXgrRSSAzf8=
58
github.com/getkin/kin-openapi v0.80.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg=
69
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
710
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
8-
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
911
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
10-
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
12+
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
13+
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
1114
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
12-
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
13-
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
15+
github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g=
16+
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
17+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
18+
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
19+
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
20+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
21+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
22+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1423
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
15-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
24+
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
25+
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
1626
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
27+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
28+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
29+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
1730
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
18-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1931
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
32+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
33+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2034
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
21-
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
2235
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
36+
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
37+
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
38+
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
39+
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
40+
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
41+
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
42+
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
43+
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
2344
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2445
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
46+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
47+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
2548
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
49+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
50+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
2651
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
27-
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
2852
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
53+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
54+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
55+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
56+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
57+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
58+
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
59+
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
60+
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
61+
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
62+
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
63+
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
64+
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
65+
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
66+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
67+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
68+
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
69+
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
70+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
71+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
72+
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
73+
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
74+
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
2975
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
30-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
3176
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
77+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
78+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
79+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
80+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
3281
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
33-
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
3482
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
83+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
84+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
85+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
86+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
87+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
88+
k8s.io/apimachinery v0.29.0 h1:+ACVktwyicPz0oc6MTMLwa2Pw3ouLAfAon1wPLtG48o=
89+
k8s.io/apimachinery v0.29.0/go.mod h1:eVBxQ/cwiJxH58eK/jd/vAk4mrxmVlnpBH5J2GbMeis=
90+
sigs.k8s.io/controller-tools v0.14.0 h1:rnNoCC5wSXlrNoBKKzL70LNJKIQKEzT6lloG6/LF73A=
91+
sigs.k8s.io/controller-tools v0.14.0/go.mod h1:TV7uOtNNnnR72SpzhStvPkoS/U5ir0nMudrkrC4M9Sc=

integration_test.go

+47-18
Original file line numberDiff line numberDiff line change
@@ -28,60 +28,89 @@ const goldenDir = "testdata/golden/"
2828
func TestOpenAPIGeneration(t *testing.T) {
2929
testcases := []struct {
3030
name string
31+
id string
3132
perPackage bool
3233
genOpts string
34+
inputFiles map[string][]string
3335
wantFiles []string
3436
}{
3537
{
3638
name: "Per Package Generation",
39+
id: "test1",
3740
perPackage: true,
3841
genOpts: "",
39-
wantFiles: []string{"testpkg.json", "testpkg2.json"},
42+
inputFiles: map[string][]string{
43+
"testpkg": {"./testdata/testpkg/test1.proto", "./testdata/testpkg/test2.proto", "./testdata/testpkg/test6.proto"},
44+
"testpkg2": {"./testdata/testpkg2/test3.proto"},
45+
},
46+
wantFiles: []string{"testpkg.json", "testpkg2.json"},
4047
},
4148
{
4249
name: "Single File Generation",
50+
id: "test2",
4351
perPackage: false,
4452
genOpts: "single_file=true",
45-
wantFiles: []string{"openapiv3.json"},
53+
inputFiles: map[string][]string{
54+
"testpkg": {"./testdata/testpkg/test1.proto", "./testdata/testpkg/test2.proto", "./testdata/testpkg/test6.proto"},
55+
"testpkg2": {"./testdata/testpkg2/test3.proto"},
56+
},
57+
wantFiles: []string{"openapiv3.json"},
4658
},
4759
{
4860
name: "Use $ref in the output",
61+
id: "test3",
4962
perPackage: false,
5063
genOpts: "single_file=true,use_ref=true",
51-
wantFiles: []string{"testRef/openapiv3.json"},
64+
inputFiles: map[string][]string{
65+
"testpkg": {"./testdata/testpkg/test1.proto", "./testdata/testpkg/test2.proto", "./testdata/testpkg/test6.proto"},
66+
"testpkg2": {"./testdata/testpkg2/test3.proto"},
67+
},
68+
wantFiles: []string{"testRef/openapiv3.json"},
69+
},
70+
{
71+
name: "Use yaml, proto_oneof, int_native, validation rules, and multiline_description",
72+
id: "test4",
73+
perPackage: false,
74+
genOpts: "yaml=true,single_file=true,proto_oneof=true,int_native=true,multiline_description=true",
75+
inputFiles: map[string][]string{
76+
"testpkg": {"./testdata/testpkg/test1.proto", "./testdata/testpkg/test2.proto", "./testdata/testpkg/test6.proto"},
77+
"testpkg2": {"./testdata/testpkg2/test3.proto"},
78+
},
79+
wantFiles: []string{"test4/openapiv3.yaml"},
80+
},
81+
{
82+
name: "Test validation rules",
83+
id: "test5",
84+
perPackage: false,
85+
genOpts: "yaml=true,single_file=true,proto_oneof=true,int_native=true,multiline_description=true",
86+
inputFiles: map[string][]string{
87+
"test5": {"./testdata/test5/rules.proto"},
88+
},
89+
wantFiles: []string{"test5/openapiv3.yaml"},
5290
},
5391
}
5492

5593
for _, tc := range testcases {
5694
t.Run(tc.name, func(t *testing.T) {
57-
tempDir, err := os.MkdirTemp("", "openapi-temp")
58-
if err != nil {
59-
t.Fatal(err)
95+
if len(tc.inputFiles) == 0 {
96+
t.Fatalf("inputFiles must be set for test case %s", tc.name)
6097
}
61-
defer os.RemoveAll(tempDir)
6298

63-
// we assume that the package name is the same as the name of the folder containing the proto files.
64-
packages := make(map[string][]string)
65-
err = filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
66-
if strings.HasSuffix(path, ".proto") {
67-
dir := filepath.Dir(path)
68-
packages[dir] = append(packages[dir], path)
69-
}
70-
return nil
71-
})
99+
tempDir, err := os.MkdirTemp("", "openapi-temp")
72100
if err != nil {
73101
t.Fatal(err)
74102
}
103+
defer os.RemoveAll(tempDir)
75104

76105
if tc.perPackage {
77-
for _, files := range packages {
106+
for _, files := range tc.inputFiles {
78107
args := []string{"-Itestdata", "--openapi_out=" + tc.genOpts + ":" + tempDir}
79108
args = append(args, files...)
80109
protocOpenAPI(t, args)
81110
}
82111
} else {
83112
args := []string{"-Itestdata", "--openapi_out=" + tc.genOpts + ":" + tempDir}
84-
for _, files := range packages {
113+
for _, files := range tc.inputFiles {
85114
args = append(args, files...)
86115
}
87116
protocOpenAPI(t, args)

0 commit comments

Comments
 (0)