Skip to content

Commit

Permalink
add deprecated tag (#1)
Browse files Browse the repository at this point in the history
* fix: 2024-05-25 15:45:21

* add tag query and parameter
  • Loading branch information
kooksee authored May 30, 2024
1 parent 02b52d0 commit 9d6a282
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 54 deletions.
41 changes: 41 additions & 0 deletions examples/tests/deprecated/message.openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated with protoc-gen-openapi
# https://github.com/pubgo/protoc-gen-openapi

openapi: 3.0.3
info:
title: openapi examples
version: 0.0.1
paths:
/v1/messages:deprecated:
put:
tags:
- Messaging1
operationId: Messaging1_UpdateMessage
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Messagedeprecated'
required: true
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Messagedeprecated'
components:
schemas:
Messagedeprecated:
type: object
properties:
id:
type: string
label:
deprecated: true
type: string
label1:
deprecated: true
type: string
tags:
- name: Messaging1
45 changes: 45 additions & 0 deletions examples/tests/deprecated/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

syntax = "proto3";

package tests.deprecated.message.v1;

import "google/api/annotations.proto";
import "openapiv3/annotations.proto";

option go_package = "github.com/google/gnostic/apps/protoc-gen-openapi/examples/tests/noannotations/message/v1;message";

service Messaging1 {
rpc UpdateMessage (Messagedeprecated) returns (Messagedeprecated) {
option (google.api.http) = {
put: "/v1/messages:deprecated"
body: "*"
};
}
}

message Messagedeprecated {
int64 id = 1;
string label = 2 [
(openapi.v3.property) = {
deprecated: true
}
];

string label1 = 3 [
deprecated = true
];
}
8 changes: 8 additions & 0 deletions examples/tests/openapiv3annotations/message.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ paths:
required: true
schema:
type: string
- name: Authorization
in: header
description: jwt token
required: true
schema:
type: string
requestBody:
content:
application/json:
Expand All @@ -40,6 +46,8 @@ paths:
$ref: '#/components/schemas/Message'
security:
- BasicAuth: []
x-operation-id: updateMessage
abc: def
components:
schemas:
Message:
Expand Down
126 changes: 72 additions & 54 deletions examples/tests/openapiv3annotations/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,72 +23,90 @@ import "openapiv3/annotations.proto";
option go_package = "github.com/google/gnostic/apps/protoc-gen-openapi/examples/tests/openapiv3annotations/message/v1;message";

option (openapi.v3.document) = {
info: {
title: "Title from annotation";
version: "Version from annotation";
description: "Description from annotation";
contact: {
name: "Contact Name";
url: "https://github.com/google/gnostic";
email: "[email protected]";
}
license: {
name: "Apache License";
url: "https://github.com/google/gnostic/blob/master/LICENSE";
info: {
title: "Title from annotation";
version: "Version from annotation";
description: "Description from annotation";
contact: {
name: "Contact Name";
url: "https://github.com/google/gnostic";
email: "[email protected]";
}
license: {
name: "Apache License";
url: "https://github.com/google/gnostic/blob/master/LICENSE";
}
}
}
components: {
security_schemes: {
additional_properties: [
{
name: "BasicAuth";
value: {
security_scheme: {
type: "http";
scheme: "basic";
}
}
components: {
security_schemes: {
additional_properties: [
{
name: "BasicAuth";
value: {
security_scheme: {
type: "http";
scheme: "basic";
}
}
}
]
}
]
}
}
};

service Messaging1 {
rpc UpdateMessage(Message) returns(Message) {
option(google.api.http) = {
patch: "/v1/messages/{message_id}"
body: "*"
};
option(openapi.v3.operation) = {
security: [
{
additional_properties: [
{
name: "BasicAuth";
value: {}
}
rpc UpdateMessage (Message) returns (Message) {
option (google.api.http) = {
patch: "/v1/messages/{message_id}"
body: "*"
};

option (openapi.v3.operation) = {
tags: ["abc=def"],
specification_extension: [
{
name: "x-operation-id";
value: {
yaml: "updateMessage"
};
}
],
parameters: [{
parameter: {
description: "jwt token",
in: "header",
name: "Authorization",
required: true,
},
}],
security: [
{
additional_properties: [
{
name: "BasicAuth";
value: {}
}
]
}
]
}
]
};
}
};
}
}

service Messaging2 {
rpc UpdateMessage(Message) returns (Message) {}
rpc UpdateMessage (Message) returns (Message) {}
}

message Message {
option (openapi.v3.schema) = {
title: "This is an overridden message schema title";
};
option (openapi.v3.schema) = {
title: "This is an overridden message schema title";
};

int64 id = 1;
string label = 2 [
(openapi.v3.property) = {
title: "this is an overriden field schema title";
max_length: 255;
}
];
int64 id = 1;
string label = 2 [
(openapi.v3.property) = {
title: "this is an overriden field schema title";
max_length: 255;
}
];
}
39 changes: 39 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
any_pb "google.golang.org/protobuf/types/known/anypb"

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

for _, v := range op.Parameters {
if v.Oneof == nil {
continue
}

switch v1 := v.Oneof.(type) {
case *v3.ParameterOrReference_Parameter:
p := v1.Parameter
if p.In == "header" {
if p.Schema == nil {
p.Schema = wk.NewStringSchema()
}
}
}
}

var tags []string
for _, v := range op.Tags {
if strings.Contains(v, "=") {
tagNames := strings.SplitN(v, "=", 2)
op.SpecificationExtension = append(op.SpecificationExtension, &v3.NamedAny{
Name: strings.TrimSpace(tagNames[0]),
Value: &v3.Any{
Yaml: strings.TrimSpace(tagNames[1]),
},
})
continue
}

tags = append(tags, v)
}

op.Tags = tags
g.addOperationToDocumentV3(d, op, path2, methodName)
}
}
Expand Down Expand Up @@ -871,6 +905,11 @@ func (g *OpenAPIv3Generator) addSchemasForMessagesToDocumentV3(d *v3.Document, m
}
schema.Schema.ReadOnly = outputOnly
schema.Schema.WriteOnly = inputOnly
if opt, ok := field.Desc.Options().(*descriptorpb.FieldOptions); ok && opt != nil {
if opt.Deprecated != nil && *opt.Deprecated {
schema.Schema.Deprecated = *opt.Deprecated
}
}

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

0 comments on commit 9d6a282

Please sign in to comment.