fix(openapiv2): prevent nested required fields hoisting to parent schema#6078
Merged
johanbrandhorst merged 2 commits intogrpc-ecosystem:mainfrom Nov 12, 2025
Merged
Conversation
johanbrandhorst
requested changes
Nov 11, 2025
Collaborator
johanbrandhorst
left a comment
There was a problem hiding this comment.
Hi, thanks for this high quality PR! It looks like this change fixed some issues in the existing generated examples, could you regenerate the files and commit them? See CONTRIBUTING.md for a guide. Thanks!
Note: think the proto_lint failure is a red herring, a rebase should fix it.
a4de767 to
c91509f
Compare
Collaborator
|
Please rebase, thank you! |
When generating OpenAPI schemas for request bodies with path parameters
extracted from nested messages (e.g., {thing.name}), nested required
field names were incorrectly added to the parent body schemas required
array instead of remaining in the nested objects required array.
For example, with UpdateFooRequest containing a required Foo field, and
Foo having required name and value fields, when using path parameter
{thing.name}, the generated body schema incorrectly had:
required: ["value", "thing"]
Instead of:
required: ["thing"] with thing.required: ["value"]
This fix adds logic to distinguish between field-level and nested-level
required markers when path parameters are present. It ensures only the
field name itself appears in the parents required array if the field
is marked REQUIRED, while nested field names remain in the nested
schemas required array.
c91509f to
475daa3
Compare
Collaborator
|
Thank you for contributing :) |
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.
Bug: Nested Required Fields Incorrectly Hoisted to Parent Body Schema
Summary
protoc-gen-openapiv2incorrectly hoists nested message field names marked asREQUIREDinto the parent request body'srequiredarray when those fields are also used in path parameters. This creates invalid OpenAPI schemas where therequiredarray references field names that don't exist at the top level.Version
Prerequisites
The bug occurs when ALL these conditions are met:
{thing.name})(google.api.field_behavior) = REQUIREDbody: "*"or similarExample
Proto Definition
Current (Incorrect) Output
{ "definitions": { "FooServiceUpdateFooBody": { "type": "object", "properties": { "thing": { "type": "object", "properties": { "value": { "type": "string" } } }, "updateMask": { "type": "string" } }, "required": [ "value", // WRONG: "value" is nested inside "thing"! "thing" ] } } }Expected (Correct) Output
{ "definitions": { "FooServiceUpdateFooBody": { "type": "object", "properties": { "thing": { "type": "object", "properties": { "value": { "type": "string" } }, "required": ["value"] // Nested required stays here }, "updateMask": { "type": "string" } }, "required": ["thing"] // Only top-level fields } } }Root Cause
In
protoc-gen-openapiv2/internal/genopenapi/template.go(around line 609-615), when processing nested message fields with path parameters, ALL required field names from the nested schema are hoisted to the parent'srequiredarray without distinguishing between:References to other Issues or PRs
possibly originated in #2635
Have you read the Contributing Guidelines?
Brief description of what is fixed or changed
Other comments