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

Commit

Permalink
support google timestamps, use date-time format in json schema (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: chris <chris@Profanity.local>
  • Loading branch information
chrusty and chris authored Jul 11, 2020
1 parent 5a5c815 commit aa01df3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ samples:
@PATH=./bin:$$PATH; protoc --jsonschema_out=disallow_bigints_as_strings:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/PayloadMessage.proto || echo "No messages found (PayloadMessage.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=disallow_bigints_as_strings:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/SeveralEnums.proto || echo "No messages found (SeveralEnums.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=disallow_bigints_as_strings:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/SeveralMessages.proto || echo "No messages found (SeveralMessages.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=disallow_bigints_as_strings:jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Timestamp.proto || echo "No messages found (Timestamp.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/ArrayOfEnums.proto || echo "No messages found (SeveralMessages.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/Maps.proto || echo "No messages found (Maps.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/MessageWithComments.proto || echo "No messages found (MessageWithComments.proto)"
@PATH=./bin:$$PATH; protoc -I /usr/include --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/WellKnown.proto
@PATH=./bin:$$PATH; protoc -I /usr/include --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/WellKnown.proto || echo "No messages found (WellKnown.proto)"
@PATH=./bin:$$PATH; protoc --jsonschema_out=jsonschemas --proto_path=${PROTO_PATH} ${PROTO_PATH}/NoPackage.proto

test:
Expand Down
12 changes: 10 additions & 2 deletions internal/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestGenerateJsonSchema(t *testing.T) {
testConvertSampleProto(t, sampleProtos["Maps"])
testConvertSampleProto(t, sampleProtos["SelfReference"])
testConvertSampleProto(t, sampleProtos["CyclicalReference"])
testConvertSampleProto(t, sampleProtos["Timestamp"])
}

func testConvertSampleProto(t *testing.T, sampleProto sampleProto) {
Expand Down Expand Up @@ -203,26 +204,33 @@ func configureSampleProtos() {
ProtoFileName: "MessageWithComments.proto",
}

// Self referencing proto message (see https://github.com/chrusty/protoc-gen-jsonschema/issues/7)
// Self referencing proto message (see https://github.com/chrusty/protoc-gen-jsonschema/issues/7):
sampleProtos["SelfReference"] = sampleProto{
ExpectedJSONSchema: []string{testdata.SelfReference},
FilesToGenerate: []string{"SelfReference.proto"},
ProtoFileName: "SelfReference.proto",
}

// Messages that depend on one another so as to form a cycle (see https://github.com/chrusty/protoc-gen-jsonschema/issues/20)
// Messages that depend on one another so as to form a cycle (see https://github.com/chrusty/protoc-gen-jsonschema/issues/20):
sampleProtos["CyclicalReference"] = sampleProto{
ExpectedJSONSchema: []string{testdata.CyclicalReferenceMessageM, testdata.CyclicalReferenceMessageFoo, testdata.CyclicalReferenceMessageBar, testdata.CyclicalReferenceMessageBaz},
FilesToGenerate: []string{"CyclicalReference.proto"},
ProtoFileName: "CyclicalReference.proto",
}

// Google's well-known types:
sampleProtos["WellKnown"] = sampleProto{
ExpectedJSONSchema: []string{testdata.WellKnown},
FilesToGenerate: []string{"WellKnown.proto"},
ProtoFileName: "WellKnown.proto",
}

// Timestamp:
sampleProtos["Timestamp"] = sampleProto{
ExpectedJSONSchema: []string{testdata.Timestamp},
FilesToGenerate: []string{"Timestamp.proto"},
ProtoFileName: "Timestamp.proto",
}
}

// Load the specified .proto files into a FileDescriptorSet. Any errors in loading/parsing will
Expand Down
8 changes: 8 additions & 0 deletions internal/converter/testdata/proto/Timestamp.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";
package samples;

import "google/protobuf/timestamp.proto";

message Timestamp {
google.protobuf.Timestamp timestamp = 1;
}
13 changes: 13 additions & 0 deletions internal/converter/testdata/timestamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package testdata

const Timestamp = `{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"timestamp": {
"type": "string",
"format": "date-time"
}
},
"additionalProperties": true,
"type": "object"
}`
21 changes: 13 additions & 8 deletions internal/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,19 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
jsonSchemaType.Type = gojsonschema.TYPE_BOOLEAN
}

case descriptor.FieldDescriptorProto_TYPE_GROUP,
descriptor.FieldDescriptorProto_TYPE_MESSAGE:
jsonSchemaType.Type = gojsonschema.TYPE_OBJECT
if desc.GetLabel() == descriptor.FieldDescriptorProto_LABEL_OPTIONAL {
jsonSchemaType.AdditionalProperties = []byte("true")
}
if desc.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REQUIRED {
jsonSchemaType.AdditionalProperties = []byte("false")
case descriptor.FieldDescriptorProto_TYPE_GROUP, descriptor.FieldDescriptorProto_TYPE_MESSAGE:
switch desc.GetTypeName() {
case ".google.protobuf.Timestamp":
jsonSchemaType.Type = gojsonschema.TYPE_STRING
jsonSchemaType.Format = "date-time"
default:
jsonSchemaType.Type = gojsonschema.TYPE_OBJECT
if desc.GetLabel() == descriptor.FieldDescriptorProto_LABEL_OPTIONAL {
jsonSchemaType.AdditionalProperties = []byte("true")
}
if desc.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REQUIRED {
jsonSchemaType.AdditionalProperties = []byte("false")
}
}

default:
Expand Down
11 changes: 11 additions & 0 deletions jsonschemas/Timestamp.jsonschema
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"timestamp": {
"type": "string",
"format": "date-time"
}
},
"additionalProperties": true,
"type": "object"
}

0 comments on commit aa01df3

Please sign in to comment.