Skip to content

Commit

Permalink
protoc-gen-openapiv2: RPC visibility setting transitively applied to …
Browse files Browse the repository at this point in the history
…messages (#2880)

* protoc-gen-openapiv2: RPC visibility setting transitively applied to messages

Addresses issue #2691. Skip hidden methods in
findServicesMessagesAndEnumerations.

* Add submessage and enum visibility tests

* fix lint error in visibility proto
  • Loading branch information
erademacher authored Sep 9, 2022
1 parent 73e0eb7 commit c37ecc1
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 110 deletions.
422 changes: 320 additions & 102 deletions examples/internal/proto/examplepb/visibility_rule_echo_service.pb.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ message VisibilityRuleSimpleMessage {
VisibilityEnum an_enum = 10;
}

// MessageInPreviewMethod doesn't define its own visibility restrictions,
// but is only included in a method marked as "PREVIEW", so it will only
// appear if `visibility_restriction_selectors` include "PREVIEW".
message VisibilityRuleMessageInPreviewMethod {
string id = 1;
VisibilityRuleSubMessageInPreviewMethod sub_message = 2;
VisibilityRuleEnumInPreviewMethod enum = 3;
}

// SubMessageInPreviewMethod doesn't define its own visibility restrictions,
// but is only included in a method marked as "PREVIEW", so it will only
// appear if `visibility_restriction_selectors` include "PREVIEW".
message VisibilityRuleSubMessageInPreviewMethod {
string id = 1;
}

// EnumInPreviewMethod doesn't define its own visibility restrictions,
// but is only included in a method marked as "PREVIEW", so it will only
// appear if `visibility_restriction_selectors` include "PREVIEW".
enum VisibilityRuleEnumInPreviewMethod {
VISIBILITY_RULE_ENUM_IN_PREVIEW_METHOD_UNSPECIFIED = 0;
}

// VisibilityRuleEchoService service responds to incoming echo requests.
// Different services will be available in the swagger documentation depending
// based on `google.api.VisibilityRule`s and the set `visibility_restriction_selectors`
Expand All @@ -75,7 +98,7 @@ service VisibilityRuleEchoService {
}
// EchoPreview is a preview API that should only be visible in the OpenAPI spec
// if `visibility_restriction_selectors` includes "PREVIEW".
rpc EchoPreview(VisibilityRuleSimpleMessage) returns (VisibilityRuleSimpleMessage) {
rpc EchoPreview(VisibilityRuleSimpleMessage) returns (VisibilityRuleMessageInPreviewMethod) {
option (google.api.method_visibility).restriction = "PREVIEW";
option (google.api.http) = {
get: "/v1/example/echo_preview"
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/examplepbVisibilityRuleSimpleMessage"
"$ref": "#/definitions/examplepbVisibilityRuleMessageInPreviewMethod"
}
},
"default": {
Expand Down Expand Up @@ -721,6 +721,29 @@
},
"description": "Embedded represents a message embedded in SimpleMessage."
},
"examplepbVisibilityRuleEnumInPreviewMethod": {
"type": "string",
"enum": [
"VISIBILITY_RULE_ENUM_IN_PREVIEW_METHOD_UNSPECIFIED"
],
"default": "VISIBILITY_RULE_ENUM_IN_PREVIEW_METHOD_UNSPECIFIED",
"description": "EnumInPreviewMethod doesn't define its own visibility restrictions,\nbut is only included in a method marked as \"PREVIEW\", so it will only\nappear if `visibility_restriction_selectors` include \"PREVIEW\"."
},
"examplepbVisibilityRuleMessageInPreviewMethod": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"subMessage": {
"$ref": "#/definitions/examplepbVisibilityRuleSubMessageInPreviewMethod"
},
"enum": {
"$ref": "#/definitions/examplepbVisibilityRuleEnumInPreviewMethod"
}
},
"description": "MessageInPreviewMethod doesn't define its own visibility restrictions,\nbut is only included in a method marked as \"PREVIEW\", so it will only\nappear if `visibility_restriction_selectors` include \"PREVIEW\"."
},
"examplepbVisibilityRuleSimpleMessage": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -761,6 +784,15 @@
},
"description": "SimpleMessage represents a simple message sent to the Echo service."
},
"examplepbVisibilityRuleSubMessageInPreviewMethod": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"description": "SubMessageInPreviewMethod doesn't define its own visibility restrictions,\nbut is only included in a method marked as \"PREVIEW\", so it will only\nappear if `visibility_restriction_selectors` include \"PREVIEW\"."
},
"protobufAny": {
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/examplepbVisibilityRuleSimpleMessage"
"$ref": "#/definitions/examplepbVisibilityRuleMessageInPreviewMethod"
}
},
"default": {
Expand Down Expand Up @@ -389,6 +389,29 @@
},
"description": "Embedded represents a message embedded in SimpleMessage."
},
"examplepbVisibilityRuleEnumInPreviewMethod": {
"type": "string",
"enum": [
"VISIBILITY_RULE_ENUM_IN_PREVIEW_METHOD_UNSPECIFIED"
],
"default": "VISIBILITY_RULE_ENUM_IN_PREVIEW_METHOD_UNSPECIFIED",
"description": "EnumInPreviewMethod doesn't define its own visibility restrictions,\nbut is only included in a method marked as \"PREVIEW\", so it will only\nappear if `visibility_restriction_selectors` include \"PREVIEW\"."
},
"examplepbVisibilityRuleMessageInPreviewMethod": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"subMessage": {
"$ref": "#/definitions/examplepbVisibilityRuleSubMessageInPreviewMethod"
},
"enum": {
"$ref": "#/definitions/examplepbVisibilityRuleEnumInPreviewMethod"
}
},
"description": "MessageInPreviewMethod doesn't define its own visibility restrictions,\nbut is only included in a method marked as \"PREVIEW\", so it will only\nappear if `visibility_restriction_selectors` include \"PREVIEW\"."
},
"examplepbVisibilityRuleSimpleMessage": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -426,6 +449,15 @@
},
"description": "SimpleMessage represents a simple message sent to the Echo service."
},
"examplepbVisibilityRuleSubMessageInPreviewMethod": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"description": "SubMessageInPreviewMethod doesn't define its own visibility restrictions,\nbut is only included in a method marked as \"PREVIEW\", so it will only\nappear if `visibility_restriction_selectors` include \"PREVIEW\"."
},
"protobufAny": {
"type": "object",
"properties": {
Expand Down
4 changes: 4 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ func findServicesMessagesAndEnumerations(s []*descriptor.Service, reg *descripto
for _, meth := range svc.Methods {
// Request may be fully included in query
{
if !isVisible(getMethodVisibilityOption(meth), reg) {
continue
}

swgReqName, ok := fullyQualifiedNameToOpenAPIName(meth.RequestType.FQMN(), reg)
if !ok {
glog.Errorf("couldn't resolve OpenAPI name for FQMN '%v'", meth.RequestType.FQMN())
Expand Down

0 comments on commit c37ecc1

Please sign in to comment.