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

Commit

Permalink
Convert google Int64Value to strings (#131)
Browse files Browse the repository at this point in the history
* 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 <ivandenheuvel@auvik.com>
  • Loading branch information
ianvdhSBM and ianvdhauvik authored Nov 14, 2022
1 parent 6f71afe commit c6dba84
Show file tree
Hide file tree
Showing 16 changed files with 312 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ samples: build
@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)"
@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)"
@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)"
@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)"
@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)"
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/ArrayOfEnums.proto || echo "No messages found (SeveralMessages.proto)"
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Maps.proto || echo "No messages found (Maps.proto)"
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/MessageWithComments.proto || echo "No messages found (MessageWithComments.proto)"
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Proto2Required.proto || echo "No messages found (Proto2Required.proto)"
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Proto2NestedMessage.proto || echo "No messages found (Proto2NestedMessage.proto)"
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/GoogleValue.proto || echo "No messages found (GoogleValue.proto)"
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/GoogleInt64Value.proto || echo "No messages found (GoogleInt64Value.proto)"
@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)"
@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)"
@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)"
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas -I. --proto_path=${PROTO_PATH} ${PROTO_PATH}/OptionEnumsAsConstants.proto || echo "No messages found (OptionEnumsAsConstants.proto)"
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas -I. --proto_path=${PROTO_PATH} ${PROTO_PATH}/OptionFileExtension.proto || echo "No messages found (OptionFileExtension.proto)"
@protoc --plugin=bin/protoc-gen-jsonschema --jsonschema_out=jsonschemas -I. --proto_path=${PROTO_PATH} ${PROTO_PATH}/OptionIgnoredField.proto || echo "No messages found (HiddenFields.proto)"
Expand Down
34 changes: 34 additions & 0 deletions internal/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,40 @@ func configureSampleProtos() map[string]sampleProto {
ObjectsToValidateFail: []string{testdata.GoogleValueFail},
ObjectsToValidatePass: []string{testdata.GoogleValuePass},
},
"GoogleInt64Value": {
ExpectedJSONSchema: []string{testdata.GoogleInt64Value},
FilesToGenerate: []string{"GoogleInt64Value.proto"},
ProtoFileName: "GoogleInt64Value.proto",
ObjectsToValidateFail: []string{testdata.GoogleInt64ValueFail},
ObjectsToValidatePass: []string{testdata.GoogleInt64ValuePass},
},
"GoogleInt64ValueAllowNull": {
Flags: ConverterFlags{AllowNullValues: true},
ExpectedJSONSchema: []string{testdata.GoogleInt64ValueAllowNull},
FilesToGenerate: []string{"GoogleInt64ValueAllowNull.proto"},
ProtoFileName: "GoogleInt64ValueAllowNull.proto",
ObjectsToValidateFail: []string{testdata.GoogleInt64ValueAllowNullFail},
ObjectsToValidatePass: []string{testdata.GoogleInt64ValueAllowNullPass},
},
"GoogleInt64ValueDisallowString": {
Flags: ConverterFlags{DisallowBigIntsAsStrings: true},
ExpectedJSONSchema: []string{testdata.GoogleInt64ValueDisallowString},
FilesToGenerate: []string{"GoogleInt64ValueDisallowString.proto"},
ProtoFileName: "GoogleInt64ValueDisallowString.proto",
ObjectsToValidateFail: []string{testdata.GoogleInt64ValueDisallowStringFail},
ObjectsToValidatePass: []string{testdata.GoogleInt64ValueDisallowStringPass},
},
"GoogleInt64ValueDisallowStringAllowNull": {
Flags: ConverterFlags{
DisallowBigIntsAsStrings: true,
AllowNullValues: true,
},
ExpectedJSONSchema: []string{testdata.GoogleInt64ValueDisallowStringAllowNull},
FilesToGenerate: []string{"GoogleInt64ValueDisallowStringAllowNull.proto"},
ProtoFileName: "GoogleInt64ValueDisallowStringAllowNull.proto",
ObjectsToValidateFail: []string{testdata.GoogleInt64ValueDisallowStringAllowNullFail},
ObjectsToValidatePass: []string{testdata.GoogleInt64ValueDisallowStringAllowNullPass},
},
"ImportedEnum": {
ExpectedJSONSchema: []string{testdata.ImportedEnum},
FilesToGenerate: []string{"ImportedEnum.proto"},
Expand Down
23 changes: 23 additions & 0 deletions internal/converter/testdata/google_int64_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package testdata

const GoogleInt64Value = `{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/GoogleInt64Value",
"definitions": {
"GoogleInt64Value": {
"properties": {
"big_number": {
"additionalProperties": true,
"type": "string"
}
},
"additionalProperties": true,
"type": "object",
"title": "Google Int 64 Value"
}
}
}`

const GoogleInt64ValueFail = `{"big_number": 12345}`

const GoogleInt64ValuePass = `{"big_number": "12345"}`
38 changes: 38 additions & 0 deletions internal/converter/testdata/google_int64_value_allow_null.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package testdata

const GoogleInt64ValueAllowNull = `{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/GoogleInt64ValueAllowNull",
"definitions": {
"GoogleInt64ValueAllowNull": {
"properties": {
"big_number": {
"oneOf": [
{
"type": "null"
},
{
"type": "string"
}
],
"title": "Int 64 Value",
"description": "Wrapper message for ` + "`int64`" + `. The JSON representation for ` + "`Int64Value`" + ` is JSON string."
}
},
"additionalProperties": true,
"oneOf": [
{
"type": "null"
},
{
"type": "object"
}
],
"title": "Google Int 64 Value Allow Null"
}
}
}`

const GoogleInt64ValueAllowNullFail = `{"big_number": 12345}`

const GoogleInt64ValueAllowNullPass = `{"big_number": null}`
23 changes: 23 additions & 0 deletions internal/converter/testdata/google_int64_value_disallow_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package testdata

const GoogleInt64ValueDisallowString = `{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/GoogleInt64ValueDisallowString",
"definitions": {
"GoogleInt64ValueDisallowString": {
"properties": {
"big_number": {
"additionalProperties": true,
"type": "integer"
}
},
"additionalProperties": true,
"type": "object",
"title": "Google Int 64 Value Disallow String"
}
}
}`

const GoogleInt64ValueDisallowStringFail = `{"big_number": "12345"}`

const GoogleInt64ValueDisallowStringPass = `{"big_number": 12345}`
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package testdata

const GoogleInt64ValueDisallowStringAllowNull = `{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/GoogleInt64ValueDisallowStringAllowNull",
"definitions": {
"GoogleInt64ValueDisallowStringAllowNull": {
"properties": {
"big_number": {
"oneOf": [
{
"type": "null"
},
{
"type": "integer"
}
],
"title": "Int 64 Value",
"description": "Wrapper message for ` + "`int64`" + `. The JSON representation for ` + "`Int64Value`" + ` is JSON string."
}
},
"additionalProperties": true,
"oneOf": [
{
"type": "null"
},
{
"type": "object"
}
],
"title": "Google Int 64 Value Disallow String Allow Null"
}
}
}`

const GoogleInt64ValueDisallowStringAllowNullFail = `{"big_number": "12345"}`

const GoogleInt64ValueDisallowStringAllowNullPass = `{"big_number": null}`
8 changes: 8 additions & 0 deletions internal/converter/testdata/proto/GoogleInt64Value.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";
package samples;

import "google/protobuf/wrappers.proto";

message GoogleInt64Value {
google.protobuf.Int64Value big_number = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";
package samples;

import "google/protobuf/wrappers.proto";

message GoogleInt64ValueAllowNull {
google.protobuf.Int64Value big_number = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";
package samples;

import "google/protobuf/wrappers.proto";

message GoogleInt64ValueDisallowString {
google.protobuf.Int64Value big_number = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";
package samples;

import "google/protobuf/wrappers.proto";

message GoogleInt64ValueDisallowStringAllowNull {
google.protobuf.Int64Value big_number = 1;
}
14 changes: 13 additions & 1 deletion internal/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,18 @@ func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msgDesc *d
switch *msgDesc.Name {
case "DoubleValue", "FloatValue":
jsonSchemaType.Type = gojsonschema.TYPE_NUMBER
case "Int32Value", "UInt32Value", "Int64Value", "UInt64Value":
case "Int32Value", "UInt32Value":
jsonSchemaType.Type = gojsonschema.TYPE_INTEGER
case "Int64Value", "UInt64Value":
// BigInt as ints
if messageFlags.DisallowBigIntsAsStrings {
jsonSchemaType.Type = gojsonschema.TYPE_INTEGER
} else {

// BigInt as strings
jsonSchemaType.Type = gojsonschema.TYPE_STRING
}

case "BoolValue":
jsonSchemaType.Type = gojsonschema.TYPE_BOOLEAN
case "BytesValue", "StringValue":
Expand All @@ -527,6 +537,8 @@ func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msgDesc *d
// If we're allowing nulls then prepare a OneOf:
if messageFlags.AllowNullValues {
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Type: gojsonschema.TYPE_NULL}, &jsonschema.Type{Type: jsonSchemaType.Type})
// and clear the Type that was previously set.
jsonSchemaType.Type = ""
return jsonSchemaType, nil
}

Expand Down
17 changes: 17 additions & 0 deletions jsonschemas/GoogleInt64Value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/GoogleInt64Value",
"definitions": {
"GoogleInt64Value": {
"properties": {
"big_number": {
"additionalProperties": true,
"type": "string"
}
},
"additionalProperties": true,
"type": "object",
"title": "Google Int 64 Value"
}
}
}
32 changes: 32 additions & 0 deletions jsonschemas/GoogleInt64ValueAllowNull.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/GoogleInt64ValueAllowNull",
"definitions": {
"GoogleInt64ValueAllowNull": {
"properties": {
"big_number": {
"oneOf": [
{
"type": "null"
},
{
"type": "string"
}
],
"title": "Int 64 Value",
"description": "Wrapper message for `int64`. The JSON representation for `Int64Value` is JSON string."
}
},
"additionalProperties": true,
"oneOf": [
{
"type": "null"
},
{
"type": "object"
}
],
"title": "Google Int 64 Value Allow Null"
}
}
}
17 changes: 17 additions & 0 deletions jsonschemas/GoogleInt64ValueDisallowString.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/GoogleInt64ValueDisallowString",
"definitions": {
"GoogleInt64ValueDisallowString": {
"properties": {
"big_number": {
"additionalProperties": true,
"type": "integer"
}
},
"additionalProperties": true,
"type": "object",
"title": "Google Int 64 Value Disallow String"
}
}
}
32 changes: 32 additions & 0 deletions jsonschemas/GoogleInt64ValueDisallowStringAllowNull.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/GoogleInt64ValueDisallowStringAllowNull",
"definitions": {
"GoogleInt64ValueDisallowStringAllowNull": {
"properties": {
"big_number": {
"oneOf": [
{
"type": "null"
},
{
"type": "integer"
}
],
"title": "Int 64 Value",
"description": "Wrapper message for `int64`. The JSON representation for `Int64Value` is JSON string."
}
},
"additionalProperties": true,
"oneOf": [
{
"type": "null"
},
{
"type": "object"
}
],
"title": "Google Int 64 Value Disallow String Allow Null"
}
}
}
9 changes: 8 additions & 1 deletion jsonschemas/JSONFields.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"$ref": "#/definitions/JSONFields",
"definitions": {
"JSONFields": {
"required": [
"otherNumb"
],
"properties": {
"name": {
"type": "string"
Expand All @@ -21,10 +24,14 @@
},
"snakeNumb": {
"type": "string"
},
"otherNumb": {
"type": "integer"
}
},
"additionalProperties": true,
"type": "object"
"type": "object",
"title": "JSON Fields"
}
}
}

0 comments on commit c6dba84

Please sign in to comment.