Skip to content

Commit

Permalink
Add validation that oneofs don't contain no_box message fields.
Browse files Browse the repository at this point in the history
See #858
  • Loading branch information
thesamet committed Jun 22, 2020
1 parent 0680d41 commit 2c16d1d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class ProtoValidation(implicits: DescriptorImplicits) {
s"${fd.getFullName}: Sealed oneofs can not be type mapped. Use regular oneofs instead."
)
}
if (fd.isMessage && fd.isInOneof && fd.getMessageType().messageOptions.getNoBox()) {
throw new GeneratorException(
s"${fd.getFullName}: message fields in oneofs are not allowed to have no_box set."
)
}
}
}

Expand Down
18 changes: 9 additions & 9 deletions compiler-plugin/src/main/scala/scalapb/gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import scalapb.GeneratorOption._

object gen {
val SandboxedGenerator = SandboxedJvmGenerator.forModule(
"scala",
Artifact(
"com.thesamet.scalapb",
"compilerplugin_2.12",
scalapb.compiler.Version.scalapbVersion
),
"scalapb.ScalaPbCodeGenerator$",
scalapb.ScalaPbCodeGenerator.suggestedDependencies
)
"scala",
Artifact(
"com.thesamet.scalapb",
"compilerplugin_2.12",
scalapb.compiler.Version.scalapbVersion
),
"scalapb.ScalaPbCodeGenerator$",
scalapb.ScalaPbCodeGenerator.suggestedDependencies
)

def apply(options: Set[GeneratorOption]): (SandboxedJvmGenerator, Seq[String]) =
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,31 @@ class ProtoValidationSpec extends AnyFlatSpec with Matchers {
"file1.proto: object_name is not allowed in package-scoped options."
)
}

it should "fail when oneof contains a no_box message" in {
intercept[GeneratorException] {
runValidation(
"file1.proto" ->
"""
|syntax = "proto3";
|
|package a.b;
|
|import "scalapb/scalapb.proto";
|
|message NoBox {
| option (scalapb.message).no_box = true;
|}
|
|message Msg {
| oneof foo {
| NoBox field = 1;
| }
|}
""".stripMargin
)
}.message must be(
"a.b.Msg.field: message fields in oneofs are not allowed to have no_box set."
)
}
}

0 comments on commit 2c16d1d

Please sign in to comment.