Skip to content

Commit 67a4e9e

Browse files
authored
fix(plugin): Drop synthetic oneofs (#14)
1 parent dc6f6fa commit 67a4e9e

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

cmd/protoc-gen-openapi/generator/generator.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,10 @@ func (g *OpenAPIv3Generator) addSchemasForMessagesToDocumentV3(d *v3.Document, m
881881
var required []string
882882

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

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

1025+
// Filter out synthetic oneofs created by proto3 optional fields.
1026+
// These are handled as regular optional fields in the field loop.
1027+
var explicitOneofs []*protogen.Oneof
1028+
for _, o := range oneofs {
1029+
if !o.Desc.IsSynthetic() {
1030+
explicitOneofs = append(explicitOneofs, o)
1031+
}
1032+
}
1033+
oneofs = explicitOneofs
1034+
if len(oneofs) == 0 {
1035+
return
1036+
}
1037+
10231038
// Check if this message only contains oneofs and no other fields
10241039
if len(schema.Properties.AdditionalProperties) == 0 {
10251040
// Flatten all oneofs to the message level

0 commit comments

Comments
 (0)