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

Commit 1e10cf8

Browse files
authored
119 bug when disallowing bigints and allowing null values (#120)
* 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 <>
1 parent 860187f commit 1e10cf8

File tree

6 files changed

+89
-13
lines changed

6 files changed

+89
-13
lines changed

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ go 1.14
44

55
require (
66
github.com/alecthomas/jsonschema v0.0.0-20210918223802-a1d3f4b43d7b
7-
github.com/fatih/camelcase v1.0.0 // indirect
7+
github.com/fatih/camelcase v1.0.0
88
github.com/iancoleman/orderedmap v0.2.0
9-
github.com/iancoleman/strcase v0.2.0 // indirect
9+
github.com/iancoleman/strcase v0.2.0
1010
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1111
github.com/kr/pretty v0.1.0 // indirect
1212
github.com/sirupsen/logrus v1.4.2
@@ -16,4 +16,5 @@ require (
1616
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
1717
google.golang.org/protobuf v1.27.1
1818
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
19+
gopkg.in/yaml.v2 v2.4.0 // indirect
1920
)

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:
4040
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
4141
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
4242
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
43-
golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456 h1:ng0gs1AKnRRuEMZoTLLlbOd+C17zUDepwGQBb/n+JVg=
44-
golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4543
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
4644
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4745
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
@@ -52,5 +50,6 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
5250
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5351
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
5452
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
55-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
5653
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
54+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
55+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

internal/converter/converter_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ func configureSampleProtos() map[string]sampleProto {
171171
ObjectsToValidateFail: []string{testdata.ArrayOfPrimitivesDoubleFail},
172172
ObjectsToValidatePass: []string{testdata.ArrayOfPrimitivesDoublePass},
173173
},
174+
"BigIntAsString": {
175+
Flags: ConverterFlags{
176+
AllowNullValues: true,
177+
DisallowBigIntsAsStrings: true,
178+
},
179+
ExpectedJSONSchema: []string{testdata.BigIntAsString},
180+
FilesToGenerate: []string{"BigIntAsString.proto"},
181+
ProtoFileName: "BigIntAsString.proto",
182+
ObjectsToValidateFail: []string{testdata.BigIntAsStringFail},
183+
ObjectsToValidatePass: []string{testdata.BigIntAsStringPass},
184+
},
174185
"BytesPayload": {
175186
ExpectedJSONSchema: []string{testdata.BytesPayload},
176187
FilesToGenerate: []string{"BytesPayload.proto"},
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package testdata
2+
3+
const BigIntAsString = `{
4+
"$schema": "http://json-schema.org/draft-04/schema#",
5+
"$ref": "#/definitions/BigIntAsString",
6+
"definitions": {
7+
"BigIntAsString": {
8+
"properties": {
9+
"big_number": {
10+
"oneOf": [
11+
{
12+
"type": "integer"
13+
},
14+
{
15+
"type": "null"
16+
}
17+
]
18+
}
19+
},
20+
"additionalProperties": true,
21+
"oneOf": [
22+
{
23+
"type": "null"
24+
},
25+
{
26+
"type": "object"
27+
}
28+
],
29+
"title": "Big Int As String"
30+
}
31+
}
32+
}`
33+
34+
const BigIntAsStringFail = `{"big_number": "1827634182736443333"}`
35+
36+
const BigIntAsStringPass = `{"big_number": 1827634182736443333}`
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+
import "options.proto";
4+
5+
message BigIntAsString {
6+
option (protoc.gen.jsonschema.message_options).allow_null_values = true;
7+
int64 big_number = 1;
8+
}

internal/converter/types.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
8383

8484
// Switch the types, and pick a JSONSchema equivalent:
8585
switch desc.GetType() {
86+
87+
// Float32:
8688
case descriptor.FieldDescriptorProto_TYPE_DOUBLE,
8789
descriptor.FieldDescriptorProto_TYPE_FLOAT:
8890
if messageFlags.AllowNullValues {
@@ -94,6 +96,7 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
9496
jsonSchemaType.Type = gojsonschema.TYPE_NUMBER
9597
}
9698

99+
// Int32:
97100
case descriptor.FieldDescriptorProto_TYPE_INT32,
98101
descriptor.FieldDescriptorProto_TYPE_UINT32,
99102
descriptor.FieldDescriptorProto_TYPE_FIXED32,
@@ -108,24 +111,38 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
108111
jsonSchemaType.Type = gojsonschema.TYPE_INTEGER
109112
}
110113

114+
// Int64:
111115
case descriptor.FieldDescriptorProto_TYPE_INT64,
112116
descriptor.FieldDescriptorProto_TYPE_UINT64,
113117
descriptor.FieldDescriptorProto_TYPE_FIXED64,
114118
descriptor.FieldDescriptorProto_TYPE_SFIXED64,
115119
descriptor.FieldDescriptorProto_TYPE_SINT64:
116-
if messageFlags.AllowNullValues {
117-
jsonSchemaType.OneOf = []*jsonschema.Type{
118-
{Type: gojsonschema.TYPE_STRING},
119-
{Type: gojsonschema.TYPE_NULL},
120+
121+
// As integer:
122+
if c.Flags.DisallowBigIntsAsStrings {
123+
if messageFlags.AllowNullValues {
124+
jsonSchemaType.OneOf = []*jsonschema.Type{
125+
{Type: gojsonschema.TYPE_INTEGER},
126+
{Type: gojsonschema.TYPE_NULL},
127+
}
128+
} else {
129+
jsonSchemaType.Type = gojsonschema.TYPE_INTEGER
120130
}
121-
} else {
122-
jsonSchemaType.Type = gojsonschema.TYPE_STRING
123131
}
124132

125-
if c.Flags.DisallowBigIntsAsStrings {
126-
jsonSchemaType.Type = gojsonschema.TYPE_INTEGER
133+
// As string:
134+
if !c.Flags.DisallowBigIntsAsStrings {
135+
if messageFlags.AllowNullValues {
136+
jsonSchemaType.OneOf = []*jsonschema.Type{
137+
{Type: gojsonschema.TYPE_STRING},
138+
{Type: gojsonschema.TYPE_NULL},
139+
}
140+
} else {
141+
jsonSchemaType.Type = gojsonschema.TYPE_STRING
142+
}
127143
}
128144

145+
// String:
129146
case descriptor.FieldDescriptorProto_TYPE_STRING:
130147
stringDef := &jsonschema.Type{Type: gojsonschema.TYPE_STRING}
131148
// Check for custom options
@@ -164,6 +181,7 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
164181
}
165182
}
166183

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

201+
// ENUM:
183202
case descriptor.FieldDescriptorProto_TYPE_ENUM:
184203

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

198217
jsonSchemaType = &enumSchema
199218

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

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

0 commit comments

Comments
 (0)