Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
merged from develop
Browse files Browse the repository at this point in the history
  • Loading branch information
fehguy committed Jan 20, 2017
2 parents b9ab6b1 + 6c51e88 commit 9edcd11
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 49 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Which will include the proper cross-publish version of swagger-scala-module.
## How does it work?
Including the library in your project allows the swagger extension module to discover this module, bringing in the appropriate jackson library in the process. You can then use scala classes and objects in your swagger project.

## Treatment of `Option` and `required`
All properties, besides those wrapped in `Option` or explicitly set via annotations `@ApiModelProperty(required = false)`, default to `required = true` in the generated swagger model. See [#7](https://github.com/swagger-api/swagger-scala-module/issues/7)

## Support
The following methods are available to obtain support for Swagger:

Expand Down
51 changes: 24 additions & 27 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ organization := "io.swagger"

version := "1.0.3"

// scala 2.12 requires jdk8. JDK8 cannot be used with 2.10 so crossbuilding won't work
scalaVersion := "2.10.4"
scalaVersion := "2.11.8"

crossScalaVersions := Seq("2.10.0", "2.10.1", "2.10.2", "2.10.3", "2.10.4", "2.10.6", "2.11.0", "2.11.1", "2.11.4", "2.11.7")

//scalaVersion := "2.12.0"

//crossScalaVersions := Seq(/*"2.10.0", "2.10.1", "2.10.2", "2.10.3", "2.10.4", "2.10.6", "2.11.0", "2.11.1", "2.11.4", "2.11.7",*/ "2.12.0")
crossScalaVersions := Seq("2.10.6", scalaVersion.value, "2.12.1")

organizationHomepage in ThisBuild := Some(url("http://swagger.io"))

Expand All @@ -34,8 +29,8 @@ libraryDependencies ++= Seq(
"junit" % "junit" % "4.12" % "test"
)

publishTo <<= (version) { version: String =>
if (version.trim.endsWith("SNAPSHOT"))
publishTo := {
if (version.value.trim.endsWith("SNAPSHOT"))
Some("Sonatype Nexus Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots")
else
Some("Sonatype Nexus Releases" at "https://oss.sonatype.org/service/local/staging/deploy/maven2")
Expand Down Expand Up @@ -70,21 +65,23 @@ startYear := Some(2014)

licenses := Seq(("Apache License 2.0", new URL("http://www.apache.org/licenses/LICENSE-2.0.html")))

pomExtra <<= (pomExtra, name, description) {(pom, name, desc) => pom ++ Group(
<scm>
<connection>scm:git:git@github.com:swagger-api/swagger-scala-module.git</connection>
<developerConnection>scm:git:git@github.com:swagger-api/swagger-scala-module.git</developerConnection>
<url>https://github.com/swagger-api/swagger-scala-module</url>
</scm>
<issueManagement>
<system>github</system>
<url>https://github.com/swagger-api/swagger-scala-module/issues</url>
</issueManagement>
<developers>
<developer>
<id>fehguy</id>
<name>Tony Tam</name>
<email>fehguy@gmail.com</email>
</developer>
</developers>
)}
pomExtra := {
pomExtra.value ++ Group(
<scm>
<connection>scm:git:git@github.com:swagger-api/swagger-scala-module.git</connection>
<developerConnection>scm:git:git@github.com:swagger-api/swagger-scala-module.git</developerConnection>
<url>https://github.com/swagger-api/swagger-scala-module</url>
</scm>
<issueManagement>
<system>github</system>
<url>https://github.com/swagger-api/swagger-scala-module/issues</url>
</issueManagement>
<developers>
<developer>
<id>fehguy</id>
<name>Tony Tam</name>
<email>fehguy@gmail.com</email>
</developer>
</developers>
)
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.5
sbt.version=0.13.13
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.lang.annotation.Annotation
import java.lang.reflect.Type
import java.util.Iterator

import com.fasterxml.jackson.databind.`type`.CollectionLikeType
import com.fasterxml.jackson.databind.`type`.ReferenceType
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import io.swagger.converter._
import io.swagger.models.Model
Expand Down Expand Up @@ -32,26 +32,33 @@ class SwaggerScalaModelConverter extends ModelConverter {
val sp = new StringProperty()
for (v <- enumInstance.values)
sp._enum(v.toString)
sp.setRequired(true)
return sp
}
case None =>
if (cls.isAssignableFrom(classOf[BigDecimal])) {
return PrimitiveType.DECIMAL.createProperty()
val dp = PrimitiveType.DECIMAL.createProperty()
dp.setRequired(true)
return dp
}
}
}

// Unbox scala options
val nextType = `type` match {
case clt: CollectionLikeType if isOption(cls) => clt.getContentType
case _ => `type`
}

if (chain.hasNext())
chain.next().resolveProperty(nextType, context, annotations, chain)
else
null
`type` match {
case rt: ReferenceType if isOption(cls) && chain.hasNext => rt.getContentType
val nextType = rt.getContentType
val nextResolved = chain.next().resolveProperty(nextType, context, annotations, chain)
nextResolved.setRequired(false)
nextResolved
case t if chain.hasNext =>
val nextResolved = chain.next().resolveProperty(t, context, annotations, chain)
nextResolved.setRequired(true)
nextResolved
case _ =>
null
}
}

override
def resolve(`type`: Type, context: ModelConverterContext, chain: Iterator[ModelConverter]): Model = {
Expand Down
36 changes: 27 additions & 9 deletions src/test/scala/ModelPropertyParserTest.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import io.swagger.converter._
import io.swagger.models.properties

import models._

import io.swagger.util.Json
import io.swagger.models.properties._

import scala.collection.JavaConverters._

import models._
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalatest.{FlatSpec, Matchers}

import scala.collection.JavaConverters._

@RunWith(classOf[JUnitRunner])
class ModelPropertyParserTest extends FlatSpec with Matchers {
Expand Down Expand Up @@ -59,5 +54,28 @@ class ModelPropertyParserTest extends FlatSpec with Matchers {
model should be ('defined)
val modelOpt = model.get.getProperties().get("field")
modelOpt shouldBe a [properties.DecimalProperty]
modelOpt.getRequired should be (true)
}

it should "process all properties as required barring Option[_] or if overridden in annotation" in {
val schemas = ModelConverters
.getInstance()
.readAll(classOf[ModelWithOptionAndNonOption])
.asScala

val model = schemas("ModelWithOptionAndNonOption")
model should not be (null)

val optional = model.getProperties().get("optional")
optional.getRequired should be (false)

val required = model.getProperties().get("required")
required.getRequired should be (true)

val forcedRequired = model.getProperties().get("forcedRequired")
forcedRequired.getRequired should be (true)

val forcedOptional = model.getProperties().get("forcedOptional")
forcedOptional.getRequired should be (false)
}
}
9 changes: 8 additions & 1 deletion src/test/scala/models/ModelWOptionString.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ case class ModelWOptionString (
@ApiModel(description = "An Option[Model] is not an array")
case class ModelWOptionModel (
@(ApiModelProperty @field)(value="this is an Option[Model] attribute") modelOpt: Option[ModelWOptionString]
)
)

case class ModelWithOptionAndNonOption(
required: String,
optional: Option[String],
@ApiModelProperty(required = false) forcedOptional: String,
@ApiModelProperty(required = true) forcedRequired: Option[String]
)

0 comments on commit 9edcd11

Please sign in to comment.