Skip to content

Commit

Permalink
support proto oneof, multi-line descriptions with yaml, native int sc…
Browse files Browse the repository at this point in the history
…hemas, 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]>
  • Loading branch information
shashankram authored Apr 4, 2024
1 parent 0617ff6 commit 510e2d5
Show file tree
Hide file tree
Showing 32 changed files with 1,238 additions and 146 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test

on:
pull_request:
branches: [main]

jobs:
go:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup Go 1.22
uses: actions/setup-go@v4
with:
go-version: '1.22.x'

- name: Run tests
run: make build-and-test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ _output/
# Local binary produced by make build
protoc-gen-openapi
out/
_output/
51 changes: 44 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@

all: build run

build:
go build -o protoc-gen-openapi
build-and-test: build gotest

ROOTDIR := $(shell pwd)
OUTPUTDIR = $(ROOTDIR)/_output
BINDIR = $(OUTPUTDIR)/.bin

.PHONY: install-deps
install-deps: install-protoc
mkdir -p $(BINDIR)
GOBIN=$(BINDIR) go install github.com/golang/protobuf/protoc-gen-go

build: install-deps
mkdir -p $(BINDIR)
go build -o $(BINDIR)/protoc-gen-openapi

run:
rm -fr out
mkdir out
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
rm -fr $(OUTPUTDIR)
mkdir -p $(OUTPUTDIR)
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

gotest:
go test
PATH=$(BINDIR):$(PATH) go test -v ./...

PROTOC_VERSION:=3.15.8
PROTOC_URL:=https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}
.PHONY: install-protoc
.SILENT: install-protoc
install-protoc:
mkdir -p $(BINDIR)
if [ $(shell ${BINDIR}/protoc --version | grep -c ${PROTOC_VERSION}) -ne 0 ]; then \
echo expected protoc version ${PROTOC_VERSION} already installed ;\
else \
if [ "$(shell uname)" = "Darwin" ]; then \
echo "downloading protoc for osx" ;\
wget $(PROTOC_URL)-osx-x86_64.zip -O $(BINDIR)/protoc-${PROTOC_VERSION}.zip ;\
elif [ "$(shell uname -m)" = "aarch64" ]; then \
echo "downloading protoc for linux aarch64" ;\
wget $(PROTOC_URL)-linux-aarch_64.zip -O $(BINDIR)/protoc-${PROTOC_VERSION}.zip ;\
else \
echo "downloading protoc for linux x86-64" ;\
wget $(PROTOC_URL)-linux-x86_64.zip -O $(BINDIR)/protoc-${PROTOC_VERSION}.zip ;\
fi ;\
unzip $(BINDIR)/protoc-${PROTOC_VERSION}.zip -d $(BINDIR)/protoc-${PROTOC_VERSION} ;\
mv $(BINDIR)/protoc-${PROTOC_VERSION}/bin/protoc $(BINDIR)/protoc ;\
chmod +x $(BINDIR)/protoc ;\
rm -rf $(BINDIR)/protoc-${PROTOC_VERSION} $(BINDIR)/protoc-${PROTOC_VERSION}.zip ;\
fi

clean:
@rm -fr out protoc-gen-openapi
@rm -rf $(OUTPUTDIR)
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,17 @@ on Debian or Ubuntu, you can install it from the package manager:
sudo apt-get install -y golang
```

To build, first ensure you have the protocol compiler (protoc):

```bash
go get github.com/golang/protobuf/proto
```
To build, run the following command from this project directory:

```bash
go build
make build
```

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

```bash
cp protoc-gen-openapi $HOME/bin
cp _output/.bin/protoc-gen-openapi $HOME/bin
```

Since the following is often in your `$HOME/.bashrc` file:
Expand Down Expand Up @@ -69,7 +64,13 @@ Other supported options are:
* when set to `true`, the output is in yaml file.
* `include_description`
* when set to `true`, the openapi schema will include descriptions, generated from the proto message comment.
* `multiline_description`
* 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`.
* `enum_as_int_or_string`
* when set to `true`, the openapi schema will include `x-kubernetes-int-or-string` on enums.
* `additional_empty_schemas`
* a `+` separated list of message names (`core.solo.io.Status`), whose generated schema should be an empty object that accepts all values.
* a `+` separated list of message names (`core.solo.io.Status`), whose generated schema should be an empty object that accepts all values.
* `proto_oneof`
* when set to `true`, the openapi schema will include `oneOf` emulating the behavior of proto `oneof`.
* `int_native`
* 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
12 changes: 12 additions & 0 deletions changelog/v0.2.0/oneof-misc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
changelog:
- type: BREAKING_CHANGE
issueLink: https://github.com/solo-io/protoc-gen-openapi/issues/20
resolvesIssue: true
description: |
Adds support for the following:
- encodes proto oneof to OpenAPI oneOf schema
- multi-line descriptions for properties when using yaml=true
- fixes uint32 schema to use min-max and not int32 format
- adds proto for custom field options support
- updates go protobuf packages to use new lib as per https://protobuf.dev/reference/go/faq/#versions
- build changes for proto code generation
19 changes: 13 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
module github.com/solo-io/protoc-gen-openapi

