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

Commit

Permalink
Attempting to support proto OneOf (#58)
Browse files Browse the repository at this point in the history
* Attempting to support proto OneOf

* Fixing a broken test affected by this PR

Co-authored-by: Chrusty <Chris@H>
  • Loading branch information
chrusty and Chrusty authored Mar 22, 2021
1 parent b4c1908 commit a1a1159
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ samples:
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Proto2Required.proto || echo "No messages found (Proto2Required.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Proto2NestedMessage.proto || echo "No messages found (Proto2NestedMessage.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/GoogleValue.proto || echo "No messages found (GoogleValue.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/OneOf.proto || echo "No messages found (OneOf.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=all_fields_required:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Proto2NestedObject.proto || echo "No messages found (Proto2NestedObject.proto)"
@PATH=./bin:$$PATH; protoc -I /usr/include --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/WellKnown.proto || echo "No messages found (WellKnown.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/NoPackage.proto
Expand Down
5 changes: 5 additions & 0 deletions internal/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ func configureSampleProtos() map[string]sampleProto {
ProtoFileName: "JSONFields.proto",
UseJSONFieldnamesOnly: true,
},
"OneOf": {
ExpectedJSONSchema: []string{testdata.OneOf},
FilesToGenerate: []string{"OneOf.proto"},
ProtoFileName: "OneOf.proto",
},
}
}

Expand Down
19 changes: 18 additions & 1 deletion internal/converter/testdata/message_kind_11.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ const MessageKind11 = `{
}
},
"additionalProperties": true,
"type": "object"
"type": "object",
"oneOf": [
{
"required": [
"kind2"
]
},
{
"required": [
"kind3"
]
},
{
"required": [
"kind4"
]
}
]
}
`
39 changes: 37 additions & 2 deletions internal/converter/testdata/message_kind_12.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const MessageKind12 = `{
"type": "string"
},
"f": {
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"name": {
"type": "string"
Expand Down Expand Up @@ -113,7 +114,24 @@ const MessageKind12 = `{
}
},
"additionalProperties": true,
"type": "object"
"type": "object",
"oneOf": [
{
"required": [
"kind2"
]
},
{
"required": [
"kind3"
]
},
{
"required": [
"kind4"
]
}
]
},
"kind5": {
"properties": {
Expand Down Expand Up @@ -189,6 +207,23 @@ const MessageKind12 = `{
}
},
"additionalProperties": true,
"type": "object"
"type": "object",
"oneOf": [
{
"required": [
"kind5"
]
},
{
"required": [
"kind6"
]
},
{
"required": [
"kind7"
]
}
]
}
`
39 changes: 39 additions & 0 deletions internal/converter/testdata/oneof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package testdata

const OneOf = `{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"bar": {
"properties": {
"foo": {
"type": "integer"
}
},
"additionalProperties": true,
"type": "object"
},
"baz": {
"properties": {
"foo": {
"type": "string"
}
},
"additionalProperties": true,
"type": "object"
}
},
"additionalProperties": true,
"type": "object",
"oneOf": [
{
"required": [
"bar"
]
},
{
"required": [
"baz"
]
}
]
}`
17 changes: 17 additions & 0 deletions internal/converter/testdata/proto/OneOf.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax="proto3";
package samples;

message OneOf {
oneof choice {
Bar bar = 1;
Baz baz = 2;
}

message Bar {
int32 foo = 1;
}

message Baz {
string foo = 1;
}
}
5 changes: 5 additions & 0 deletions internal/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msg *descr
}
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 fieldDesc.OneofIndex != nil {
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Required: []string{fieldDesc.GetName()}})
}

// Figure out which field names we want to use:
switch {
case c.UseJSONFieldnamesOnly:
Expand Down
19 changes: 18 additions & 1 deletion jsonschemas/MessageKind11.jsonschema
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,22 @@
}
},
"additionalProperties": true,
"type": "object"
"type": "object",
"oneOf": [
{
"required": [
"kind2"
]
},
{
"required": [
"kind3"
]
},
{
"required": [
"kind4"
]
}
]
}
39 changes: 37 additions & 2 deletions jsonschemas/MessageKind12.jsonschema
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "string"
},
"f": {
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"name": {
"type": "string"
Expand Down Expand Up @@ -111,7 +112,24 @@
}
},
"additionalProperties": true,
"type": "object"
"type": "object",
"oneOf": [
{
"required": [
"kind2"
]
},
{
"required": [
"kind3"
]
},
{
"required": [
"kind4"
]
}
]
},
"kind5": {
"properties": {
Expand Down Expand Up @@ -187,5 +205,22 @@
}
},
"additionalProperties": true,
"type": "object"
"type": "object",
"oneOf": [
{
"required": [
"kind5"
]
},
{
"required": [
"kind6"
]
},
{
"required": [
"kind7"
]
}
]
}
37 changes: 37 additions & 0 deletions jsonschemas/OneOf.jsonschema
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"bar": {
"properties": {
"foo": {
"type": "integer"
}
},
"additionalProperties": true,
"type": "object"
},
"baz": {
"properties": {
"foo": {
"type": "string"
}
},
"additionalProperties": true,
"type": "object"
}
},
"additionalProperties": true,
"type": "object",
"oneOf": [
{
"required": [
"bar"
]
},
{
"required": [
"baz"
]
}
]
}

0 comments on commit a1a1159

Please sign in to comment.