Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Fix behaviour of "one of" and "optional of proto3 ( #161, #126) (#173)
Browse files Browse the repository at this point in the history
* Fix one of and optional behaviour on proto3

* Update test data and result file
  • Loading branch information
AkihiroUeda35 authored Feb 12, 2024
1 parent 0ca6ba2 commit 11e14e6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 23 deletions.
39 changes: 31 additions & 8 deletions internal/converter/testdata/oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const OneOf = `{
"$ref": "#/definitions/OneOf",
"definitions": {
"OneOf": {
"required": [
"something"
],
"properties": {
"bar": {
"$ref": "#/definitions/samples.OneOf.Bar",
Expand All @@ -20,15 +23,35 @@ const OneOf = `{
},
"additionalProperties": true,
"type": "object",
"oneOf": [
{
"required": [
"bar"
]
},
"allOf": [
{
"required": [
"baz"
"oneOf": [
{
"not": {
"anyOf": [
{
"required": [
"bar"
]
},
{
"required": [
"baz"
]
}
]
}
},
{
"required": [
"bar"
]
},
{
"required": [
"baz"
]
}
]
}
],
Expand Down
26 changes: 19 additions & 7 deletions internal/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,13 +625,21 @@ func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msgDesc *d
c.logger.WithField("field_name", fieldDesc.GetName()).WithField("type", recursedJSONSchemaType.Type).Trace("Converted field")

// If this field is part of a OneOf declaration then build that here:
if c.Flags.EnforceOneOf && fieldDesc.OneofIndex != nil {
if c.Flags.EnforceOneOf && fieldDesc.OneofIndex != nil && !fieldDesc.GetProto3Optional() {
for {
if *fieldDesc.OneofIndex < int32(len(jsonSchemaType.AllOf)) {
break
}
var notAnyOf = &jsonschema.Type{Not: &jsonschema.Type{AnyOf: []*jsonschema.Type{}}}
jsonSchemaType.AllOf = append(jsonSchemaType.AllOf, &jsonschema.Type{OneOf: []*jsonschema.Type{notAnyOf}})
}
if c.Flags.UseJSONFieldnamesOnly {
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Required: []string{fieldDesc.GetJsonName()}})
jsonSchemaType.AllOf[*fieldDesc.OneofIndex].OneOf = append(jsonSchemaType.AllOf[*fieldDesc.OneofIndex].OneOf, &jsonschema.Type{Required: []string{fieldDesc.GetJsonName()}})
jsonSchemaType.AllOf[*fieldDesc.OneofIndex].OneOf[0].Not.AnyOf = append(jsonSchemaType.AllOf[*fieldDesc.OneofIndex].OneOf[0].Not.AnyOf, &jsonschema.Type{Required: []string{fieldDesc.GetJsonName()}})
} else {
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Required: []string{fieldDesc.GetName()}})
jsonSchemaType.AllOf[*fieldDesc.OneofIndex].OneOf = append(jsonSchemaType.AllOf[*fieldDesc.OneofIndex].OneOf, &jsonschema.Type{Required: []string{fieldDesc.GetName()}})
jsonSchemaType.AllOf[*fieldDesc.OneofIndex].OneOf[0].Not.AnyOf = append(jsonSchemaType.AllOf[*fieldDesc.OneofIndex].OneOf[0].Not.AnyOf, &jsonschema.Type{Required: []string{fieldDesc.GetName()}})
}

}

// Figure out which field names we want to use:
Expand All @@ -646,9 +654,13 @@ func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msgDesc *d
}

// Enforce all_fields_required:
if messageFlags.AllFieldsRequired && len(jsonSchemaType.OneOf) == 0 && jsonSchemaType.Properties != nil {
for _, property := range jsonSchemaType.Properties.Keys() {
jsonSchemaType.Required = append(jsonSchemaType.Required, property)
if messageFlags.AllFieldsRequired {
if fieldDesc.OneofIndex == nil && !fieldDesc.GetProto3Optional() {
if c.Flags.UseJSONFieldnamesOnly {
jsonSchemaType.Required = append(jsonSchemaType.Required, fieldDesc.GetJsonName())
} else {
jsonSchemaType.Required = append(jsonSchemaType.Required, fieldDesc.GetName())
}
}
}

Expand Down
36 changes: 28 additions & 8 deletions jsonschemas/OneOf.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,35 @@
},
"additionalProperties": true,
"type": "object",
"oneOf": [
"allOf": [
{
"required": [
"bar"
]
},
{
"required": [
"baz"
"oneOf": [
{
"not": {
"anyOf": [
{
"required": [
"bar"
]
},
{
"required": [
"baz"
]
}
]
}
},
{
"required": [
"bar"
]
},
{
"required": [
"baz"
]
}
]
}
],
Expand Down

0 comments on commit 11e14e6

Please sign in to comment.