fix(openapiv2): Invalid entries in body parameter schema required array when using body: "field_name"#6088
Merged
johanbrandhorst merged 2 commits intogrpc-ecosystem:mainfrom Nov 12, 2025
Conversation
body: "field_name"body: "field_name"
johanbrandhorst
requested changes
Nov 11, 2025
Collaborator
johanbrandhorst
left a comment
There was a problem hiding this comment.
Thanks for another excellent analysis and PR, I suspect this will likely also cause some generation failures as it fixes existing generation bugs, if not, could you add a case of this to one of the example protos? Thanks!
…rray
When generating OpenAPI schemas for body parameters with body: "field_name"
and path parameters extracted from nested fields, the body field name was
incorrectly added to the schema's required array via updateSwaggerObjectFromFieldBehavior.
For example, with UpdateCommentRequest containing a required Comment field,
and Comment having a required "comment" field, when using path parameter
{comment.name} and body: "comment", the generated body schema incorrectly had:
required: ["comment", "resource", "author", "comment"]
Or when the field name doesn't exist as a property:
required: ["title", "direction"] where "direction" is not a property
This fix filters the required array after calling renderFieldAsDefinition to:
1. Remove duplicate entries of the body field name
2. Remove the body field name if it's not actually a property of the schema
The body parameter's required status is already correctly set via the
parameter's required: true attribute.
The fix in template.go correctly removes invalid body field names from the required array when they are not properties of the schema. This regeneration updates a_bit_of_everything.swagger.json to remove the incorrectly included 'book' field from the required array in the UpdateBookRequest body parameter. Related to body parameter schema generation fix.
da2d098 to
eb44769
Compare
Contributor
Author
I've rebased & updated examples for both PRs - I suspect that this one will have a conflict after merging #6078 so will need to rebase again and fix, but LMK! |
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
When using
body: "field_name"in HTTP annotations with path parameters extracted from nested fields, the body field name is incorrectly added to the schema'srequiredarray, causing either duplicate entries or invalid references to non-existent properties.Bug 1: Duplicate Required Field
When the body field name matches a property in the nested message:
Proto definition:
Generated OpenAPI (incorrect):
{ "name": "comment", "in": "body", "required": true, "schema": { "required": ["comment", "author", "comment"] } }The
"comment"field appears twice in the required array.Expected:
{ "name": "comment", "in": "body", "required": true, "schema": { "required": ["comment", "author"] } }Bug 2: Self-Referential Schema
When the body field name does NOT match any property in the nested message:
Proto definition:
Generated OpenAPI (incorrect):
{ "name": "direction", "in": "body", "required": true, "schema": { "properties": { "title": {"type": "string"} }, "required": ["title", "direction"] } }The required array contains
"direction", which is not a property of the schema.Expected:
{ "name": "direction", "in": "body", "required": true, "schema": { "properties": { "title": {"type": "string"} }, "required": ["title"] } }Root Cause
When
renderFieldAsDefinitionis called for body parameters with path parameters, it callsupdateSwaggerObjectFromFieldBehaviorwhich adds the field name to the schema'srequiredarray if the field is markedREQUIRED.However, the schema represents the field's type (the nested message), not the containing message. The body field name should not be in that schema's required array - the body parameter's required status is already correctly expressed via
"required": trueat the parameter level.Trigger Conditions
This bug occurs when ALL of these conditions are met:
body: "field_name"(notbody: "*"){field_name.property})[(google.api.field_behavior) = REQUIRED]Environment
Additional Context
This is a separate issue from the nested required fields hoisting bug. That bug affects
body: "*"while this bug affectsbody: "field_name".References to other Issues or PRs
Have you read the Contributing Guidelines?
yes