Skip to content

Commit 9d6a282

Browse files
authored
add deprecated tag (#1)
* fix: 2024-05-25 15:45:21 * add tag query and parameter
1 parent 02b52d0 commit 9d6a282

File tree

5 files changed

+205
-54
lines changed

5 files changed

+205
-54
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Generated with protoc-gen-openapi
2+
# https://github.com/pubgo/protoc-gen-openapi
3+
4+
openapi: 3.0.3
5+
info:
6+
title: openapi examples
7+
version: 0.0.1
8+
paths:
9+
/v1/messages:deprecated:
10+
put:
11+
tags:
12+
- Messaging1
13+
operationId: Messaging1_UpdateMessage
14+
requestBody:
15+
content:
16+
application/json:
17+
schema:
18+
$ref: '#/components/schemas/Messagedeprecated'
19+
required: true
20+
responses:
21+
"200":
22+
description: OK
23+
content:
24+
application/json:
25+
schema:
26+
$ref: '#/components/schemas/Messagedeprecated'
27+
components:
28+
schemas:
29+
Messagedeprecated:
30+
type: object
31+
properties:
32+
id:
33+
type: string
34+
label:
35+
deprecated: true
36+
type: string
37+
label1:
38+
deprecated: true
39+
type: string
40+
tags:
41+
- name: Messaging1
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2021 Google LLC.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
syntax = "proto3";
17+
18+
package tests.deprecated.message.v1;
19+
20+
import "google/api/annotations.proto";
21+
import "openapiv3/annotations.proto";
22+
23+
option go_package = "github.com/google/gnostic/apps/protoc-gen-openapi/examples/tests/noannotations/message/v1;message";
24+
25+
service Messaging1 {
26+
rpc UpdateMessage (Messagedeprecated) returns (Messagedeprecated) {
27+
option (google.api.http) = {
28+
put: "/v1/messages:deprecated"
29+
body: "*"
30+
};
31+
}
32+
}
33+
34+
message Messagedeprecated {
35+
int64 id = 1;
36+
string label = 2 [
37+
(openapi.v3.property) = {
38+
deprecated: true
39+
}
40+
];
41+
42+
string label1 = 3 [
43+
deprecated = true
44+
];
45+
}

examples/tests/openapiv3annotations/message.openapi.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ paths:
2525
required: true
2626
schema:
2727
type: string
28+
- name: Authorization
29+
in: header
30+
description: jwt token
31+
required: true
32+
schema:
33+
type: string
2834
requestBody:
2935
content:
3036
application/json:
@@ -40,6 +46,8 @@ paths:
4046
$ref: '#/components/schemas/Message'
4147
security:
4248
- BasicAuth: []
49+
x-operation-id: updateMessage
50+
abc: def
4351
components:
4452
schemas:
4553
Message:

examples/tests/openapiv3annotations/message.proto

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,72 +23,90 @@ import "openapiv3/annotations.proto";
2323
option go_package = "github.com/google/gnostic/apps/protoc-gen-openapi/examples/tests/openapiv3annotations/message/v1;message";
2424

2525
option (openapi.v3.document) = {
26-
info: {
27-
title: "Title from annotation";
28-
version: "Version from annotation";
29-
description: "Description from annotation";
30-
contact: {
31-
name: "Contact Name";
32-
url: "https://github.com/google/gnostic";
33-
34-
}
35-
license: {
36-
name: "Apache License";
37-
url: "https://github.com/google/gnostic/blob/master/LICENSE";
26+
info: {
27+
title: "Title from annotation";
28+
version: "Version from annotation";
29+
description: "Description from annotation";
30+
contact: {
31+
name: "Contact Name";
32+
url: "https://github.com/google/gnostic";
33+
34+
}
35+
license: {
36+
name: "Apache License";
37+
url: "https://github.com/google/gnostic/blob/master/LICENSE";
38+
}
3839
}
39-
}
40-
components: {
41-
security_schemes: {
42-
additional_properties: [
43-
{
44-
name: "BasicAuth";
45-
value: {
46-
security_scheme: {
47-
type: "http";
48-
scheme: "basic";
49-
}
50-
}
40+
components: {
41+
security_schemes: {
42+
additional_properties: [
43+
{
44+
name: "BasicAuth";
45+
value: {
46+
security_scheme: {
47+
type: "http";
48+
scheme: "basic";
49+
}
50+
}
51+
}
52+
]
5153
}
52-
]
5354
}
54-
}
5555
};
5656

5757
service Messaging1 {
58-
rpc UpdateMessage(Message) returns(Message) {
59-
option(google.api.http) = {
60-
patch: "/v1/messages/{message_id}"
61-
body: "*"
62-
};
63-
option(openapi.v3.operation) = {
64-
security: [
65-
{
66-
additional_properties: [
67-
{
68-
name: "BasicAuth";
69-
value: {}
70-
}
58+
rpc UpdateMessage (Message) returns (Message) {
59+
option (google.api.http) = {
60+
patch: "/v1/messages/{message_id}"
61+
body: "*"
62+
};
63+
64+
option (openapi.v3.operation) = {
65+
tags: ["abc=def"],
66+
specification_extension: [
67+
{
68+
name: "x-operation-id";
69+
value: {
70+
yaml: "updateMessage"
71+
};
72+
}
73+
],
74+
parameters: [{
75+
parameter: {
76+
description: "jwt token",
77+
in: "header",
78+
name: "Authorization",
79+
required: true,
80+
},
81+
}],
82+
security: [
83+
{
84+
additional_properties: [
85+
{
86+
name: "BasicAuth";
87+
value: {}
88+
}
89+
]
90+
}
7191
]
72-
}
73-
]
74-
};
75-
}
92+
};
93+
}
7694
}
7795

7896
service Messaging2 {
79-
rpc UpdateMessage(Message) returns (Message) {}
97+
rpc UpdateMessage (Message) returns (Message) {}
8098
}
8199

82100
message Message {
83-
option (openapi.v3.schema) = {
84-
title: "This is an overridden message schema title";
85-
};
101+
option (openapi.v3.schema) = {
102+
title: "This is an overridden message schema title";
103+
};
86104

87-
int64 id = 1;
88-
string label = 2 [
89-
(openapi.v3.property) = {
90-
title: "this is an overriden field schema title";
91-
max_length: 255;
92-
}
93-
];
105+
int64 id = 1;
106+
string label = 2 [
107+
(openapi.v3.property) = {
108+
title: "this is an overriden field schema title";
109+
max_length: 255;
110+
}
111+
];
94112
}

generator/generator.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"google.golang.org/protobuf/compiler/protogen"
3030
"google.golang.org/protobuf/proto"
3131
"google.golang.org/protobuf/reflect/protoreflect"
32+
"google.golang.org/protobuf/types/descriptorpb"
3233
any_pb "google.golang.org/protobuf/types/known/anypb"
3334

3435
wk "github.com/pubgo/protoc-gen-openapi/generator/wellknown"
@@ -763,6 +764,39 @@ func (g *OpenAPIv3Generator) addPathsToDocumentV3(d *v3.Document, services []*pr
763764
proto.Merge(op, extOperation.(*v3.Operation))
764765
}
765766

767+
for _, v := range op.Parameters {
768+
if v.Oneof == nil {
769+
continue
770+
}
771+
772+
switch v1 := v.Oneof.(type) {
773+
case *v3.ParameterOrReference_Parameter:
774+
p := v1.Parameter
775+
if p.In == "header" {
776+
if p.Schema == nil {
777+
p.Schema = wk.NewStringSchema()
778+
}
779+
}
780+
}
781+
}
782+
783+
var tags []string
784+
for _, v := range op.Tags {
785+
if strings.Contains(v, "=") {
786+
tagNames := strings.SplitN(v, "=", 2)
787+
op.SpecificationExtension = append(op.SpecificationExtension, &v3.NamedAny{
788+
Name: strings.TrimSpace(tagNames[0]),
789+
Value: &v3.Any{
790+
Yaml: strings.TrimSpace(tagNames[1]),
791+
},
792+
})
793+
continue
794+
}
795+
796+
tags = append(tags, v)
797+
}
798+
799+
op.Tags = tags
766800
g.addOperationToDocumentV3(d, op, path2, methodName)
767801
}
768802
}
@@ -871,6 +905,11 @@ func (g *OpenAPIv3Generator) addSchemasForMessagesToDocumentV3(d *v3.Document, m
871905
}
872906
schema.Schema.ReadOnly = outputOnly
873907
schema.Schema.WriteOnly = inputOnly
908+
if opt, ok := field.Desc.Options().(*descriptorpb.FieldOptions); ok && opt != nil {
909+
if opt.Deprecated != nil && *opt.Deprecated {
910+
schema.Schema.Deprecated = *opt.Deprecated
911+
}
912+
}
874913

875914
// Merge any `Property` annotations with the current
876915
extProperty := proto.GetExtension(field.Desc.Options(), v3.E_Property)

0 commit comments

Comments
 (0)