Skip to content

openapi: look up service comments by declaration index - #7600

Merged
johanbrandhorst merged 1 commit into
grpc-ecosystem:mainfrom
b84330808:fix/openapi-comments-after-empty-service
Oct 29, 2026
Merged

johanbrandhorst merged 1 commit into
grpc-ecosystem:mainfrom
b84330808:fix/openapi-comments-after-empty-service

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

References to other Issues or PRs

Refs #746 (the case that still happens on main; the two-service example in the issue already works).

Have you read the Contributing Guidelines?

Yes.

Brief description of what is fixed or changed

The registry leaves services without methods out of File.Services, but the source code info paths that hold comments use the index of the service declaration in the .proto file. Both generators used the File.Services index, so a service declared after an empty one reads the comments of the declaration before it:

// Placeholder service.
service Placeholder {}

// Manages books.
service BookService {
  // Gets a book.
  rpc GetBook(Book) returns (Book) {
    option (google.api.http) = {get: "/v1/book"};
  }
}

Here the BookService tag gets the description "Placeholder service." and GetBook has no summary. With Empty, A, B, B's tag and operation get A's comments.

The index is now looked up in the file's service declarations (File.GetService()): in protoc-gen-openapiv2 through findServiceIndex, which replaces the svcBaseIdx bookkeeping in renderServices, and in protoc-gen-openapiv3 through a serviceIndex helper used by serviceComments and methodComments.

Other comments

Added TestGenerateCommentsAfterEmptyService (v2) and TestServiceAndMethodCommentsAfterEmptyService (v3); both fail without the change.

I used Claude Code to write this change.

The registry leaves services without methods out of File.Services, but
source code info paths use the index of the service declaration in the
file. A service declared after an empty one therefore read the comments
of the service before it: its tag got the empty service's description
and its operations lost their summaries.

Look the index up in the file's service declarations instead, in both
protoc-gen-openapiv2 and protoc-gen-openapiv3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@johanbrandhorst johanbrandhorst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@johanbrandhorst
johanbrandhorst enabled auto-merge (squash) October 29, 2026 10:55
@johanbrandhorst
johanbrandhorst merged commit bdca55c into grpc-ecosystem:main Oct 29, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants