-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Codegen: serialize empty arrays and require them (jsoniter fix) (#3655)
- Loading branch information
1 parent
c97a8fd
commit a7abd3b
Showing
39 changed files
with
1,318 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
openapi-codegen/sbt-plugin/src/sbt-test/sbt-openapi-codegen/caching/build.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
openapi-codegen/sbt-plugin/src/sbt-test/sbt-openapi-codegen/caching/test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
> clean | ||
> run | ||
> check | ||
$ copy-file target/scala-2.12/src_managed/main/sbt-openapi-codegen/TapirGeneratedEndpoints.scala target/TapirGeneratedEndpoints.scala | ||
$ copy-file target/scala-2.13/src_managed/main/sbt-openapi-codegen/TapirGeneratedEndpoints.scala target/TapirGeneratedEndpoints.scala | ||
> compile | ||
$ newer target/TapirGeneratedEndpoints.scala target/scala-2.12/src_managed/main/sbt-openapi-codegen/TapirGeneratedEndpoints.scala | ||
$ newer target/TapirGeneratedEndpoints.scala target/scala-2.13/src_managed/main/sbt-openapi-codegen/TapirGeneratedEndpoints.scala |
8 changes: 4 additions & 4 deletions
8
openapi-codegen/sbt-plugin/src/sbt-test/sbt-openapi-codegen/minimal/build.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...degen/sbt-plugin/src/sbt-test/sbt-openapi-codegen/oneOf-json-roundtrip/Expected.scala.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package sttp.tapir.generated | ||
|
||
object TapirGeneratedEndpoints { | ||
|
||
import sttp.tapir._ | ||
import sttp.tapir.model._ | ||
import sttp.tapir.generic.auto._ | ||
import sttp.tapir.json.circe._ | ||
import io.circe.generic.semiauto._ | ||
|
||
import sttp.tapir.generated.TapirGeneratedEndpointsJsonSerdes._ | ||
|
||
sealed trait ADTWithoutDiscriminator | ||
sealed trait ADTWithDiscriminator | ||
sealed trait ADTWithDiscriminatorNoMapping | ||
case class SubtypeWithoutD1 ( | ||
s: String, | ||
i: Option[Int] = None, | ||
a: Seq[String], | ||
absent: Option[String] = None | ||
) extends ADTWithoutDiscriminator | ||
case class SubtypeWithD1 ( | ||
s: String, | ||
i: Option[Int] = None, | ||
d: Option[Double] = None | ||
) extends ADTWithDiscriminator with ADTWithDiscriminatorNoMapping | ||
case class SubtypeWithoutD3 ( | ||
s: String, | ||
i: Option[Int] = None, | ||
d: Option[Double] = None, | ||
absent: Option[String] = None | ||
) extends ADTWithoutDiscriminator | ||
case class SubtypeWithoutD2 ( | ||
a: Seq[String], | ||
absent: Option[String] = None | ||
) extends ADTWithoutDiscriminator | ||
case class SubtypeWithD2 ( | ||
s: String, | ||
a: Option[Seq[String]] = None | ||
) extends ADTWithDiscriminator with ADTWithDiscriminatorNoMapping | ||
|
||
|
||
|
||
lazy val putAdtTest = | ||
endpoint | ||
.put | ||
.in(("adt" / "test")) | ||
.in(jsonBody[ADTWithoutDiscriminator]) | ||
.out(jsonBody[ADTWithoutDiscriminator].description("successful operation")) | ||
|
||
lazy val postAdtTest = | ||
endpoint | ||
.post | ||
.in(("adt" / "test")) | ||
.in(jsonBody[ADTWithDiscriminatorNoMapping]) | ||
.out(jsonBody[ADTWithDiscriminator].description("successful operation")) | ||
|
||
|
||
lazy val generatedEndpoints = List(putAdtTest, postAdtTest) | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
openapi-codegen/sbt-plugin/src/sbt-test/sbt-openapi-codegen/oneOf-json-roundtrip/build.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
lazy val root = (project in file(".")) | ||
.enablePlugins(OpenapiCodegenPlugin) | ||
.settings( | ||
scalaVersion := "2.13.13", | ||
version := "0.1" | ||
) | ||
|
||
libraryDependencies ++= Seq( | ||
"com.softwaremill.sttp.tapir" %% "tapir-json-circe" % "1.10.0", | ||
"com.softwaremill.sttp.tapir" %% "tapir-openapi-docs" % "1.10.0", | ||
"com.softwaremill.sttp.apispec" %% "openapi-circe-yaml" % "0.8.0", | ||
"io.circe" %% "circe-generic" % "0.14.6", | ||
"com.beachape" %% "enumeratum" % "1.7.3", | ||
"com.beachape" %% "enumeratum-circe" % "1.7.3", | ||
"org.scalatest" %% "scalatest" % "3.2.18" % Test, | ||
"com.softwaremill.sttp.tapir" %% "tapir-sttp-stub-server" % "1.10.0" % Test | ||
) | ||
|
||
import scala.io.Source | ||
|
||
TaskKey[Unit]("check") := { | ||
val generatedCode = | ||
Source.fromFile("target/scala-2.13/src_managed/main/sbt-openapi-codegen/TapirGeneratedEndpoints.scala").getLines.mkString("\n") | ||
val expected = Source.fromFile("Expected.scala.txt").getLines.mkString("\n") | ||
val generatedTrimmed = generatedCode.linesIterator.zipWithIndex.filterNot(_._1.forall(_.isWhitespace)).map{ case (a, i) => a.trim -> i }.toSeq | ||
val expectedTrimmed = expected.linesIterator.filterNot(_.forall(_.isWhitespace)).map(_.trim).toSeq | ||
if (generatedTrimmed.size != expectedTrimmed.size) | ||
sys.error(s"expected ${expectedTrimmed.size} non-empty lines, found ${generatedTrimmed.size}") | ||
generatedTrimmed.zip(expectedTrimmed).foreach { case ((a, i), b) => | ||
if (a != b) sys.error(s"Generated code did not match (expected '$b' on line $i, found '$a')") | ||
} | ||
println("Skipping swagger roundtrip for petstore") | ||
() | ||
} |
1 change: 1 addition & 0 deletions
1
...sbt-plugin/src/sbt-test/sbt-openapi-codegen/oneOf-json-roundtrip/project/build.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.9.9 |
11 changes: 11 additions & 0 deletions
11
...egen/sbt-plugin/src/sbt-test/sbt-openapi-codegen/oneOf-json-roundtrip/project/plugins.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
val pluginVersion = System.getProperty("plugin.version") | ||
if (pluginVersion == null) | ||
throw new RuntimeException("""| | ||
| | ||
|The system property 'plugin.version' is not defined. | ||
|Specify this property using the scriptedLaunchOpts -D. | ||
| | ||
|""".stripMargin) | ||
else addSbtPlugin("com.softwaremill.sttp.tapir" % "sbt-openapi-codegen" % pluginVersion) | ||
} |
31 changes: 31 additions & 0 deletions
31
...bt-plugin/src/sbt-test/sbt-openapi-codegen/oneOf-json-roundtrip/src/main/scala/Main.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
object Main extends App { | ||
/* | ||
import sttp.tapir._ | ||
import sttp.tapir.json.circe._ | ||
import io.circe.generic.auto._ | ||
type Limit = Int | ||
type AuthToken = String | ||
case class BooksFromYear(genre: String, year: Int) | ||
case class Book(title: String) | ||
val booksListing: Endpoint[(BooksFromYear, Limit, AuthToken), String, List[Book], Any] = | ||
endpoint | ||
.get | ||
.in(("books" / path[String]("genre") / path[Int]("year")).mapTo[BooksFromYear]) | ||
.in(query[Limit]("limit").description("Maximum number of books to retrieve")) | ||
.in(header[AuthToken]("X-Auth-Token")) | ||
.errorOut(stringBody) | ||
.out(jsonBody[List[Book]]) | ||
*/ | ||
import sttp.apispec.openapi.circe.yaml._ | ||
import sttp.tapir.generated._ | ||
import sttp.tapir.docs.openapi._ | ||
|
||
val docs = OpenAPIDocsInterpreter().toOpenAPI(TapirGeneratedEndpoints.generatedEndpoints, "My Bookshop", "1.0") | ||
|
||
import java.nio.file.{Paths, Files} | ||
import java.nio.charset.StandardCharsets | ||
|
||
Files.write(Paths.get("target/swagger.yaml"), docs.toYaml.getBytes(StandardCharsets.UTF_8)) | ||
} |
Oops, something went wrong.