1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9//     * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//     * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15//     * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// Author: benjy@google.com (Benjy Weinberger)
32//  Based on original Protocol Buffers design by
33//  Sanjay Ghemawat, Jeff Dean, and others.
34//
35// A proto file used to test the "custom options" feature of google.protobuf.
36
37syntax = "proto2";
38
39// Some generic_services option(s) added automatically.
40// See:  http://go/proto2-generic-services-default
41option cc_generic_services = true;     // auto-added
42option java_generic_services = true;   // auto-added
43option py_generic_services = true;
44
45// A custom file option (defined below).
46option (file_opt1) = 9876543210;
47
48import "google/protobuf/descriptor.proto";
49
50// We don't put this in a package within proto2 because we need to make sure
51// that the generated code doesn't depend on being in the proto2 namespace.
52package protobuf_unittest;
53
54
55// Some simple test custom options of various types.
56
57extend google.protobuf.FileOptions {
58  optional uint64 file_opt1 = 7736974;
59}
60
61extend google.protobuf.MessageOptions {
62  optional int32 message_opt1 = 7739036;
63}
64
65extend google.protobuf.FieldOptions {
66  optional fixed64 field_opt1 = 7740936;
67  // This is useful for testing that we correctly register default values for
68  // extension options.
69  optional int32 field_opt2 = 7753913 [default=42];
70}
71
72extend google.protobuf.OneofOptions {
73  optional int32 oneof_opt1 = 7740111;
74}
75
76extend google.protobuf.EnumOptions {
77  optional sfixed32 enum_opt1 = 7753576;
78}
79
80extend google.protobuf.EnumValueOptions {
81  optional int32 enum_value_opt1 = 1560678;
82}
83
84extend google.protobuf.ServiceOptions {
85  optional sint64 service_opt1 = 7887650;
86}
87
88enum MethodOpt1 {
89  METHODOPT1_VAL1 = 1;
90  METHODOPT1_VAL2 = 2;
91}
92
93extend google.protobuf.MethodOptions {
94  optional MethodOpt1 method_opt1 = 7890860;
95}
96
97// A test message with custom options at all possible locations (and also some
98// regular options, to make sure they interact nicely).
99message TestMessageWithCustomOptions {
100  option message_set_wire_format = false;
101
102  option (message_opt1) = -56;
103
104  optional string field1 = 1 [ctype=CORD,
105                              (field_opt1)=8765432109];
106
107  oneof AnOneof {
108    option (oneof_opt1) = -99;
109    int32 oneof_field = 2;
110  }
111
112  enum AnEnum {
113    option (enum_opt1) = -789;
114
115    ANENUM_VAL1 = 1;
116    ANENUM_VAL2 = 2 [(enum_value_opt1) = 123];
117  }
118}
119
120
121// A test RPC service with custom options at all possible locations (and also
122// some regular options, to make sure they interact nicely).
123message CustomOptionFooRequest {
124}
125
126message CustomOptionFooResponse {
127}
128
129message CustomOptionFooClientMessage {
130}
131
132message CustomOptionFooServerMessage {
133}
134
135service TestServiceWithCustomOptions {
136  option (service_opt1) = -9876543210;
137
138  rpc Foo(CustomOptionFooRequest) returns (CustomOptionFooResponse) {
139    option (method_opt1) = METHODOPT1_VAL2;
140  }
141}
142
143
144
145// Options of every possible field type, so we can test them all exhaustively.
146
147message DummyMessageContainingEnum {
148  enum TestEnumType {
149    TEST_OPTION_ENUM_TYPE1 = 22;
150    TEST_OPTION_ENUM_TYPE2 = -23;
151  }
152}
153
154message DummyMessageInvalidAsOptionType {
155}
156
157extend google.protobuf.MessageOptions {
158  optional         bool     bool_opt = 7706090;
159  optional        int32    int32_opt = 7705709;
160  optional        int64    int64_opt = 7705542;
161  optional       uint32   uint32_opt = 7704880;
162  optional       uint64   uint64_opt = 7702367;
163  optional       sint32   sint32_opt = 7701568;
164  optional       sint64   sint64_opt = 7700863;
165  optional      fixed32  fixed32_opt = 7700307;
166  optional      fixed64  fixed64_opt = 7700194;
167  optional     sfixed32 sfixed32_opt = 7698645;
168  optional     sfixed64 sfixed64_opt = 7685475;
169  optional        float    float_opt = 7675390;
170  optional       double   double_opt = 7673293;
171  optional       string   string_opt = 7673285;
172  optional        bytes    bytes_opt = 7673238;
173  optional DummyMessageContainingEnum.TestEnumType enum_opt = 7673233;
174  optional DummyMessageInvalidAsOptionType message_type_opt = 7665967;
175}
176
177message CustomOptionMinIntegerValues {
178  option     (bool_opt) = false;
179  option    (int32_opt) = -0x80000000;
180  option    (int64_opt) = -0x8000000000000000;
181  option   (uint32_opt) = 0;
182  option   (uint64_opt) = 0;
183  option   (sint32_opt) = -0x80000000;
184  option   (sint64_opt) = -0x8000000000000000;
185  option  (fixed32_opt) = 0;
186  option  (fixed64_opt) = 0;
187  option (sfixed32_opt) = -0x80000000;
188  option (sfixed64_opt) = -0x8000000000000000;
189}
190
191message CustomOptionMaxIntegerValues {
192  option     (bool_opt) = true;
193  option    (int32_opt) = 0x7FFFFFFF;
194  option    (int64_opt) = 0x7FFFFFFFFFFFFFFF;
195  option   (uint32_opt) = 0xFFFFFFFF;
196  option   (uint64_opt) = 0xFFFFFFFFFFFFFFFF;
197  option   (sint32_opt) = 0x7FFFFFFF;
198  option   (sint64_opt) = 0x7FFFFFFFFFFFFFFF;
199  option  (fixed32_opt) = 0xFFFFFFFF;
200  option  (fixed64_opt) = 0xFFFFFFFFFFFFFFFF;
201  option (sfixed32_opt) = 0x7FFFFFFF;
202  option (sfixed64_opt) = 0x7FFFFFFFFFFFFFFF;
203}
204
205message CustomOptionOtherValues {
206  option  (int32_opt) = -100;  // To test sign-extension.
207  option  (float_opt) = 12.3456789;
208  option (double_opt) = 1.234567890123456789;
209  option (string_opt) = "Hello, \"World\"";
210  option  (bytes_opt) = "Hello\0World";
211  option   (enum_opt) = TEST_OPTION_ENUM_TYPE2;
212}
213
214message SettingRealsFromPositiveInts {
215  option  (float_opt) = 12;
216  option (double_opt) = 154;
217}
218
219message SettingRealsFromNegativeInts {
220  option  (float_opt) = -12;
221  option  (double_opt) = -154;
222}
223
224// Options of complex message types, themselves combined and extended in
225// various ways.
226
227message ComplexOptionType1 {
228  optional int32 foo = 1;
229  optional int32 foo2 = 2;
230  optional int32 foo3 = 3;
231  repeated int32 foo4 = 4;
232
233  extensions 100 to max;
234}
235
236message ComplexOptionType2 {
237  optional ComplexOptionType1 bar = 1;
238  optional int32 baz = 2;
239
240  message ComplexOptionType4 {
241    optional int32 waldo = 1;
242
243    extend google.protobuf.MessageOptions {
244      optional ComplexOptionType4 complex_opt4 = 7633546;
245    }
246  }
247
248  optional ComplexOptionType4 fred = 3;
249  repeated ComplexOptionType4 barney = 4;
250
251  extensions 100 to max;
252}
253
254message ComplexOptionType3 {
255  optional int32 qux = 1;
256
257  optional group ComplexOptionType5 = 2 {
258    optional int32 plugh = 3;
259  }
260}
261
262extend ComplexOptionType1 {
263  optional int32 quux = 7663707;
264  optional ComplexOptionType3 corge = 7663442;
265}
266
267extend ComplexOptionType2 {
268  optional int32 grault = 7650927;
269  optional ComplexOptionType1 garply = 7649992;
270}
271
272extend google.protobuf.MessageOptions {
273  optional protobuf_unittest.ComplexOptionType1 complex_opt1 = 7646756;
274  optional ComplexOptionType2 complex_opt2 = 7636949;
275  optional ComplexOptionType3 complex_opt3 = 7636463;
276  optional group ComplexOpt6 = 7595468 {
277    optional int32 xyzzy = 7593951;
278  }
279}
280
281// Note that we try various different ways of naming the same extension.
282message VariousComplexOptions {
283  option (.protobuf_unittest.complex_opt1).foo = 42;
284  option (protobuf_unittest.complex_opt1).(.protobuf_unittest.quux) = 324;
285  option (.protobuf_unittest.complex_opt1).(protobuf_unittest.corge).qux = 876;
286  option (protobuf_unittest.complex_opt1).foo4 = 99;
287  option (protobuf_unittest.complex_opt1).foo4 = 88;
288  option (complex_opt2).baz = 987;
289  option (complex_opt2).(grault) = 654;
290  option (complex_opt2).bar.foo = 743;
291  option (complex_opt2).bar.(quux) = 1999;
292  option (complex_opt2).bar.(protobuf_unittest.corge).qux = 2008;
293  option (complex_opt2).(garply).foo = 741;
294  option (complex_opt2).(garply).(.protobuf_unittest.quux) = 1998;
295  option (complex_opt2).(protobuf_unittest.garply).(corge).qux = 2121;
296  option (ComplexOptionType2.ComplexOptionType4.complex_opt4).waldo = 1971;
297  option (complex_opt2).fred.waldo = 321;
298  option (complex_opt2).barney = { waldo: 101 };
299  option (complex_opt2).barney = { waldo: 212 };
300  option (protobuf_unittest.complex_opt3).qux = 9;
301  option (complex_opt3).complexoptiontype5.plugh = 22;
302  option (complexopt6).xyzzy = 24;
303}
304
305// ------------------------------------------------------
306// Definitions for testing aggregate option parsing.
307// See descriptor_unittest.cc.
308
309message AggregateMessageSet {
310  option message_set_wire_format = true;
311  extensions 4 to max;
312}
313
314message AggregateMessageSetElement {
315  extend AggregateMessageSet {
316    optional AggregateMessageSetElement message_set_extension = 15447542;
317  }
318  optional string s = 1;
319}
320
321// A helper type used to test aggregate option parsing
322message Aggregate {
323  optional int32 i = 1;
324  optional string s = 2;
325
326  // A nested object
327  optional Aggregate sub = 3;
328
329  // To test the parsing of extensions inside aggregate values
330  optional google.protobuf.FileOptions file = 4;
331  extend google.protobuf.FileOptions {
332    optional Aggregate nested = 15476903;
333  }
334
335  // An embedded message set
336  optional AggregateMessageSet mset = 5;
337}
338
339// Allow Aggregate to be used as an option at all possible locations
340// in the .proto grammer.
341extend google.protobuf.FileOptions      { optional Aggregate fileopt    = 15478479; }
342extend google.protobuf.MessageOptions   { optional Aggregate msgopt     = 15480088; }
343extend google.protobuf.FieldOptions     { optional Aggregate fieldopt   = 15481374; }
344extend google.protobuf.EnumOptions      { optional Aggregate enumopt    = 15483218; }
345extend google.protobuf.EnumValueOptions { optional Aggregate enumvalopt = 15486921; }
346extend google.protobuf.ServiceOptions   { optional Aggregate serviceopt = 15497145; }
347extend google.protobuf.MethodOptions    { optional Aggregate methodopt  = 15512713; }
348
349// Try using AggregateOption at different points in the proto grammar
350option (fileopt) = {
351  s: 'FileAnnotation'
352  // Also test the handling of comments
353  /* of both types */ i: 100
354
355  sub { s: 'NestedFileAnnotation' }
356
357  // Include a google.protobuf.FileOptions and recursively extend it with
358  // another fileopt.
359  file {
360    [protobuf_unittest.fileopt] {
361      s:'FileExtensionAnnotation'
362    }
363  }
364
365  // A message set inside an option value
366  mset {
367    [protobuf_unittest.AggregateMessageSetElement.message_set_extension] {
368      s: 'EmbeddedMessageSetElement'
369    }
370  }
371};
372
373message AggregateMessage {
374  option (msgopt) = { i:101 s:'MessageAnnotation' };
375  optional int32 fieldname = 1 [(fieldopt) = { s:'FieldAnnotation' }];
376}
377
378service AggregateService {
379  option (serviceopt) = { s:'ServiceAnnotation' };
380  rpc Method (AggregateMessage) returns (AggregateMessage) {
381    option (methodopt) = { s:'MethodAnnotation' };
382  }
383}
384
385enum AggregateEnum {
386  option (enumopt) = { s:'EnumAnnotation' };
387  VALUE = 1 [(enumvalopt) = { s:'EnumValueAnnotation' }];
388}
389
390// Test custom options for nested type.
391message NestedOptionType {
392  message NestedMessage {
393    option (message_opt1) = 1001;
394    optional int32 nested_field = 1 [(field_opt1) = 1002];
395  }
396  enum NestedEnum {
397    option (enum_opt1) = 1003;
398    NESTED_ENUM_VALUE = 1 [(enum_value_opt1) = 1004];
399  }
400  extend google.protobuf.FileOptions {
401    optional int32 nested_extension = 7912573 [(field_opt2) = 1005];
402  }
403}
404
405// Custom message option that has a required enum field.
406// WARNING: this is strongly discouraged!
407message OldOptionType {
408  enum TestEnum {
409    OLD_VALUE = 0;
410  }
411  required TestEnum value = 1;
412}
413
414// Updated version of the custom option above.
415message NewOptionType {
416  enum TestEnum {
417    OLD_VALUE = 0;
418    NEW_VALUE = 1;
419  }
420  required TestEnum value = 1;
421}
422
423extend google.protobuf.MessageOptions {
424  optional OldOptionType required_enum_opt = 106161807;
425}
426
427// Test message using the "required_enum_opt" option defined above.
428message TestMessageWithRequiredEnumOption {
429  option (required_enum_opt) = { value: OLD_VALUE };
430}
431