Skip to content

Commit

Permalink
Don't remove 'val' from case classes' parameters with names (#2634)
Browse files Browse the repository at this point in the history
* Fix codegen for case classes with field named value

* Add test
  • Loading branch information
ekhov authored Jan 20, 2024
1 parent 31dfc58 commit 842af96
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ object CodeGen {

case Code.CaseClass(name, fields, companionObject) =>
s"case class $name(\n" +
fields.map(render(basePackage)).mkString(",\n").replace("val", "") +
fields.map(render(basePackage)).mkString(",\n").replace("val ", " ") +
"\n)" + companionObject.map(render(basePackage)).map("\n" + _).getOrElse("")

case Code.Enum(name, cases, caseNames, discriminator, noDiscriminator, schema) =>
Expand Down
13 changes: 13 additions & 0 deletions zio-http-gen/src/test/resources/GeneratedValues.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package test.component

import zio.schema._

case class Values(
value1: Int,
value2: String,
)
object Values {

implicit val codec: Schema[Values] = DeriveSchema.gen[Values]

}
8 changes: 8 additions & 0 deletions zio-http-gen/src/test/scala/zio/http/gen/model/Values.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package zio.http.gen.model

import zio.schema.{DeriveSchema, Schema}

case class Values(value1: Int, value2: String)
object Values {
implicit val codec: Schema[Values] = DeriveSchema.gen[Values]
}
14 changes: 14 additions & 0 deletions zio-http-gen/src/test/scala/zio/http/gen/scala/CodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,19 @@ object CodeGenSpec extends ZIOSpecDefault {
"/GeneratedPaymentNoDiscriminator.scala",
)
},
test("Endpoint with case class with field named 'value'") {
val endpoint = Endpoint(Method.POST / "values").out[Values]
val openAPI = OpenAPIGen.fromEndpoints(endpoint)
val code = EndpointGen.fromOpenAPI(openAPI)

val tempDir = Files.createTempDirectory("codegen")
CodeGen.writeFiles(code, java.nio.file.Paths.get(tempDir.toString, "test"), "test", Some(scalaFmtPath))

fileShouldBe(
tempDir,
"test/component/Values.scala",
"/GeneratedValues.scala",
)
},
) @@ java11OrNewer @@ flaky // Downloading scalafmt on CI is flaky
}

0 comments on commit 842af96

Please sign in to comment.