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

Commit

Permalink
119 bug when disallowing bigints and allowing null values (#120)
Browse files Browse the repository at this point in the history
* Test for bigint-as-string

* Rendering bigint schemas as integer, even when allow-nulls is chosen

* Using more recent yaml import

Co-authored-by: Chrusty <>
  • Loading branch information
chrusty authored Jun 25, 2022
1 parent 860187f commit 1e10cf8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 13 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.14

require (
github.com/alecthomas/jsonschema v0.0.0-20210918223802-a1d3f4b43d7b
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/camelcase v1.0.0
github.com/iancoleman/orderedmap v0.2.0
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/iancoleman/strcase v0.2.0
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/sirupsen/logrus v1.4.2
Expand All @@ -16,4 +16,5 @@ require (
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
google.golang.org/protobuf v1.27.1
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456 h1:ng0gs1AKnRRuEMZoTLLlbOd+C17zUDepwGQBb/n+JVg=
golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand All @@ -52,5 +50,6 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
11 changes: 11 additions & 0 deletions internal/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ func configureSampleProtos() map[string]sampleProto {
ObjectsToValidateFail: []string{testdata.ArrayOfPrimitivesDoubleFail},
ObjectsToValidatePass: []string{testdata.ArrayOfPrimitivesDoublePass},
},
"BigIntAsString": {
Flags: ConverterFlags{
AllowNullValues: true,
DisallowBigIntsAsStrings: true,
},
ExpectedJSONSchema: []string{testdata.BigIntAsString},
FilesToGenerate: []string{"BigIntAsString.proto"},
ProtoFileName: "BigIntAsString.proto",
ObjectsToValidateFail: []string{testdata.BigIntAsStringFail},
ObjectsToValidatePass: []string{testdata.BigIntAsStringPass},
},
"BytesPayload": {
ExpectedJSONSchema: []string{testdata.BytesPayload},
FilesToGenerate: []string{"BytesPayload.proto"},
Expand Down
36 changes: 36 additions & 0 deletions internal/converter/testdata/bigint_as_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package testdata

const BigIntAsString = `{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/BigIntAsString",
"definitions": {
"BigIntAsString": {
"properties": {
"big_number": {
"oneOf": [
{
"type": "integer"
},
{
"type": "null"
}
]
}
},
"additionalProperties": true,
"oneOf": [
{
"type": "null"
},
{
"type": "object"
}
],
"title": "Big Int As String"
}
}
}`

const BigIntAsStringFail = `{"big_number": "1827634182736443333"}`

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

message BigIntAsString {
option (protoc.gen.jsonschema.message_options).allow_null_values = true;
int64 big_number = 1;
}
37 changes: 29 additions & 8 deletions internal/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes

// Switch the types, and pick a JSONSchema equivalent:
switch desc.GetType() {

// Float32:
case descriptor.FieldDescriptorProto_TYPE_DOUBLE,
descriptor.FieldDescriptorProto_TYPE_FLOAT:
if messageFlags.AllowNullValues {
Expand All @@ -94,6 +96,7 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
jsonSchemaType.Type = gojsonschema.TYPE_NUMBER
}

// Int32:
case descriptor.FieldDescriptorProto_TYPE_INT32,
descriptor.FieldDescriptorProto_TYPE_UINT32,
descriptor.FieldDescriptorProto_TYPE_FIXED32,
Expand All @@ -108,24 +111,38 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
jsonSchemaType.Type = gojsonschema.TYPE_INTEGER
}

// Int64:
case descriptor.FieldDescriptorProto_TYPE_INT64,
descriptor.FieldDescriptorProto_TYPE_UINT64,
descriptor.FieldDescriptorProto_TYPE_FIXED64,
descriptor.FieldDescriptorProto_TYPE_SFIXED64,
descriptor.FieldDescriptorProto_TYPE_SINT64:
if messageFlags.AllowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_STRING},
{Type: gojsonschema.TYPE_NULL},

// As integer:
if c.Flags.DisallowBigIntsAsStrings {
if messageFlags.AllowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_INTEGER},
{Type: gojsonschema.TYPE_NULL},
}
} else {
jsonSchemaType.Type = gojsonschema.TYPE_INTEGER
}
} else {
jsonSchemaType.Type = gojsonschema.TYPE_STRING
}

if c.Flags.DisallowBigIntsAsStrings {
jsonSchemaType.Type = gojsonschema.TYPE_INTEGER
// As string:
if !c.Flags.DisallowBigIntsAsStrings {
if messageFlags.AllowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_STRING},
{Type: gojsonschema.TYPE_NULL},
}
} else {
jsonSchemaType.Type = gojsonschema.TYPE_STRING
}
}

// String:
case descriptor.FieldDescriptorProto_TYPE_STRING:
stringDef := &jsonschema.Type{Type: gojsonschema.TYPE_STRING}
// Check for custom options
Expand Down Expand Up @@ -164,6 +181,7 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
}
}

// Bytes:
case descriptor.FieldDescriptorProto_TYPE_BYTES:
if messageFlags.AllowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
Expand All @@ -180,6 +198,7 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
jsonSchemaType.BinaryEncoding = "base64"
}

// ENUM:
case descriptor.FieldDescriptorProto_TYPE_ENUM:

// Go through all the enums we have, see if we can match any to this field.
Expand All @@ -197,6 +216,7 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes

jsonSchemaType = &enumSchema

// Bool:
case descriptor.FieldDescriptorProto_TYPE_BOOL:
if messageFlags.AllowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
Expand All @@ -207,6 +227,7 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
jsonSchemaType.Type = gojsonschema.TYPE_BOOLEAN
}

// Group (object):
case descriptor.FieldDescriptorProto_TYPE_GROUP, descriptor.FieldDescriptorProto_TYPE_MESSAGE:
switch desc.GetTypeName() {
// Make sure that durations match a particular string pattern (eg 3.4s):
Expand Down

0 comments on commit 1e10cf8

Please sign in to comment.