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

Commit 4038653

Browse files
chrustychris
andauthored
Fixing a stack overflow (infinite recursion) when converting google.protobuf.Value (#36)
Co-authored-by: chris <[email protected]>
1 parent 94e4524 commit 4038653

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ samples:
3333
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/MessageWithComments.proto || echo "No messages found (MessageWithComments.proto)"
3434
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Proto2Required.proto || echo "No messages found (Proto2Required.proto)"
3535
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Proto2NestedMessage.proto || echo "No messages found (Proto2NestedMessage.proto)"
36+
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/GoogleValue.proto || echo "No messages found (GoogleValue.proto)"
3637
@PATH=./bin:$$PATH; protoc --jsonschema_out=all_fields_required:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Proto2NestedObject.proto || echo "No messages found (Proto2NestedObject.proto)"
3738
@PATH=./bin:$$PATH; protoc -I /usr/include --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/WellKnown.proto || echo "No messages found (WellKnown.proto)"
3839
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/NoPackage.proto

internal/converter/converter_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ func configureSampleProtos() map[string]sampleProto {
233233
FilesToGenerate: []string{"Proto2NestedObject.proto"},
234234
ProtoFileName: "Proto2NestedObject.proto",
235235
},
236+
"GoogleValue": {
237+
ExpectedJSONSchema: []string{testdata.GoogleValue},
238+
FilesToGenerate: []string{"GoogleValue.proto"},
239+
ProtoFileName: "GoogleValue.proto",
240+
},
236241
}
237242
}
238243

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package testdata
2+
3+
const GoogleValue = `{
4+
"$schema": "http://json-schema.org/draft-04/schema#",
5+
"properties": {
6+
"arg": {
7+
"oneOf": [
8+
{
9+
"type": "null"
10+
},
11+
{
12+
"type": "object"
13+
}
14+
]
15+
}
16+
},
17+
"additionalProperties": true,
18+
"type": "object"
19+
}`
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto3";
2+
package samples;
3+
4+
import "google/protobuf/struct.proto";
5+
6+
message GoogleValue {
7+
google.protobuf.Value arg = 1;
8+
}

internal/converter/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var (
3030
"BoolValue": true,
3131
"StringValue": true,
3232
"BytesValue": true,
33+
"Value": true,
3334
}
3435
)
3536

@@ -421,6 +422,11 @@ func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msg *descr
421422
{Type: gojsonschema.TYPE_NULL},
422423
{Type: gojsonschema.TYPE_STRING},
423424
}
425+
case "Value":
426+
schema.OneOf = []*jsonschema.Type{
427+
{Type: gojsonschema.TYPE_NULL},
428+
{Type: gojsonschema.TYPE_OBJECT},
429+
}
424430
}
425431
return schema, nil
426432
}

jsonschemas/GoogleValue.jsonschema

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"properties": {
4+
"arg": {
5+
"oneOf": [
6+
{
7+
"type": "null"
8+
},
9+
{
10+
"type": "object"
11+
}
12+
]
13+
}
14+
},
15+
"additionalProperties": true,
16+
"type": "object"
17+
}

0 commit comments

Comments
 (0)