fix(openapiv2): prevent nested required fields hoisting to parent schema #6078
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