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

Commit c6dba84

Browse files
Convert google Int64Value to strings (#131)
* Add support to convert google Int64Value wrappers to string type. * Add files and test for disallowing strings and allow null values. * Add test cases for disallowing big ints as strings. Co-authored-by: Ian Van Den Heuvel <[email protected]>
1 parent 6f71afe commit c6dba84

16 files changed

+312
-3
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ samples: build
4242
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=disallow_bigints_as_strings:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/SeveralMessages.proto || echo "No messages found (SeveralMessages.proto)"
4343
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=disallow_bigints_as_strings:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Timestamp.proto || echo "No messages found (Timestamp.proto)"
4444
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=all_fields_required:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/PayloadMessage2.proto || echo "No messages found (PayloadMessage2.proto)"
45-
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=json_fieldnames:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/JSONFields.proto || echo "No messages found (JSONFields.proto)"
45+
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=json_fieldnames:jsonschemas -I. --proto_path=${PROTO_PATH} ${PROTO_PATH}/JSONFields.proto || echo "No messages found (JSONFields.proto)"
4646
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/ArrayOfEnums.proto || echo "No messages found (SeveralMessages.proto)"
4747
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Maps.proto || echo "No messages found (Maps.proto)"
4848
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/MessageWithComments.proto || echo "No messages found (MessageWithComments.proto)"
4949
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Proto2Required.proto || echo "No messages found (Proto2Required.proto)"
5050
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Proto2NestedMessage.proto || echo "No messages found (Proto2NestedMessage.proto)"
5151
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/GoogleValue.proto || echo "No messages found (GoogleValue.proto)"
52+
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/GoogleInt64Value.proto || echo "No messages found (GoogleInt64Value.proto)"
53+
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=disallow_bigints_as_strings:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/GoogleInt64ValueDisallowString.proto || echo "No messages found (GoogleInt64ValueDisallowString.proto)"
54+
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=allow_null_values:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/GoogleInt64ValueAllowNull.proto || echo "No messages found (GoogleInt64ValueAllowNull.proto)"
55+
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=disallow_bigints_as_strings,allow_null_values:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/GoogleInt64ValueDisallowStringAllowNull.proto || echo "No messages found (GoogleInt64ValueDisallowStringAllowNull.proto)"
5256
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas -I. --proto_path=${PROTO_PATH} ${PROTO_PATH}/OptionEnumsAsConstants.proto || echo "No messages found (OptionEnumsAsConstants.proto)"
5357
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas -I. --proto_path=${PROTO_PATH} ${PROTO_PATH}/OptionFileExtension.proto || echo "No messages found (OptionFileExtension.proto)"
5458
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas -I. --proto_path=${PROTO_PATH} ${PROTO_PATH}/OptionIgnoredField.proto || echo "No messages found (HiddenFields.proto)"

internal/converter/converter_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,40 @@ func configureSampleProtos() map[string]sampleProto {
227227
ObjectsToValidateFail: []string{testdata.GoogleValueFail},
228228
ObjectsToValidatePass: []string{testdata.GoogleValuePass},
229229
},
230+
"GoogleInt64Value": {
231+
ExpectedJSONSchema: []string{testdata.GoogleInt64Value},
232+
FilesToGenerate: []string{"GoogleInt64Value.proto"},
233+
ProtoFileName: "GoogleInt64Value.proto",
234+
ObjectsToValidateFail: []string{testdata.GoogleInt64ValueFail},
235+
ObjectsToValidatePass: []string{testdata.GoogleInt64ValuePass},
236+
},
237+
"GoogleInt64ValueAllowNull": {
238+
Flags: ConverterFlags{AllowNullValues: true},
239+
ExpectedJSONSchema: []string{testdata.GoogleInt64ValueAllowNull},
240+
FilesToGenerate: []string{"GoogleInt64ValueAllowNull.proto"},
241+
ProtoFileName: "GoogleInt64ValueAllowNull.proto",
242+
ObjectsToValidateFail: []string{testdata.GoogleInt64ValueAllowNullFail},
243+
ObjectsToValidatePass: []string{testdata.GoogleInt64ValueAllowNullPass},
244+
},
245+
"GoogleInt64ValueDisallowString": {
246+
Flags: ConverterFlags{DisallowBigIntsAsStrings: true},
247+
ExpectedJSONSchema: []string{testdata.GoogleInt64ValueDisallowString},
248+
FilesToGenerate: []string{"GoogleInt64ValueDisallowString.proto"},
249+
ProtoFileName: "GoogleInt64ValueDisallowString.proto",
250+
ObjectsToValidateFail: []string{testdata.GoogleInt64ValueDisallowStringFail},
251+
ObjectsToValidatePass: []string{testdata.GoogleInt64ValueDisallowStringPass},
252+
},
253+
"GoogleInt64ValueDisallowStringAllowNull": {
254+
Flags: ConverterFlags{
255+
DisallowBigIntsAsStrings: true,
256+
AllowNullValues: true,
257+
},
258+
ExpectedJSONSchema: []string{testdata.GoogleInt64ValueDisallowStringAllowNull},
259+
FilesToGenerate: []string{"GoogleInt64ValueDisallowStringAllowNull.proto"},
260+
ProtoFileName: "GoogleInt64ValueDisallowStringAllowNull.proto",
261+
ObjectsToValidateFail: []string{testdata.GoogleInt64ValueDisallowStringAllowNullFail},
262+
ObjectsToValidatePass: []string{testdata.GoogleInt64ValueDisallowStringAllowNullPass},
263+
},
230264
"ImportedEnum": {
231265
ExpectedJSONSchema: []string{testdata.ImportedEnum},
232266
FilesToGenerate: []string{"ImportedEnum.proto"},
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package testdata
2+
3+
const GoogleInt64Value = `{
4+
"$schema": "http://json-schema.org/draft-04/schema#",
5+
"$ref": "#/definitions/GoogleInt64Value",
6+
"definitions": {
7+
"GoogleInt64Value": {
8+
"properties": {
9+
"big_number": {
10+
"additionalProperties": true,
11+
"type": "string"
12+
}
13+
},
14+
"additionalProperties": true,
15+
"type": "object",
16+
"title": "Google Int 64 Value"
17+
}
18+
}
19+
}`
20+
21+
const GoogleInt64ValueFail = `{"big_number": 12345}`
22+
23+
const GoogleInt64ValuePass = `{"big_number": "12345"}`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package testdata
2+
3+
const GoogleInt64ValueAllowNull = `{
4+
"$schema": "http://json-schema.org/draft-04/schema#",
5+
"$ref": "#/definitions/GoogleInt64ValueAllowNull",
6+
"definitions": {
7+
"GoogleInt64ValueAllowNull": {
8+
"properties": {
9+
"big_number": {
10+
"oneOf": [
11+
{
12+
"type": "null"
13+
},
14+
{
15+
"type": "string"
16+
}
17+
],
18+
"title": "Int 64 Value",
19+
"description": "Wrapper message for ` + "`int64`" + `. The JSON representation for ` + "`Int64Value`" + ` is JSON string."
20+
}
21+
},
22+
"additionalProperties": true,
23+
"oneOf": [
24+
{
25+
"type": "null"
26+
},
27+
{
28+
"type": "object"
29+
}
30+
],
31+
"title": "Google Int 64 Value Allow Null"
32+
}
33+
}
34+
}`
35+
36+
const GoogleInt64ValueAllowNullFail = `{"big_number": 12345}`
37+
38+
const GoogleInt64ValueAllowNullPass = `{"big_number": null}`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package testdata
2+
3+
const GoogleInt64ValueDisallowString = `{
4+
"$schema": "http://json-schema.org/draft-04/schema#",
5+
"$ref": "#/definitions/GoogleInt64ValueDisallowString",
6+
"definitions": {
7+
"GoogleInt64ValueDisallowString": {
8+
"properties": {
9+
"big_number": {
10+
"additionalProperties": true,
11+
"type": "integer"
12+
}
13+
},
14+
"additionalProperties": true,
15+
"type": "object",
16+
"title": "Google Int 64 Value Disallow String"
17+
}
18+
}
19+
}`
20+
21+
const GoogleInt64ValueDisallowStringFail = `{"big_number": "12345"}`
22+
23+
const GoogleInt64ValueDisallowStringPass = `{"big_number": 12345}`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package testdata
2+
3+
const GoogleInt64ValueDisallowStringAllowNull = `{
4+
"$schema": "http://json-schema.org/draft-04/schema#",
5+
"$ref": "#/definitions/GoogleInt64ValueDisallowStringAllowNull",
6+
"definitions": {
7+
"GoogleInt64ValueDisallowStringAllowNull": {
8+
"properties": {
9+
"big_number": {
10+
"oneOf": [
11+
{
12+
"type": "null"
13+
},
14+
{
15+
"type": "integer"
16+
}
17+
],
18+
"title": "Int 64 Value",
19+
"description": "Wrapper message for ` + "`int64`" + `. The JSON representation for ` + "`Int64Value`" + ` is JSON string."
20+
}
21+
},
22+
"additionalProperties": true,
23+
"oneOf": [
24+
{
25+
"type": "null"
26+
},
27+
{
28+
"type": "object"
29+
}
30+
],
31+
"title": "Google Int 64 Value Disallow String Allow Null"
32+
}
33+
}
34+
}`
35+
36+
const GoogleInt64ValueDisallowStringAllowNullFail = `{"big_number": "12345"}`
37+
38+
const GoogleInt64ValueDisallowStringAllowNullPass = `{"big_number": null}`
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/wrappers.proto";
5+
6+
message GoogleInt64Value {
7+
google.protobuf.Int64Value big_number = 1;
8+
}
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/wrappers.proto";
5+
6+
message GoogleInt64ValueAllowNull {
7+
google.protobuf.Int64Value big_number = 1;
8+
}
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/wrappers.proto";
5+
6+
message GoogleInt64ValueDisallowString {
7+
google.protobuf.Int64Value big_number = 1;
8+
}
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/wrappers.proto";
5+
6+
message GoogleInt64ValueDisallowStringAllowNull {
7+
google.protobuf.Int64Value big_number = 1;
8+
}

0 commit comments

Comments
 (0)