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

Remove support for java #97

Merged
merged 1 commit into from
Jun 12, 2021
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
92 changes: 42 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
[![License](https://img.shields.io/github/license/eikek/sbt-openapi-schema.svg?style=flat-square&color=steelblue)](https://github.com/eikek/sbt-openapi-schema/blob/master/LICENSE.txt)


This is an sbt plugin to generate Scala, Java or Elm code given an
openapi 3.x specification. Unlike other codegen tools, this focuses
only on the `#/components/schema` section. Also, it generates
immutable classes and optionally the corresponding JSON conversions.
This is an sbt plugin to generate Scala or Elm code given an openapi
3.x specification. Unlike other codegen tools, this focuses only on
the `#/components/schema` section. Also, it generates immutable
classes and optionally the corresponding JSON conversions.

- Scala: `case class`es are generated and JSON conversion via circe.
- Java: immutable classes (with builder pattern) are generated and
JSON conversion via jackson (requires jackson > 2.9).
- Elm: (experimental) records are generated and constructors for
"empty" values. It works only for objects. JSON conversion is
generated using Elm's default encoding support and the
- Elm: records are generated and constructors for "empty" values. It
works only for objects. JSON conversion is generated using Elm's
default encoding support and the
[json-decode-pipeline](https://github.com/NoRedInk/elm-json-decode-pipeline)
module for decoding.
- JSON support is optional.
Expand Down Expand Up @@ -63,27 +61,25 @@ from the `compile` task.
## Configuration

The configuration is specific to the target language. There exists a
separate configuration object for Java, Scala and Elm.
separate configuration object for Scala and Elm.

The key `openapiJavaConfig` and `openapiScalaConfig` define some
configuration to customize the code generation.
The key `openapiScalaConfig` defines some configuration to customize
the code generation.

For Java, it looks like this:
For Scala, it looks like this:
```scala
case class JavaConfig(mapping: CustomMapping = CustomMapping.none
, json: JavaJson = JavaJson.none
, builderParents: List[Superclass] = Nil) {
case class ScalaConfig(
mapping: CustomMapping = CustomMapping.none,
json: ScalaJson = ScalaJson.none
) {

def withJson(json: JavaJson): JavaConfig =
def withJson(json: ScalaJson): ScalaConfig =
copy(json = json)

def addBuilderParent(sc: Superclass): JavaConfig =
copy(builderParents = sc :: builderParents)

def addMapping(cm: CustomMapping): JavaConfig =
def addMapping(cm: CustomMapping): ScalaConfig =
copy(mapping = mapping.andThen(cm))

def setMapping(cm: CustomMapping): JavaConfig =
def setMapping(cm: CustomMapping): ScalaConfig =
copy(mapping = cm)
}
```
Expand All @@ -92,12 +88,13 @@ By default, no JSON support is added to the generated classes. This
can be changed via:

```
openapiJavaConfig := JavaConfig.default.withJson(JavaJson.jackson)
openapiScalaConfig := ScalaConfig().withJson(ScalaJson.circeSemiauto)
```

This generates the required annotations for jackson. Note, that this
plugin doesn't change your `libraryDependencies` setting. So you need
to add the jackson dependency yourself.
This generates the encoder and decoder using
[circe](https://github.com/circe/circe). Note, that this plugin
doesn't change your `libraryDependencies` setting. So you need to add
the circe dependencies yourself.

The `CustomMapping` class allows to change the class names or use
different types (for example, you might want to change `LocalDate` to
Expand Down Expand Up @@ -129,22 +126,27 @@ file. Here is a `build.sbt` example snippet:
```scala
import com.github.eikek.sbt.openapi._

val CirceVersion = "0.14.1"
libraryDependencies ++= Seq(
"com.fasterxml.jackson.core" % "jackson-databind" % "2.9.8",
"com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % "2.9.8"
"io.circe" %% "circe-generic" % CirceVersion,
"io.circe" %% "circe-parser" % CirceVersion
)

openapiSpec := (Compile/resourceDirectory).value/"test.yml"
openapiTargetLanguage := Language.Java
Compile/openapiJavaConfig := JavaConfig.default.
withJson(JavaJson.jackson).
addMapping(CustomMapping.forName({ case s => s + "Dto" }))
openapiTargetLanguage := Language.Scala
Compile/openapiScalaConfig := ScalaConfig()
.withJson(ScalaJson.circeSemiauto)
.addMapping(CustomMapping.forType({ case TypeDef("LocalDateTime", _) =>
TypeDef("Timestamp", Imports("com.mypackage.Timestamp"))
}))
.addMapping(CustomMapping.forName({ case s => s + "Dto" }))

enablePlugins(OpenApiSchema)
```

It adds jackson JSON support and changes the name of all classes by
appending the suffix "Dto".
It adds circe JSON support and changes the name of all classes by
appending the suffix "Dto". It also changes the type used for local
dates to be `com.mypackage.Timestamp`.


## Elm
Expand Down Expand Up @@ -193,12 +195,12 @@ list along with the main source dir. It may look something like this:

It always generates type aliases for records.

While source files for scala and java are added to sbt's
`sourceGenerators` so that they get compiled with your sources, the
elm source files are not added anywhere, because there is no support
for Elm in sbt. However, in the `build.sbt` file, you can tell sbt to
generate the files before compiling your elm app. This can be
configured to run during resource generation. Example:
While source files for scala are added to sbt's `sourceGenerators` so
that they get compiled with your sources, the elm source files are not
added anywhere, because there is no support for Elm in sbt. However,
in the `build.sbt` file, you can tell sbt to generate the files before
compiling your elm app. This can be configured to run during resource
generation. Example:

``` scala

Expand Down Expand Up @@ -461,16 +463,6 @@ lint`](https://redoc.ly/docs/cli/) against your specification file.
This also requires to have nodejs installed.


## TODOs

- support validation
- openapi has several validation definitions
- (java) support bean validation
- convert markdown description into html
- add unit tests
- add more integration tests
- see [Testing SBT Plugins](https://www.scala-sbt.org/1.x/docs/Testing-sbt-plugins.html)
- see `plugin/src/sbt-test/*`

## Credits

Expand Down

This file was deleted.

68 changes: 0 additions & 68 deletions plugin/src/main/scala/com/github/eikek/sbt/openapi/JavaJson.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ object OpenApiSchema extends AutoPlugin {
def extension: String = productPrefix.toLowerCase
}
object Language {
case object Java extends Language
case object Scala extends Language
case object Elm extends Language
}
Expand All @@ -28,13 +27,11 @@ object OpenApiSchema extends AutoPlugin {

val openapiSpec = settingKey[File]("The openapi specification")
val openapiPackage = settingKey[Pkg]("The package to place the generated files into")
val openapiJavaConfig =
settingKey[JavaConfig]("Configuration for generating java files")
val openapiScalaConfig =
settingKey[ScalaConfig]("Configuration for generating scala files")
val openapiElmConfig = settingKey[ElmConfig]("Configuration for generating elm files")
val openapiTargetLanguage =
settingKey[Language]("The target language: either Language.Scala or Language.Java.")
settingKey[Language]("The target language: either Language.Scala or Language.Elm.")
val openapiOutput = settingKey[File]("The directory where files are generated")
val openapiCodegen = taskKey[Seq[File]]("Run the code generation")
val openapiStaticDoc = taskKey[File]("Generate a static HTML documentation")
Expand All @@ -52,7 +49,6 @@ object OpenApiSchema extends AutoPlugin {

val defaultSettings = Seq(
openapiPackage := Pkg("org.myapi"),
openapiJavaConfig := JavaConfig(),
openapiScalaConfig := ScalaConfig(),
openapiElmConfig := ElmConfig(),
openapiOutput := {
Expand All @@ -64,13 +60,12 @@ object OpenApiSchema extends AutoPlugin {
openapiCodegen := {
val out = openapiOutput.value
val logger = streams.value.log
val cfgJava = openapiJavaConfig.value
val cfgScala = openapiScalaConfig.value
val cfgElm = openapiElmConfig.value
val spec = openapiSpec.value
val pkg = openapiPackage.value
val lang = openapiTargetLanguage.value
generateCode(logger, out, lang, cfgJava, cfgScala, cfgElm, spec, pkg)
generateCode(logger, out, lang, cfgScala, cfgElm, spec, pkg)
},
openapiStaticOut := (Compile / resourceManaged).value / "openapiDoc",
openapiStaticGen := OpenApiDocGenerator.Swagger,
Expand Down Expand Up @@ -100,7 +95,6 @@ object OpenApiSchema extends AutoPlugin {
logger: Logger,
out: File,
lang: Language,
cfgJava: JavaConfig,
cfgScala: ScalaConfig,
cfgElm: ElmConfig,
spec: File,
Expand All @@ -115,11 +109,9 @@ object OpenApiSchema extends AutoPlugin {

val files = groupedSchemas.map { sc =>
val (name, code) = (lang, sc) match {
case (Language.Scala, _) => ScalaCode.generate(sc, pkg, cfgScala)
case (Language.Java, _: SingularSchemaClass) =>
JavaCode.generate(sc, pkg, cfgJava)
case (Language.Scala, _) => ScalaCode.generate(sc, pkg, cfgScala)
case (Language.Elm, _: SingularSchemaClass) => ElmCode.generate(sc, pkg, cfgElm)
case _ => sys.error(s"Java and Elm not yet supported for discriminants")
case _ => sys.error(s"Elm not yet supported for discriminants")
}
val file = targetPath / (name + "." + lang.extension)
if (!file.exists || IO.read(file) != code) {
Expand Down
Loading