Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't arbitrarily prune oneOf options #2671

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions wire-schema/api/wire-schema.api
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ public final class com/squareup/wire/schema/MessageType : com/squareup/wire/sche
public final fun isDeprecated ()Z
public fun linkMembers (Lcom/squareup/wire/schema/Linker;)V
public fun linkOptions (Lcom/squareup/wire/schema/Linker;Lcom/squareup/wire/schema/SyntaxRules;Z)V
public final fun oneOf (Ljava/lang/String;)Lcom/squareup/wire/schema/OneOf;
public fun retainAll (Lcom/squareup/wire/schema/Schema;Lcom/squareup/wire/schema/MarkSet;)Lcom/squareup/wire/schema/Type;
public fun retainLinked (Ljava/util/Set;Ljava/util/Set;)Lcom/squareup/wire/schema/Type;
public final fun toElement ()Lcom/squareup/wire/schema/internal/parser/MessageElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ data class MessageType(
fun extensionField(qualifiedName: String): Field? =
extensionFields.firstOrNull { it.qualifiedName == qualifiedName }

/** Returns the oneOf named [name], or null if this type has no such oneOf. */
fun oneOf(name: String): OneOf? =
oneOfs.firstOrNull { it.name == name }

/** Returns the field tagged [tag], or null if this type has no such field. */
fun field(tag: Int): Field? {
for (field in declaredFields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ class Pruner(
val member = protoMember.member
return when (val type = schema.getType(protoMember.type)) {
is MessageType -> {
val field = type.field(member) ?: type.extensionField(member)!!
pruningRules.isFieldRetainedVersion(field.options)
val field = type.field(member) ?: type.extensionField(member)
if (field != null) {
pruningRules.isFieldRetainedVersion(field.options)
} else {
pruningRules.isFieldRetainedVersion(type.oneOf(member)!!.options)
}
}
is EnumType -> {
val enumConstant = type.constant(member)!!
Expand Down Expand Up @@ -177,9 +181,13 @@ class Pruner(

if (type is MessageType) {
val field = type.field(member) ?: type.extensionField(member)
checkNotNull(field) { "unexpected member: $member" }
result.add(field.type)
options = field.options
if (field != null) {
result.add(field.type)
options = field.options
} else {
val oneOf = checkNotNull(type.oneOf(member)) { "unexpected member: $member" }
options = oneOf.options
}
} else if (type is EnumType) {
val constant = type.constant(member)
?: throw IllegalStateException("unexpected member: $member")
Expand Down Expand Up @@ -218,6 +226,7 @@ class Pruner(
result.add(get(root, field.qualifiedName))
}
for (oneOf in type.oneOfs) {
result.add(get(root, oneOf.name))
for (field in oneOf.fields) {
result.add(get(root, field.name))
}
Expand Down
129 changes: 129 additions & 0 deletions wire-schema/src/jvmTest/kotlin/com/squareup/wire/schema/PrunerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,135 @@ class PrunerTest {
assertThat(pruned.getType("MessageB")).isNull()
}

@Test
fun oneOfOptionsAreNotArbitrarilyPruned() {
val schema = buildSchema {
add(
"test_event.proto".toPath(),
"""
|syntax = "proto3";
|
|import "test_event_custom_option.proto";
|
|package test.oneOf.options.test;
|
|message TestMessage {
| oneof element {
| option (my_custom_oneOf_option) = true;
| string one = 1;
| string two = 2;
| }
|}
""".trimMargin(),
)
add(
"test_event_custom_option.proto".toPath(),
"""
|syntax = "proto3";
|
|import "google/protobuf/descriptor.proto";
|
|package test.oneOf.options;
|
|extend google.protobuf.OneofOptions {
| bool my_custom_oneOf_option = 101400;
|}
""".trimMargin(),
)
}
val pruned = schema.prune(
PruningRules.Builder()
.addRoot("test.oneOf.options.test.TestMessage")
.build(),
)
assertThat(pruned.protoFile("test_event.proto")!!.toSchema())
.isEqualTo(
// spotless:off because spotless will remove the indents (trailing spaces) in the oneof block.
"""
|// Proto schema formatted by Wire, do not edit.
|// Source: test_event.proto
|
|syntax = "proto3";
|
|package test.oneOf.options.test;
|
|import "test_event_custom_option.proto";
|
|message TestMessage {
| oneof element {
| option (my_custom_oneOf_option) = true;
|
| string one = 1;
| string two = 2;
| }
|}
|""".trimMargin(),
// spotless:on
)
}

@Test
fun oneOfOptionsArePruned() {
val schema = buildSchema {
add(
"test_event.proto".toPath(),
"""
|syntax = "proto3";
|
|import "test_event_custom_option.proto";
|
|package test.oneOf.options.test;
|
|message TestMessage {
| oneof element {
| option (my_custom_oneOf_option) = true;
| string one = 1;
| string two = 2;
| }
|}
""".trimMargin(),
)
add(
"test_event_custom_option.proto".toPath(),
"""
|syntax = "proto3";
|
|import "google/protobuf/descriptor.proto";
|
|package test.oneOf.options;
|
|extend google.protobuf.OneofOptions {
| bool my_custom_oneOf_option = 101400;
|}
""".trimMargin(),
)
}
val pruned = schema.prune(
PruningRules.Builder()
.prune("google.protobuf.OneofOptions#test.oneOf.options.my_custom_oneOf_option")
.build(),
)
assertThat(pruned.protoFile("test_event.proto")!!.toSchema())
.isEqualTo(
"""
|// Proto schema formatted by Wire, do not edit.
|// Source: test_event.proto
|
|syntax = "proto3";
|
|package test.oneOf.options.test;
|
|message TestMessage {
| oneof element {
| string one = 1;
| string two = 2;
| }
|}
|
""".trimMargin(),
)
}

@Test
fun retainMap() {
val schema = buildSchema {
Expand Down
Loading