Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(template): replace openapiv2 generator with gnostic #153

Merged
merged 9 commits into from
Nov 18, 2022
Merged
4 changes: 2 additions & 2 deletions _template/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ GO_DEPENDENCIES = google.golang.org/protobuf/cmd/protoc-gen-go \
{{- if .Extensions.grpc.grpcGateway }}
# additional dependencies for grpc-gateway
GO_DEPENDENCIES += github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
github.com/google/gnostic/cmd/protoc-gen-openapi
{{- end }}

define make-go-dependency
Expand All @@ -106,7 +106,7 @@ protobreaking: api/proto/buf.lock bin/protoc-gen-buf-breaking ## Compares your c
bin/buf breaking --against '.git#branch=main'

generate: ## Generates code from protobuf files
generate: {{if .Extensions.grpc.grpcGateway}}bin/protoc-gen-grpc-gateway bin/protoc-gen-openapiv2{{end}} api/proto/buf.lock bin/protoc-gen-go bin/protoc-gen-go-grpc bin/protoc-gen-validate
generate: {{if .Extensions.grpc.grpcGateway}}bin/protoc-gen-grpc-gateway bin/protoc-gen-openapi{{end}} api/proto/buf.lock bin/protoc-gen-go bin/protoc-gen-go-grpc bin/protoc-gen-validate
PATH=$(PWD)/bin:$$PATH buf generate
{{- end }}

Expand Down
2 changes: 1 addition & 1 deletion _template/api/proto/buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ deps:
- buf.build/envoyproxy/protoc-gen-validate
- buf.build/googleapis/googleapis
{{- if .Extensions.grpc.grpcGateway }}
- buf.build/grpc-ecosystem/grpc-gateway
- buf.build/gnostic/gnostic
{{- end }}
lint:
use:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";
import "validate/validate.proto";
{{- if .Extensions.grpc.grpcGateway }}
import "protoc-gen-openapiv2/options/annotations.proto";
// weak, otherwise gnostic imports broken imports for gen-go
// weak import: proto definition only adds options, no additional messages.
// buf:lint:ignore IMPORT_NO_WEAK
import weak "gnostic/openapi/v3/annotations.proto"; // Will not import _ "" in the gen-go files
{{- end }}

// Defines the import path that should be used to import the generated package,
Expand All @@ -18,15 +21,31 @@ option go_package = "{{.Base.moduleName | replace `.git` ``}}/internal/pkg/pb;pb

{{- if .Extensions.grpc.grpcGateway }}
// These annotations are used when generating the OpenAPI file.
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
swagger: "2.0"
option (gnostic.openapi.v3.document) = {
info: {
title: "{{.Base.appName}}-api"
version: "1.0"
danielzwink marked this conversation as resolved.
Show resolved Hide resolved
description: "{{.Base.projectDescription}}"
title: "{{.Base.appName}}-api";
version: "1.0";
description: "{{.Base.projectDescription}}";
}
components: {
security_schemes: {
additional_properties: [
{
name: "BasicAuth";
value: {
security_scheme: {
type: "http";
scheme: "basic";
}
}
}
]
}
}
servers : {
url: "http://localhost:8080";
description: "Host Server"
}
schemes: HTTP
host: "localhost:8080"
};
{{- end }}

Expand All @@ -36,7 +55,7 @@ service {{ .Base.appName | title | replace "-" "" }}Service {
option (google.api.http) = {
get: "/v1/example"
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
option (gnostic.openapi.v3.operation) = {
summary: "Get example response"
description: "Returns an empty example response"
tags: "example"
Expand Down
2 changes: 1 addition & 1 deletion _template/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins:
- name: grpc-gateway
out: internal/pkg/api/pb
opt: paths=source_relative
- name: openapiv2
- name: openapi
out: api/openapi
{{- end }}
- name: validate
Expand Down
2 changes: 1 addition & 1 deletion _template/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import (
_ "google.golang.org/protobuf/cmd/protoc-gen-go"
{{- if .Extensions.grpc.grpcGateway }}
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway"
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"
_ "github.com/google/gnostic/cmd/protoc-gen-openapi"
{{- end }}
)
9 changes: 4 additions & 5 deletions embed_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.