Skip to content

Commit a097709

Browse files
authored
fix: handle presence for oneof fields in schema conversion (#68)
1 parent 97e9f3a commit a097709

15 files changed

+603
-1866
lines changed

.devcontainer/Dockerfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM mcr.microsoft.com/devcontainers/go:1.23
2+
3+
RUN go install github.com/bufbuild/buf/cmd/[email protected]

.devcontainer/devcontainer.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// The Dev Container format allows you to configure your environment. At the heart of it
2+
// is a Docker image or Dockerfile which controls the tools available in your environment.
3+
//
4+
// See https://aka.ms/devcontainer.json for more information.
5+
{
6+
"name": "Protoc Gen Connect OpenAPI",
7+
"build": {
8+
"context": ".",
9+
"dockerfile": "Dockerfile"
10+
},
11+
"remoteUser": "root"
12+
}

internal/converter/schema/schema.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ func MessageToSchema(opts options.Options, tt protoreflect.MessageDescriptor) (s
3939
fields := tt.Fields()
4040
for i := 0; i < fields.Len(); i++ {
4141
field := fields.Get(i)
42-
if oneOf := field.ContainingOneof(); oneOf != nil {
42+
if oneOf := field.ContainingOneof(); oneOf != nil && !oneOf.IsSynthetic() {
4343
oneOneGroups[oneOf.FullName()] = append(oneOneGroups[oneOf.FullName()], util.MakeFieldName(opts, field))
4444
}
45-
props.Set(util.MakeFieldName(opts, field), FieldToSchema(opts, base.CreateSchemaProxy(s), field))
45+
prop := FieldToSchema(opts, base.CreateSchemaProxy(s), field)
46+
if field.HasOptionalKeyword() {
47+
nullable := true
48+
prop.Schema().Nullable = &nullable
49+
}
50+
props.Set(util.MakeFieldName(opts, field), prop)
4651
}
4752

4853
s.Properties = props

internal/converter/testdata/standard/output/flex.openapi.json

+2-19
Original file line numberDiff line numberDiff line change
@@ -620,24 +620,6 @@
620620
"schemas": {
621621
"flex.ComplexType": {
622622
"type": "object",
623-
"anyOf": [
624-
{
625-
"required": [
626-
"optionalMsgField"
627-
]
628-
},
629-
{
630-
"not": {
631-
"anyOf": [
632-
{
633-
"required": [
634-
"optionalMsgField"
635-
]
636-
}
637-
]
638-
}
639-
}
640-
],
641623
"properties": {
642624
"doubleField": {
643625
"type": "number",
@@ -741,7 +723,8 @@
741723
"$ref": "#/components/schemas/flex.Other"
742724
}
743725
],
744-
"title": "optionalMsgField"
726+
"title": "optionalMsgField",
727+
"nullable": true
745728
}
746729
},
747730
"title": "ComplexType",

internal/converter/testdata/standard/output/flex.openapi.yaml

+1-7
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,6 @@ components:
379379
schemas:
380380
flex.ComplexType:
381381
type: object
382-
anyOf:
383-
- required:
384-
- optionalMsgField
385-
- not:
386-
anyOf:
387-
- required:
388-
- optionalMsgField
389382
properties:
390383
doubleField:
391384
type: number
@@ -462,6 +455,7 @@ components:
462455
allOf:
463456
- $ref: '#/components/schemas/flex.Other'
464457
title: optionalMsgField
458+
nullable: true
465459
title: ComplexType
466460
additionalProperties: false
467461
description: Type that has a bunch of different types

internal/converter/testdata/standard/output/protovalidate.numbers.openapi.json

+2-19
Original file line numberDiff line numberDiff line change
@@ -1455,24 +1455,6 @@
14551455
},
14561456
"buf.validate.conformance.cases.Int64LTEOptional": {
14571457
"type": "object",
1458-
"anyOf": [
1459-
{
1460-
"required": [
1461-
"val"
1462-
]
1463-
},
1464-
{
1465-
"not": {
1466-
"anyOf": [
1467-
{
1468-
"required": [
1469-
"val"
1470-
]
1471-
}
1472-
]
1473-
}
1474-
}
1475-
],
14761458
"properties": {
14771459
"val": {
14781460
"type": [
@@ -1481,7 +1463,8 @@
14811463
],
14821464
"title": "val",
14831465
"maximum": 64,
1484-
"format": "int64"
1466+
"format": "int64",
1467+
"nullable": true
14851468
}
14861469
},
14871470
"title": "Int64LTEOptional",

internal/converter/testdata/standard/output/protovalidate.numbers.openapi.yaml

+1-7
Original file line numberDiff line numberDiff line change
@@ -1097,13 +1097,6 @@ components:
10971097
additionalProperties: false
10981098
buf.validate.conformance.cases.Int64LTEOptional:
10991099
type: object
1100-
anyOf:
1101-
- required:
1102-
- val
1103-
- not:
1104-
anyOf:
1105-
- required:
1106-
- val
11071100
properties:
11081101
val:
11091102
type:
@@ -1112,6 +1105,7 @@ components:
11121105
title: val
11131106
maximum: 64
11141107
format: int64
1108+
nullable: true
11151109
title: Int64LTEOptional
11161110
additionalProperties: false
11171111
buf.validate.conformance.cases.Int64None:

0 commit comments

Comments
 (0)