Skip to content

Commit c61f60e

Browse files
authored
fix(openapiv2): Invalid entries in body parameter schema required array when using body: "field_name" (#6088)
* fix(openapiv2): remove invalid body field name from schema required array 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. * chore(openapiv2): regenerate examples with body parameter required fix 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.
1 parent 7403f9c commit c61f60e

File tree

3 files changed

+510
-4
lines changed

3 files changed

+510
-4
lines changed

examples/internal/proto/examplepb/a_bit_of_everything.swagger.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5071,10 +5071,7 @@
50715071
}
50725072
},
50735073
"description": "The book's `name` field is used to identify the book to be updated.\nFormat: publishers/{publisher}/books/{book}",
5074-
"title": "The book to update.",
5075-
"required": [
5076-
"book"
5077-
]
5074+
"title": "The book to update."
50785075
}
50795076
},
50805077
{

protoc-gen-openapiv2/internal/genopenapi/template.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,39 @@ func renderServices(services []*descriptor.Service, paths *openapiPathsObject, r
15651565
if err != nil {
15661566
return err
15671567
}
1568+
// renderFieldAsDefinition may add the body field name to the schema's required array
1569+
// via updateSwaggerObjectFromFieldBehavior. However, for body parameters, the schema
1570+
// represents the field's type, not the containing message. The body field name should
1571+
// only be in the schema's required array if it's actually a property of the schema.
1572+
// Remove the body field name from required if it's not a property to avoid invalid entries.
1573+
if schema.Required != nil && schema.Properties != nil {
1574+
// Build a set of property names
1575+
propertyNames := make(map[string]bool)
1576+
for _, prop := range *schema.Properties {
1577+
propertyNames[prop.Key] = true
1578+
}
1579+
// Filter required array: keep field names that are either:
1580+
// 1. Not the body field name, OR
1581+
// 2. The body field name AND it's actually a property
1582+
filteredRequired := make([]string, 0, len(schema.Required))
1583+
seenBodyFieldName := false
1584+
for _, req := range schema.Required {
1585+
if req == bodyFieldName {
1586+
if propertyNames[req] {
1587+
// It's a property, keep it (but only once)
1588+
if !seenBodyFieldName {
1589+
filteredRequired = append(filteredRequired, req)
1590+
seenBodyFieldName = true
1591+
}
1592+
}
1593+
// else: It's not a property, skip it
1594+
} else {
1595+
// Not the body field name, keep it
1596+
filteredRequired = append(filteredRequired, req)
1597+
}
1598+
}
1599+
schema.Required = filteredRequired
1600+
}
15681601
}
15691602
if schema.Title != "" {
15701603
desc = mergeDescription(schema)

0 commit comments

Comments
 (0)