go 1.18
go 1.22

require (
github.com/getkin/kin-openapi v0.80.0
github.com/ghodss/yaml v1.0.0
github.com/golang/protobuf v1.3.2
github.com/golang/protobuf v1.5.3
google.golang.org/protobuf v1.31.0
sigs.k8s.io/controller-tools v0.14.0
)

require (
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.19.5 // indirect
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/tools v0.16.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.29.0 // indirect
)
77 changes: 67 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,34 +1,91 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/getkin/kin-openapi v0.80.0 h1:W/s5/DNnDCR8P+pYyafEWlGk4S7/AfQUWXgrRSSAzf8=
github.com/getkin/kin-openapi v0.80.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g=
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/apimachinery v0.29.0 h1:+ACVktwyicPz0oc6MTMLwa2Pw3ouLAfAon1wPLtG48o=
k8s.io/apimachinery v0.29.0/go.mod h1:eVBxQ/cwiJxH58eK/jd/vAk4mrxmVlnpBH5J2GbMeis=
sigs.k8s.io/controller-tools v0.14.0 h1:rnNoCC5wSXlrNoBKKzL70LNJKIQKEzT6lloG6/LF73A=
sigs.k8s.io/controller-tools v0.14.0/go.mod h1:TV7uOtNNnnR72SpzhStvPkoS/U5ir0nMudrkrC4M9Sc=
65 changes: 47 additions & 18 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,60 +28,89 @@ const goldenDir = "testdata/golden/"
func TestOpenAPIGeneration(t *testing.T) {
testcases := []struct {
name string
id string
perPackage bool
genOpts string
inputFiles map[string][]string
wantFiles []string
}{
{
name: "Per Package Generation",
id: "test1",
perPackage: true,
genOpts: "",
wantFiles: []string{"testpkg.json", "testpkg2.json"},
inputFiles: map[string][]string{
"testpkg": {"./testdata/testpkg/test1.proto", "./testdata/testpkg/test2.proto", "./testdata/testpkg/test6.proto"},
"testpkg2": {"./testdata/testpkg2/test3.proto"},
},
wantFiles: []string{"testpkg.json", "testpkg2.json"},
},
{
name: "Single File Generation",
id: "test2",
perPackage: false,
genOpts: "single_file=true",
wantFiles: []string{"openapiv3.json"},
inputFiles: map[string][]string{
"testpkg": {"./testdata/testpkg/test1.proto", "./testdata/testpkg/test2.proto", "./testdata/testpkg/test6.proto"},
"testpkg2": {"./testdata/testpkg2/test3.proto"},
},
wantFiles: []string{"openapiv3.json"},
},
{
name: "Use $ref in the output",
id: "test3",
perPackage: false,
genOpts: "single_file=true,use_ref=true",
wantFiles: []string{"testRef/openapiv3.json"},
inputFiles: map[string][]string{
"testpkg": {"./testdata/testpkg/test1.proto", "./testdata/testpkg/test2.proto", "./testdata/testpkg/test6.proto"},
"testpkg2": {"./testdata/testpkg2/test3.proto"},
},
wantFiles: []string{"testRef/openapiv3.json"},
},
{
name: "Use yaml, proto_oneof, int_native, validation rules, and multiline_description",
id: "test4",
perPackage: false,
genOpts: "yaml=true,single_file=true,proto_oneof=true,int_native=true,multiline_description=true",
inputFiles: map[string][]string{
"testpkg": {"./testdata/testpkg/test1.proto", "./testdata/testpkg/test2.proto", "./testdata/testpkg/test6.proto"},
"testpkg2": {"./testdata/testpkg2/test3.proto"},
},
wantFiles: []string{"test4/openapiv3.yaml"},
},
{
name: "Test validation rules",
id: "test5",
perPackage: false,
genOpts: "yaml=true,single_file=true,proto_oneof=true,int_native=true,multiline_description=true",
inputFiles: map[string][]string{
"test5": {"./testdata/test5/rules.proto"},
},
wantFiles: []string{"test5/openapiv3.yaml"},
},
}

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

// we assume that the package name is the same as the name of the folder containing the proto files.
packages := make(map[string][]string)
err = filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".proto") {
dir := filepath.Dir(path)
packages[dir] = append(packages[dir], path)
}
return nil
})
tempDir, err := os.MkdirTemp("", "openapi-temp")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)

if tc.perPackage {
for _, files := range packages {
for _, files := range tc.inputFiles {
args := []string{"-Itestdata", "--openapi_out=" + tc.genOpts + ":" + tempDir}
args = append(args, files...)
protocOpenAPI(t, args)
}
} else {
args := []string{"-Itestdata", "--openapi_out=" + tc.genOpts + ":" + tempDir}
for _, files := range packages {
for _, files := range tc.inputFiles {
args = append(args, files...)
}
protocOpenAPI(t, args)
Expand Down
Loading

0 comments on commit 510e2d5

Please sign in to comment.