diff --git a/fixtures/Cookie.proto b/fixtures/Cookie.proto new file mode 100644 index 00000000..3746d09f --- /dev/null +++ b/fixtures/Cookie.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; + +package com.example; + +/** + * Represents a cookie. + */ +message Cookie { + string id = 1; // The id of the cookie. + optional string name = 2; // The name of the cookie. + repeated string ingredients = 3; // Ingredients in the cookie. +} diff --git a/fixtures/cookie.pb b/fixtures/cookie.pb new file mode 100644 index 00000000..1ab7ed03 Binary files /dev/null and b/fixtures/cookie.pb differ diff --git a/fixtures/generate.go b/fixtures/generate.go index ad635fbb..0825e186 100644 --- a/fixtures/generate.go +++ b/fixtures/generate.go @@ -1,3 +1,9 @@ package fixtures //go:generate protoc --descriptor_set_out=fileset.pb --include_imports --include_source_info -I. -I../thirdparty Booking.proto Vehicle.proto + +// Compiling proto3 optional fields requires using protoc >=3.12.x and passing the --experimental_allow_proto3_optional flag. +// Rather than use this flag to compile all of the protocol buffers (which would eliminate test coverage for descriptors +// compiled without the flag), only pass this flag when compiling the one message explicitly testing proto3 optional fields. +// Once this feature is no longer behind an experimental flag, compilation of Cookie.proto can be moved to the above protoc command. +//go:generate protoc --experimental_allow_proto3_optional --descriptor_set_out=cookie.pb --include_imports --include_source_info -I. -I../thirdparty Cookie.proto diff --git a/template_test.go b/template_test.go index 6569edb9..13450ab3 100644 --- a/template_test.go +++ b/template_test.go @@ -17,6 +17,9 @@ var ( template *Template bookingFile *File vehicleFile *File + + cookieTemplate *Template + cookieFile *File ) func TestMain(m *testing.M) { @@ -30,6 +33,12 @@ func TestMain(m *testing.M) { bookingFile = template.Files[0] vehicleFile = template.Files[1] + set, _ = utils.LoadDescriptorSet("fixtures", "cookie.pb") + req = utils.CreateGenRequest(set, "Cookie.proto") + result = protokit.ParseCodeGenRequest(req) + cookieTemplate = NewTemplate(result) + cookieFile = cookieTemplate.Files[0] + os.Exit(m.Run()) } @@ -315,6 +324,41 @@ func TestFieldPropertiesProto3(t *testing.T) { require.Empty(t, field.Options) } +func TestFieldPropertiesProto3Optional(t *testing.T) { + msg := findMessage("Cookie", cookieFile) + + field := findField("id", msg) + require.Equal(t, "id", field.Name) + require.Equal(t, "The id of the cookie.", field.Description) + require.Equal(t, "", field.Label) + require.Equal(t, "string", field.Type) + require.Equal(t, "string", field.LongType) + require.Equal(t, "string", field.FullType) + require.Empty(t, field.DefaultValue) + require.Empty(t, field.Options) + + field = findField("name", msg) + require.Equal(t, "name", field.Name) + require.Equal(t, "The name of the cookie.", field.Description) + //TODO(ezimanyi): This is a proto3 optional field, and should have label "optional" + require.Equal(t, "", field.Label) + require.Equal(t, "string", field.Type) + require.Equal(t, "string", field.LongType) + require.Equal(t, "string", field.FullType) + require.Empty(t, field.DefaultValue) + require.Empty(t, field.Options) + + field = findField("ingredients", msg) + require.Equal(t, "ingredients", field.Name) + require.Equal(t, "Ingredients in the cookie.", field.Description) + require.Equal(t, "repeated", field.Label) + require.Equal(t, "string", field.Type) + require.Equal(t, "string", field.LongType) + require.Equal(t, "string", field.FullType) + require.Empty(t, field.DefaultValue) + require.Empty(t, field.Options) +} + func TestServiceProperties(t *testing.T) { service := findService("VehicleService", vehicleFile) require.Equal(t, "VehicleService", service.Name)