Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions cmd/protoc-gen-openapi/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,10 @@ func (g *OpenAPIv3Generator) addSchemasForMessagesToDocumentV3(d *v3.Document, m
var required []string

for _, field := range message.Fields {
// Skip fields that are part of a oneOf
if field.Oneof != nil {
// Skip fields that are part of an explicit oneOf.
// Proto3 optional fields create synthetic oneofs that should be
// treated as regular optional fields, not as oneOf variants.
if field.Oneof != nil && !field.Oneof.Desc.IsSynthetic() {
continue
}

Expand Down Expand Up @@ -1020,6 +1022,19 @@ func (g *OpenAPIv3Generator) addOneOfFieldsToSchema(d *v3.Document, oneofs []*pr
return
}

// Filter out synthetic oneofs created by proto3 optional fields.
// These are handled as regular optional fields in the field loop.
var explicitOneofs []*protogen.Oneof
for _, o := range oneofs {
if !o.Desc.IsSynthetic() {
explicitOneofs = append(explicitOneofs, o)
}
}
oneofs = explicitOneofs
if len(oneofs) == 0 {
return
}

// Check if this message only contains oneofs and no other fields
if len(schema.Properties.AdditionalProperties) == 0 {
// Flatten all oneofs to the message level
Expand Down
Loading