Skip to content

Commit 560b597

Browse files
authored
Include oneOfs in constructorTakesAllFields() and fix null check in generateOptionType (#3310)
* Include oneOfs in count for constructorTakesAllFields() * Fix null check in generateOptionType * Spotless
1 parent 4d23fb6 commit 560b597

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

wire-java-generator/src/main/java/com/squareup/wire/java/JavaGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ private TypeSpec generateMessage(MessageType type) {
879879

880880
/** Decides if a constructor should take all fields or a builder as a parameter. */
881881
private boolean constructorTakesAllFields(MessageType type) {
882-
return type.fields().size() < MAX_PARAMS_IN_CONSTRUCTOR;
882+
return type.getFieldsAndOneOfFields().size() < MAX_PARAMS_IN_CONSTRUCTOR;
883883
}
884884

885885
private TypeSpec generateEnclosingType(EnclosingType type) {
@@ -2340,7 +2340,7 @@ public ClassName generatedTypeName(ProtoMember member) {
23402340

23412341
if (!eligibleAsAnnotationMember(schema, field)) return null;
23422342
TypeName returnType;
2343-
if (field.getLabel().equals(Field.Label.REPEATED)) {
2343+
if (Field.Label.REPEATED.equals(field.getLabel())) {
23442344
TypeName typeName = typeName(field.getType());
23452345
if (typeName.equals(TypeName.LONG)
23462346
|| typeName.equals(TypeName.INT)

wire-java-generator/src/test/java/com/squareup/wire/java/JavaGeneratorTest.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,64 @@ public void tooManyFieldsTest() throws Exception {
8484
""
8585
+ "message Message {\n"
8686
+ s.toString()
87-
+ " oneof oneof_name {\n"
88-
+ " int32 foo = 257;\n"
89-
+ " int32 bar = 258;\n"
90-
+ " }\n"
87+
+ " oneof oneof_name {\n"
88+
+ " int32 foo = 257;\n"
89+
+ " int32 bar = 258;\n"
90+
+ " }\n"
91+
+ "}\n")
92+
.build();
93+
assertThat(new JavaWithProfilesGenerator(schema).generateJava("Message"))
94+
.contains("" + "public Message(Builder builder, ByteString unknownFields)");
95+
}
96+
97+
@Test
98+
public void tooManyFieldsInOneOfTest() throws Exception {
99+
StringBuilder s = new StringBuilder();
100+
for (int i = 3; i < 259; i++) {
101+
s.append(" int32 field_" + i + " = " + i + ";\n");
102+
}
103+
Schema schema =
104+
new SchemaBuilder()
105+
.add(
106+
Path.get("message.proto"),
107+
""
108+
+ "message Message {\n"
109+
+ " repeated int32 foo = 1;\n"
110+
+ " repeated int32 bar = 2;\n"
111+
+ " oneof oneof_name {\n"
112+
+ s.toString()
113+
+ " }\n"
91114
+ "}\n")
92115
.build();
93116
assertThat(new JavaWithProfilesGenerator(schema).generateJava("Message"))
94117
.contains("" + "public Message(Builder builder, ByteString unknownFields)");
95118
}
96119

120+
@Test
121+
public void nullLabelIsHandledDuringOptionGeneration() throws Exception {
122+
Schema schema =
123+
new SchemaBuilder()
124+
.add(
125+
Path.get("message.proto"),
126+
""
127+
+ "syntax = \"proto3\";\n"
128+
+ "import \"google/protobuf/descriptor.proto\";\n"
129+
+ "message Message {\n"
130+
+ " extend google.protobuf.MessageOptions {\n"
131+
+ " string owner = 55682;\n"
132+
+ " }\n"
133+
+ "}")
134+
.build();
135+
assertThat(new JavaWithProfilesGenerator(schema).generateJava("Message", null, false, true))
136+
.contains(
137+
""
138+
+ " @Retention(RetentionPolicy.RUNTIME)\n"
139+
+ " @Target(ElementType.TYPE)\n"
140+
+ " public @interface OwnerOption {\n"
141+
+ " String value();\n"
142+
+ " }");
143+
}
144+
97145
@Test
98146
public void map() throws Exception {
99147
Schema schema =

wire-java-generator/src/test/java/com/squareup/wire/java/JavaWithProfilesGenerator.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ internal class JavaWithProfilesGenerator(private val schema: Schema) {
5353
typeName: String,
5454
profileName: String? = null,
5555
buildersOnly: Boolean = false,
56+
emitDeclaredOptions: Boolean = false,
57+
emitAppliedOptions: Boolean = false,
5658
): String {
5759
val javaGenerator = JavaGenerator.get(schema)
5860
.withProfile(profile(profileName))
5961
.withBuildersOnly(buildersOnly)
62+
.withOptions(emitDeclaredOptions, emitAppliedOptions)
6063
val type = schema.getType(typeName)
6164
val typeSpec = javaGenerator.generateType(type)
6265
val packageName = javaGenerator.generatedTypeName(type).packageName()

0 commit comments

Comments
 (0)