diff --git a/cmd/protoc-gen-openapi/generator/generator.go b/cmd/protoc-gen-openapi/generator/generator.go index cc9d1c02..df92bdb2 100644 --- a/cmd/protoc-gen-openapi/generator/generator.go +++ b/cmd/protoc-gen-openapi/generator/generator.go @@ -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 } @@ -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