Skip to content

Commit cf3273d

Browse files
author
Josh Newman
committed
feat: add tag groups extension
1 parent 86256b1 commit cf3273d

File tree

12 files changed

+126
-30
lines changed

12 files changed

+126
-30
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ go install github.com/technicallyjosh/protoc-gen-openapi@latest
3030
| `version` | The version of the API. | 0.0.1 |
3131
| `title` | The title of the API. | |
3232
| `description` | A description of the API. | |
33-
| `ignore` | A list of package names to ignore delimited with a pipe. | |
33+
| `ignore` | A list of proto package names to ignore delimited by pipes. | |
3434
| `default_response` | The default response to be used.<sup>1</sup> | |
3535
| `content_type` | The content type to be associated with all operations.<sup>1</sup> | application/json |
3636
| `json_names` | Use the JSON names that Protobuf provides. Otherwise, proto field names are used. | false |
@@ -149,6 +149,7 @@ service MyService {
149149
```
150150

151151
## Features In Progress
152+
152153
- Path Parameters and Query
153154
- Field requirements (e.g. `>` `<` etc...)
154155

api/oapi/v1/field.pb.go

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

api/oapi/v1/field.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extend google.protobuf.FieldOptions {
1414
// comments.
1515
string example = 5151;
1616

17+
// Options for a field.
1718
FieldOptions options = 5152;
1819
}
1920

api/oapi/v1/service.pb.go

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

api/oapi/v1/service.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ message ServiceOptions {
2929
// Sets the "x-displayName" extension property for the OAPI tag. This is used
3030
// by redocly/redoc for a friendly tag name.
3131
string display_name = 5;
32+
33+
// Adds the service to the extension x-tagGroups. If the group doesn't exist,
34+
// it is created and the service tag is added to it.
35+
string tag_group = 6;
3236
}

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ go 1.19
44

55
require (
66
github.com/getkin/kin-openapi v0.111.0
7-
github.com/stretchr/testify v1.8.1
87
google.golang.org/protobuf v1.28.1
98
gopkg.in/yaml.v3 v3.0.1
109
)
1110

1211
require (
13-
github.com/davecgh/go-spew v1.1.1 // indirect
1412
github.com/go-openapi/jsonpointer v0.19.5 // indirect
1513
github.com/go-openapi/swag v0.19.5 // indirect
1614
github.com/invopop/yaml v0.1.0 // indirect
1715
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect
1816
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
19-
github.com/pmezard/go-difflib v1.0.0 // indirect
2017
gopkg.in/yaml.v2 v2.4.0 // indirect
2118
)

internal/generator/generator.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ func (g *Generator) Run() error {
8787
// buildDocument builds out the base of the OAPI document with some defaults.
8888
func (g *Generator) buildDocument() (*openapi3.T, error) {
8989
doc := &openapi3.T{
90-
ExtensionProps: openapi3.ExtensionProps{},
91-
OpenAPI: "3.0.3",
90+
ExtensionProps: openapi3.ExtensionProps{
91+
Extensions: make(map[string]any),
92+
},
93+
OpenAPI: "3.0.3",
9294
Components: openapi3.Components{
9395
SecuritySchemes: make(openapi3.SecuritySchemes),
9496
Schemas: make(openapi3.Schemas),

internal/generator/message.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ var (
1313

1414
type messageMap map[string]*protogen.Message
1515

16+
// Set adds the specified message to the map.
1617
func (m messageMap) Set(val *protogen.Message) {
1718
m[util.FullName(val)] = val
1819
}
1920

21+
// Get returns the message by key or nil.
2022
func (m messageMap) Get(key string) *protogen.Message {
2123
val, ok := m[key]
2224
if !ok {

internal/generator/path.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ func (g *Generator) addPathsToDoc(doc *openapi3.T, services []*protogen.Service)
8888
ExtensionProps: props,
8989
})
9090

91+
tagGroup := strings.TrimSpace(serviceOptions.TagGroup)
92+
if tagGroup != "" {
93+
err := addTagGroup(doc, tagGroup, tagName)
94+
if err != nil {
95+
return err
96+
}
97+
}
98+
9199
for _, method := range service.Methods {
92100
err := g.addOperation(addOperationParams{
93101
doc: doc,
@@ -163,7 +171,7 @@ func (g *Generator) addOperation(p addOperationParams) error {
163171
Tags: []string{p.tagName},
164172
Description: description,
165173
OperationID: operationID,
166-
Servers: &p.doc.Servers,
174+
Servers: &openapi3.Servers{server},
167175
Deprecated: methodOptions.Deprecated,
168176
Responses: make(openapi3.Responses),
169177
Summary: methodOptions.Summary,
@@ -274,8 +282,8 @@ func (g *Generator) addOperation(p addOperationParams) error {
274282
inputFullName := string(p.method.Input.Desc.FullName())
275283
message := allMessages.Get(inputFullName)
276284

277-
// If another type is defined such as google.protobuf.Any, for now we'll just
278-
// exit.
285+
// If another type is defined such as google.protobuf.Any or
286+
// google.protobuf.Empty, for now we'll just exit.
279287
// TODO: support `Any` type for requests 🤔
280288
if message != nil {
281289
requestSchemaRef := &openapi3.SchemaRef{

internal/generator/response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (g *Generator) addDefaultResponse(doc *openapi3.T) error {
2626
Content: openapi3.Content{
2727
*g.config.ContentType: &openapi3.MediaType{
2828
Schema: &openapi3.SchemaRef{
29-
Ref: "#/components/schemas/" + name,
29+
Ref: newSchemaRef(name),
3030
},
3131
},
3232
},

0 commit comments

Comments
 (0)