Skip to content

Commit 0fdd4c9

Browse files
authored
refactor: 修改 protoc-gen-connect-openapi 并部分重构 (#10)
1 parent 485adad commit 0fdd4c9

File tree

260 files changed

+58860
-13059
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

260 files changed

+58860
-13059
lines changed

.github/workflows/go.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,16 @@ Package.resolved
2121
extensions/sample/generated
2222
/.proto
2323
/vendor
24+
/bin
25+
/proto-vendor
26+
27+
# Go workspace file
28+
go.work
29+
30+
redoc-static.html
31+
32+
out
33+
dist
34+
35+
coverage.out
36+
coverage.html

.goreleaser.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
builds:
22
- main: ./main.go
3-
id: protoc-gen-openapi
43
binary: protoc-gen-openapi
5-
skip: false
64
goos:
75
- linux
86
- darwin
97
- windows
108
goarch:
119
- amd64
10+
- arm64
11+
env:
12+
- CGO_ENABLED=0
13+
mod_timestamp: "{{ .CommitTimestamp }}"
14+
ldflags:
15+
- "-X '{{ .ModulePath }}/version.version={{ .Version }}'"
16+
- "-X '{{ .ModulePath }}/version.commit={{ .Commit }}'"
17+
- "-X '{{ .ModulePath }}/version.date={{ .CommitTimestamp }}'"
1218
archives:
1319
- name_template: "{{ .Binary }}-{{ .Tag }}-{{ .Os }}-{{ .Arch }}"
1420
format: tar.gz

Makefile

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
test:
2-
# since some tests call separately-built binaries, clear the cache to ensure all get run
3-
go clean -testcache
4-
go test ./... -v
5-
61
vet:
72
go vet ./...
83

9-
pb:
10-
protobuild vendor
11-
12-
pb_test:
13-
protobuild -c protobuf_test.yaml vendor
14-
protobuild -c protobuf_test.yaml gen
15-
16-
install_gnostic:
17-
go install github.com/google/gnostic/cmd/[email protected]
18-
194
protobuf:
205
protobuild vendor
216
protobuild gen
22-
mv github.com/pubgo/protoc-gen-openapi/generator/service.pb.go ./generator
7+
rm -rf generator
8+
mv github.com/pubgo/protoc-gen-openapi/generator generator
239
rm -rf github.com
10+
11+
protobuf_test:
12+
protobuild vendor -c protobuf_auth_base.yaml
13+
protobuild gen -c protobuf_auth_base.yaml
14+
15+
install:
16+
go install .

README.md

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,31 @@
11
# protoc-gen-openapi
22

3-
> This directory contains a protoc plugin that generates an
4-
OpenAPI description for a REST API that corresponds to a
5-
Protocol Buffer service.
3+
- 本项目一个 protobuf openapi 插件的实现
4+
- 本项目早期参考了[protoc-gen-openapi](https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi), 由于 gnostic 长时间不更新,并发现了优秀的替代品[protoc-gen-connect-openapi](https://github.com/sudorandom/protoc-gen-connect-openapi), 现在基于[protoc-gen-connect-openapi](https://github.com/sudorandom/protoc-gen-connect-openapi) 做了部分修改
5+
- internal/converter 大部分都是来自于 protoc-gen-connect-openapi, copy.go 部分是自己的实现
66

7-
> This project is a simplification of the [google/gnostic](https://github.com/google/gnostic/tree/main/openapiv3) project, dedicated to providing a simple version of the protoc-gen-openapi
7+
## 为什么修改 protoc-gen-connect-openapi
8+
- protoc-gen-connect-openapi 刚好和本项目早期底层使用一致, 为本项目重构提供了很好的思路和想法
9+
- protoc-gen-connect-openapi 是为了 connect-go 生成 openapi 文档, 包含不少 connect 关键词
10+
- protoc-gen-connect-openapi 不支持 grpc service 级别的注解
11+
- protoc-gen-connect-openapi 针对企业内部业务级别的修改无法支持
812

9-
Installation:
13+
## 改动部分
1014

11-
go install github.com/pubgo/protoc-gen-openapi@latest
15+
- 使用原生的 [google.golang.org/protobuf/compiler/protogen](https://pkg.go.dev/google.golang.org/protobuf/compiler/protogen)
16+
- 添加了 grpc service 级别的注解 [service.proto](./proto/openapiv3/service.proto), [example](./examples/tests/openapiv3annotations/message.proto)
17+
- 添加了 [lava](https://github.com/pubgo/lava) 相关的 header 信息
18+
- 添加了 Error Code 定义 [error.proto](https://github.com/pubgo/funk/blob/master/proto/errorpb/errors.proto)
19+
- 使用 [protobuild](https://github.com/pubgo/protobuild) 进行 protobuf 构建和管理
20+
21+
## Installation:
1222

13-
Usage:
23+
go install github.com/pubgo/protoc-gen-openapi@latest
1424

15-
protoc sample.proto -I=. --openapi_out=.
25+
## 使用
1626

17-
This runs the plugin for a file named `sample.proto` which
18-
refers to additional .proto files in the same directory as
19-
`sample.proto`. Output is written to the current directory.
27+
参考 `make protobuf_test`
2028

21-
## options
29+
## 计划
2230

23-
1. `version`: version number text, e.g. 1.2.3
24-
- **default**: `0.0.1`
25-
2. `title`: name of the API
26-
- **default**: empty string or service name if there is only one service
27-
3. `description`: description of the API
28-
- **default**: empty string or service description if there is only one service
29-
4. `naming`: naming convention. Use "proto" for passing names directly from the proto files
30-
- **default**: `json`
31-
- `json`: will turn field `updated_at` to `updatedAt`
32-
- `proto`: keep field `updated_at` as it is
33-
5. `fq_schema_naming`: schema naming convention. If "true", generates fully-qualified schema names by prefixing them with the proto message package name
34-
- **default**: false
35-
- `false`: keep message `Book` as it is
36-
- `true`: turn message `Book` to `google.example.library.v1.Book`, it is useful when there are same named message in different package
37-
6. `enum_type`: type for enum serialization. Use "string" for string-based serialization
38-
- **default**: `integer`
39-
- `integer`: setting type to `integer`
40-
```yaml
41-
schema:
42-
type: integer
43-
format: enum
44-
```
45-
- `string`: setting type to `string`, and list available values in `enum`
46-
```yaml
47-
schema:
48-
enum:
49-
- UNKNOWN_KIND
50-
- KIND_1
51-
- KIND_2
52-
type: string
53-
format: enum
54-
```
55-
7. `depth`: depth of recursion for circular messages
56-
- **default**: 2, this depth only used in query parameters, usually 2 is enough
57-
8. `default_response`: add default response. If "true", automatically adds a default response to operations which use the google.rpc.Status message.
58-
Useful if you use envoy or grpc-gateway to transcode as they use this type for their default error responses.
59-
- **default**: true, this option will add this default response for each method as following:
60-
```yaml
61-
default:
62-
description: Default error response
63-
content:
64-
application/json:
65-
schema:
66-
$ref: '#/components/schemas/google.rpc.Status'
67-
```
31+
- 长期兼容 gnostic, 兼容 protoc-gen-connect-openapi, 后期会进行重构

auth-base.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
openapi: 3.1.0
2+
info:
3+
title: MyProject
4+
description: "My Project Description"
5+
version: v1.0.0
6+
# 1) Define the security scheme type (HTTP bearer)
7+
components:
8+
securitySchemes:
9+
bearerAuth: # arbitrary name for the security scheme
10+
type: http
11+
scheme: bearer
12+
bearerFormat: JWT # optional, arbitrary value for documentation purposes
13+
# 2) Apply the security globally to all operations
14+
security:
15+
- bearerAuth: [] # use the same name as above

docs/_docs.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
package docs
22

3-
// https://github.com/kollalabs/protoc-gen-openapi/tree/main
3+
// https://github.com/kollalabs/protoc-gen-openapi
4+
// https://github.com/google/gnostic
5+
// https://github.com/sudorandom/protoc-gen-connect-openapi

examples/basic.proto

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Example description, used in
2+
syntax = "proto3";
3+
4+
package example.basic;
5+
6+
import "buf/validate/validate.proto";
7+
import "openapiv3/annotations.proto";
8+
9+
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
10+
option java_multiple_files = true;
11+
option java_outer_classname = "HelloWorldProto";
12+
option java_package = "io.grpc.examples.helloworld";
13+
// Full list of options can be seen here: https://github.com/google/gnostic/blob/main/openapiv3/openapi.v3.proto
14+
// File options (openapi.v3.document): Document message
15+
// Method options (openapi.v3.operation): Operation message
16+
// Message options (openapi.v3.schema): Schema message
17+
// Field options (openapi.v3.property): Schema message
18+
option (openapi.v3.document) = {
19+
info: {
20+
title: "Hello World"
21+
version: "v2"
22+
description: "This is a service which says hello to you!"
23+
contact: {
24+
name: "Ein"
25+
url: "https://github.com/sudorandom/protoc-gen-connect-openapi"
26+
27+
}
28+
license: {
29+
name: "MIT License"
30+
url: "https://github.com/sudorandom/protoc-gen-connect-openapi/blob/master/LICENSE"
31+
}
32+
}
33+
components: {
34+
security_schemes: {
35+
additional_properties: [
36+
{
37+
name: "BasicAuth"
38+
value: {
39+
security_scheme: {
40+
type: "http"
41+
scheme: "basic"
42+
}
43+
}
44+
}
45+
]
46+
}
47+
}
48+
};
49+
50+
// The greeting service definition.
51+
service Greeter {
52+
// Sends a greeting
53+
rpc SayHello(HelloRequest) returns (HelloReply) {
54+
option idempotency_level = NO_SIDE_EFFECTS;
55+
option (openapi.v3.operation).description = "This is a description just for OpenAPI";
56+
}
57+
58+
// Writes a greeting (has side effects)
59+
rpc WriteHello(HelloRequest) returns (Response) {}
60+
}
61+
62+
// The request message containing the user's name.
63+
message HelloRequest {
64+
string name = 1 [
65+
(buf.validate.field).string = {
66+
min_len: 3
67+
max_len: 100
68+
},
69+
(openapi.v3.property) = {
70+
example: {yaml: "Ein"}
71+
}
72+
];
73+
uint32 hello_count = 2 [(buf.validate.field).uint32.lt = 42];
74+
}
75+
76+
// The response message containing the greetings
77+
message HelloReply {
78+
string message = 1;
79+
}
80+
81+
message Response {
82+
repeated HelloReply repl = 1;
83+
}

0 commit comments

Comments
 (0)