protoc-gen-openapi: fix schema name collision for deeply nested messages#473
Open
DenKoren wants to merge 2 commits intogoogle:mainfrom
Open
protoc-gen-openapi: fix schema name collision for deeply nested messages#473DenKoren wants to merge 2 commits intogoogle:mainfrom
DenKoren wants to merge 2 commits intogoogle:mainfrom
Conversation
getMessageName only walked one parent level when building OpenAPI schema names from nested proto messages. Messages nested 3+ levels deep (e.g. OuterAPI.Inner.Create.Request) all collapsed to the same schema key (Create_Request), causing silent data loss in the generated OpenAPI spec — only the first message's schema survived. Replace the single parent check with a loop that walks the full message hierarchy, producing fully disambiguated names like OuterAPI_Inner_Create_Request.
Add a new option that restores the legacy single-parent naming behavior for projects that depend on the old schema names. When no_recursive_schema_naming=true, A.B.C.Request produces C_Request (old behavior). When false (the default), it produces A_B_C_Request (new behavior).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getMessageNameincmd/protoc-gen-openapi/generator/reflector.goonly walked one parent level when building OpenAPI schema names from nested protobuf messages. This caused schema name collisions for messages nested 3+ levels deep.Example: Given these proto definitions in the same package:
The old code produced
Create_Requestfor both, because it only prepended the immediate parent (Create). Only the first schema survived in the generated OpenAPI spec — the others silently reused its (structurally incorrect) definition.Backward compatibility
For backward compatibility,
no_recursive_schema_namingoption is introduced. If you need old behaviour, set this option to the generator via--openapi_ipt="no_recursive_schema_naming=true".Default behaviour changed as old behaviour produced a bug in generated openapi spec that was hard to notice.
Fix
Replace the single-parent
ifcheck with aforloop that walks the full message hierarchy:A.Create.Request→A_Create_RequestB.Inner.Create.Request→B_Inner_Create_RequestTests