@@ -25,14 +25,14 @@ import (
2525
2626 v3 "github.com/google/gnostic-models/openapiv3"
2727 "google.golang.org/genproto/googleapis/api/annotations"
28- status_pb "google.golang.org/genproto/googleapis/rpc/status"
28+ statuspb "google.golang.org/genproto/googleapis/rpc/status"
2929 "google.golang.org/protobuf/compiler/protogen"
3030 "google.golang.org/protobuf/proto"
3131 "google.golang.org/protobuf/reflect/protoreflect"
3232 "google.golang.org/protobuf/types/descriptorpb"
33- any_pb "google.golang.org/protobuf/types/known/anypb"
33+ "google.golang.org/protobuf/types/known/anypb"
3434
35- wk "github.com/pubgo/protoc-gen-openapi/generator/wellknown"
35+ "github.com/pubgo/protoc-gen-openapi/generator/wellknown"
3636)
3737
3838type Configuration struct {
@@ -55,8 +55,8 @@ const (
5555// to know the message descriptors for google.rpc.Status as well
5656// as google.protobuf.Any.
5757var (
58- statusProtoDesc = (& status_pb .Status {}).ProtoReflect ().Descriptor ()
59- anyProtoDesc = (& any_pb .Any {}).ProtoReflect ().Descriptor ()
58+ statusProtoDesc = (& statuspb .Status {}).ProtoReflect ().Descriptor ()
59+ anyProtoDesc = (& anypb .Any {}).ProtoReflect ().Descriptor ()
6060)
6161
6262// OpenAPIv3Generator holds internal state needed to generate an OpenAPIv3 document for a transcoded Protocol Buffer service.
@@ -122,15 +122,17 @@ func (g *OpenAPIv3Generator) buildDocumentV3() *v3.Document {
122122 // track of which schemas are referenced in the response so we can
123123 // add them later.
124124 for _ , file := range g .inputFiles {
125- if file .Generate {
126- // Merge any `Document` annotations with the current
127- extDocument := proto .GetExtension (file .Desc .Options (), v3 .E_Document )
128- if extDocument != nil {
129- proto .Merge (d , extDocument .(* v3.Document ))
130- }
125+ if ! file .Generate {
126+ continue
127+ }
131128
132- g .addPathsToDocumentV3 (d , file .Services )
129+ // Merge any `Document` annotations with the current
130+ extDocument := proto .GetExtension (file .Desc .Options (), v3 .E_Document )
131+ if extDocument != nil {
132+ proto .Merge (d , extDocument .(* v3.Document ))
133133 }
134+
135+ g .addPathsToDocumentV3 (d , file .Services )
134136 }
135137
136138 // While we have required schemas left to generate, go through the files again
@@ -579,11 +581,11 @@ func (g *OpenAPIv3Generator) buildOperationV3(
579581 // Add the default response if needed
580582 if * g .conf .DefaultResponse {
581583 anySchemaName := g .reflect .formatMessageName (anyProtoDesc )
582- anySchema := wk .NewGoogleProtobufAnySchema (anySchemaName )
584+ anySchema := wellknown .NewGoogleProtobufAnySchema (anySchemaName )
583585 g .addSchemaToDocumentV3 (d , anySchema )
584586
585587 statusSchemaName := g .reflect .formatMessageName (statusProtoDesc )
586- statusSchema := wk .NewGoogleRpcStatusSchema (statusSchemaName , anySchemaName )
588+ statusSchema := wellknown .NewGoogleRpcStatusSchema (statusSchemaName , anySchemaName )
587589 g .addSchemaToDocumentV3 (d , statusSchema )
588590
589591 defaultResponse := & v3.NamedResponseOrReference {
@@ -592,7 +594,7 @@ func (g *OpenAPIv3Generator) buildOperationV3(
592594 Oneof : & v3.ResponseOrReference_Response {
593595 Response : & v3.Response {
594596 Description : "Default error response" ,
595- Content : wk .NewApplicationJsonMediaType (& v3.SchemaOrReference {
597+ Content : wellknown .NewApplicationJsonMediaType (& v3.SchemaOrReference {
596598 Oneof : & v3.SchemaOrReference_Reference {
597599 Reference : & v3.Reference {XRef : "#/components/schemas/" + statusSchemaName },
598600 },
@@ -705,8 +707,9 @@ func (g *OpenAPIv3Generator) addOperationToDocumentV3(d *v3.Document, op *v3.Ope
705707// addPathsToDocumentV3 adds paths from a specified file descriptor.
706708func (g * OpenAPIv3Generator ) addPathsToDocumentV3 (d * v3.Document , services []* protogen.Service ) {
707709 for _ , service := range services {
708- annotationsCount := 0
710+ extService , _ := proto . GetExtension ( service . Desc . Options (), E_Service ).( * Service )
709711
712+ annotationsCount := 0
710713 for _ , method := range service .Methods {
711714 comment := g .filterCommentString (method .Comments .Leading )
712715 inputMessage := method .Input
@@ -764,6 +767,18 @@ func (g *OpenAPIv3Generator) addPathsToDocumentV3(d *v3.Document, services []*pr
764767 proto .Merge (op , extOperation .(* v3.Operation ))
765768 }
766769
770+ // Merge any `Service` annotations with the current
771+ if extService != nil {
772+ op .Parameters = append (op .Parameters , extService .Parameters ... )
773+ op .SpecificationExtension = append (op .SpecificationExtension , extService .SpecificationExtension ... )
774+ op .Tags = append (op .Tags , extService .Tags ... )
775+ op .Servers = append (op .Servers , extService .Servers ... )
776+ op .Security = append (op .Security , extService .Security ... )
777+ if extService .ExternalDocs != nil {
778+ proto .Merge (op .ExternalDocs , extService .ExternalDocs )
779+ }
780+ }
781+
767782 for _ , v := range op .Parameters {
768783 if v .Oneof == nil {
769784 continue
@@ -774,7 +789,7 @@ func (g *OpenAPIv3Generator) addPathsToDocumentV3(d *v3.Document, services []*pr
774789 p := v1 .Parameter
775790 if p .In == "header" {
776791 if p .Schema == nil {
777- p .Schema = wk .NewStringSchema ()
792+ p .Schema = wellknown .NewStringSchema ()
778793 }
779794 }
780795 }
@@ -840,15 +855,15 @@ func (g *OpenAPIv3Generator) addSchemasForMessagesToDocumentV3(d *v3.Document, m
840855 // `google.protobuf.Value` and `google.protobuf.Any` have special JSON transcoding
841856 // so we can't just reflect on the message descriptor.
842857 if typeName == ".google.protobuf.Value" {
843- g .addSchemaToDocumentV3 (d , wk .NewGoogleProtobufValueSchema (schemaName ))
858+ g .addSchemaToDocumentV3 (d , wellknown .NewGoogleProtobufValueSchema (schemaName ))
844859 continue
845860 } else if typeName == ".google.protobuf.Any" {
846- g .addSchemaToDocumentV3 (d , wk .NewGoogleProtobufAnySchema (schemaName ))
861+ g .addSchemaToDocumentV3 (d , wellknown .NewGoogleProtobufAnySchema (schemaName ))
847862 continue
848863 } else if typeName == ".google.rpc.Status" {
849864 anySchemaName := g .reflect .formatMessageName (anyProtoDesc )
850- g .addSchemaToDocumentV3 (d , wk .NewGoogleProtobufAnySchema (anySchemaName ))
851- g .addSchemaToDocumentV3 (d , wk .NewGoogleRpcStatusSchema (schemaName , anySchemaName ))
865+ g .addSchemaToDocumentV3 (d , wellknown .NewGoogleProtobufAnySchema (anySchemaName ))
866+ g .addSchemaToDocumentV3 (d , wellknown .NewGoogleRpcStatusSchema (schemaName , anySchemaName ))
852867 continue
853868 }
854869
0 commit comments