Skip to content

fix(protoc-gen-openapiv2): fix panic on enum resolution in nested messages#6367

Merged
johanbrandhorst merged 1 commit intogrpc-ecosystem:mainfrom
franchb:fix-openapi-enum-panic
Feb 17, 2026
Merged

fix(protoc-gen-openapiv2): fix panic on enum resolution in nested messages#6367
johanbrandhorst merged 1 commit intogrpc-ecosystem:mainfrom
franchb:fix-openapi-enum-panic

Conversation

@franchb
Copy link
Copy Markdown
Contributor

@franchb franchb commented Feb 16, 2026

Summary

Fixes #6366protoc-gen-openapiv2 panics with "can't resolve OpenAPI ref from typename" when messages reference enums or cross-package types (regression from #6315).

Three bugs in collectReferencedNamesForCache():

  1. Traversal order: Message FQMNs were pre-populated in the refs map before scanning services. Since collectNestedTypeFQNs uses refs for cycle detection, it short-circuited on pre-populated entries and never discovered nested enum types. Fixed by moving message pre-population to after service scanning.

  2. Incomplete traversal: Only service method request/response types were traversed for nested types, but renderMessagesAsDefinition renders all messages from the file. Cross-package types referenced by non-service messages were missing from the naming cache. Fixed by also calling collectNestedTypeFQNs on all messages from the current file.

  3. Package filter: The filter compared only the first dot-component (e.g. "example") against the full multi-component package name (e.g. "example.v1"), which always failed. Fixed by using strings.HasPrefix(trimmedName, currentPackage+".").

Test plan

🤖 Generated with Claude Code

…sages (grpc-ecosystem#6366)

Fix two bugs in collectReferencedNamesForCache() introduced by PR grpc-ecosystem#6315
that caused a panic ("can't resolve OpenAPI ref from typename") when a
service method returned a message containing another message with an
enum field, particularly with multi-component package names like
"example.v1".

Bug 1: Message FQMNs were pre-populated in the refs map before scanning
services, causing collectNestedTypeFQNs to short-circuit on
already-visited entries and miss nested enum types.

Bug 2: The package filter compared only the first dot-component (e.g.
"example") against the full package name (e.g. "example.v1"), which
always failed for multi-component packages.

Fixes grpc-ecosystem#6366

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@franchb franchb force-pushed the fix-openapi-enum-panic branch from 252c706 to 5e4e81d Compare February 16, 2026 08:22
Copy link
Copy Markdown
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

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

This looks.. reasonable? This code is pretty hard to follow, but if this fixes the case supplied in the bug then I'm satisfied.

@johanbrandhorst
Copy link
Copy Markdown
Collaborator

@printfn any chance you could pull this and try it out in your environment to confirm the fix?

@printfn
Copy link
Copy Markdown
Contributor

printfn commented Feb 16, 2026

The fix seems to work. I did notice that compared to 2.27.7 a bunch of the "$ref" values in the generated JSON seem to have changed. I assume this is expected?

@johanbrandhorst
Copy link
Copy Markdown
Collaborator

The fix seems to work. I did notice that compared to 2.27.7 a bunch of the "$ref" values in the generated JSON seem to have changed. I assume this is expected?

Hm, perhaps if you're using the opaque API? I'm not sure, but if they break anything please open a new issue. I'll merge this and get a release out with the fix, thanks!

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.27.8 panics with "can't resolve OpenAPI ref from typename"

3 participants