Skip to content

Commit

Permalink
fix: handle presence for oneof fields in schema conversion (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
akosyakov authored Feb 7, 2025
1 parent 97e9f3a commit a097709
Show file tree
Hide file tree
Showing 15 changed files with 603 additions and 1,866 deletions.
3 changes: 3 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM mcr.microsoft.com/devcontainers/go:1.23

RUN go install github.com/bufbuild/buf/cmd/[email protected]
12 changes: 12 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// The Dev Container format allows you to configure your environment. At the heart of it
// is a Docker image or Dockerfile which controls the tools available in your environment.
//
// See https://aka.ms/devcontainer.json for more information.
{
"name": "Protoc Gen Connect OpenAPI",
"build": {
"context": ".",
"dockerfile": "Dockerfile"
},
"remoteUser": "root"
}
9 changes: 7 additions & 2 deletions internal/converter/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ func MessageToSchema(opts options.Options, tt protoreflect.MessageDescriptor) (s
fields := tt.Fields()
for i := 0; i < fields.Len(); i++ {
field := fields.Get(i)
if oneOf := field.ContainingOneof(); oneOf != nil {
if oneOf := field.ContainingOneof(); oneOf != nil && !oneOf.IsSynthetic() {
oneOneGroups[oneOf.FullName()] = append(oneOneGroups[oneOf.FullName()], util.MakeFieldName(opts, field))
}
props.Set(util.MakeFieldName(opts, field), FieldToSchema(opts, base.CreateSchemaProxy(s), field))
prop := FieldToSchema(opts, base.CreateSchemaProxy(s), field)
if field.HasOptionalKeyword() {
nullable := true
prop.Schema().Nullable = &nullable
}
props.Set(util.MakeFieldName(opts, field), prop)
}

s.Properties = props
Expand Down
21 changes: 2 additions & 19 deletions internal/converter/testdata/standard/output/flex.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -620,24 +620,6 @@
"schemas": {
"flex.ComplexType": {
"type": "object",
"anyOf": [
{
"required": [
"optionalMsgField"
]
},
{
"not": {
"anyOf": [
{
"required": [
"optionalMsgField"
]
}
]
}
}
],
"properties": {
"doubleField": {
"type": "number",
Expand Down Expand Up @@ -741,7 +723,8 @@
"$ref": "#/components/schemas/flex.Other"
}
],
"title": "optionalMsgField"
"title": "optionalMsgField",
"nullable": true
}
},
"title": "ComplexType",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,6 @@ components:
schemas:
flex.ComplexType:
type: object
anyOf:
- required:
- optionalMsgField
- not:
anyOf:
- required:
- optionalMsgField
properties:
doubleField:
type: number
Expand Down Expand Up @@ -462,6 +455,7 @@ components:
allOf:
- $ref: '#/components/schemas/flex.Other'
title: optionalMsgField
nullable: true
title: ComplexType
additionalProperties: false
description: Type that has a bunch of different types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1455,24 +1455,6 @@
},
"buf.validate.conformance.cases.Int64LTEOptional": {
"type": "object",
"anyOf": [
{
"required": [
"val"
]
},
{
"not": {
"anyOf": [
{
"required": [
"val"
]
}
]
}
}
],
"properties": {
"val": {
"type": [
Expand All @@ -1481,7 +1463,8 @@
],
"title": "val",
"maximum": 64,
"format": "int64"
"format": "int64",
"nullable": true
}
},
"title": "Int64LTEOptional",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1097,13 +1097,6 @@ components:
additionalProperties: false
buf.validate.conformance.cases.Int64LTEOptional:
type: object
anyOf:
- required:
- val
- not:
anyOf:
- required:
- val
properties:
val:
type:
Expand All @@ -1112,6 +1105,7 @@ components:
title: val
maximum: 64
format: int64
nullable: true
title: Int64LTEOptional
additionalProperties: false
buf.validate.conformance.cases.Int64None:
Expand Down
Loading

0 comments on commit a097709

Please sign in to comment.