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

Commit

Permalink
Merge pull request #37 from swagger-api/develop
Browse files Browse the repository at this point in the history
Merge from develop
  • Loading branch information
fehguy authored Jun 7, 2017
2 parents 9edcd11 + 4d3f3dc commit 611dfbc
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 15 deletions.
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
language: scala
jdk:
- oraclejdk7
- oraclejdk8
scala:
- 2.10.4
- 2.11.4
- 2.10.6
- 2.11.8
- 2.12.1
matrix:
exclude:
- jdk: oraclejdk7
scala: 2.12.1
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Swagger Scala Module

[![Build Status](https://travis-ci.org/swagger-api/swagger-scala-module.svg?branch=develop)](https://travis-ci.org/swagger-api/swagger-scala-module)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.swagger/swagger-scala-module_2.11/badge.svg?style=plastic)](https://maven-badges.herokuapp.com/maven-central/io.swagger/swagger-scala-module_2.11)

The goal of Swagger™ is to define a standard, language-agnostic interface to REST APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined via Swagger, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Similar to what interfaces have done for lower-level programming, Swagger removes the guesswork in calling the service.

Expand All @@ -18,7 +19,7 @@ This project is compatible with [swagger-core](https://github.com/swagger-api/sw
To enable the swagger-scala-module, include the appropriate version in your project:

```
"io.swagger" %% "swagger-scala-module" % "1.0.0",
"io.swagger" %% "swagger-scala-module" % "1.0.3"
```

Which will include the proper cross-publish version of swagger-scala-module.
Expand All @@ -42,7 +43,7 @@ The following methods are available to obtain support for Swagger:
See the guide on [getting started with swagger](http://swagger.io) to get started with adding swagger to your API.


### To build from source (currently 1.0.0)
### To build from source
```
sbt publishLocal
```
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Defaults._

organization := "io.swagger"

version := "1.0.3"
version := "1.0.4"

scalaVersion := "2.11.8"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SwaggerScalaModelConverter extends ModelConverter {
SwaggerScalaModelConverter

override
def resolveProperty(`type`: Type, context: ModelConverterContext,
def resolveProperty(`type`: Type, context: ModelConverterContext,
annotations: Array[Annotation] , chain: Iterator[ModelConverter]): Property = {
val javaType = Json.mapper().constructType(`type`)
val cls = javaType.getRawClass
Expand All @@ -40,17 +40,32 @@ class SwaggerScalaModelConverter extends ModelConverter {
val dp = PrimitiveType.DECIMAL.createProperty()
dp.setRequired(true)
return dp
} else if (cls.isAssignableFrom(classOf[BigInt])) {
val dp = PrimitiveType.INT.createProperty()
dp.setRequired(true)
return dp
}
}
}

// Unbox scala options
`type` match {
case rt: ReferenceType if isOption(cls) && chain.hasNext => rt.getContentType
case rt: ReferenceType if isOption(cls) =>
val nextType = rt.getContentType
val nextResolved = chain.next().resolveProperty(nextType, context, annotations, chain)
nextResolved.setRequired(false)
nextResolved
val nextResolved = {
Option(resolveProperty(nextType, context, annotations, chain)) match {
case Some(p) => Some(p)
case None if chain.hasNext() =>
Option(chain.next().resolveProperty(nextType, context, annotations, chain))
case _ => None
}
}
nextResolved match {
case Some(nextResolved) =>
nextResolved.setRequired(false)
nextResolved
case None => null
}
case t if chain.hasNext =>
val nextResolved = chain.next().resolveProperty(t, context, annotations, chain)
nextResolved.setRequired(true)
Expand All @@ -64,7 +79,7 @@ class SwaggerScalaModelConverter extends ModelConverter {
def resolve(`type`: Type, context: ModelConverterContext, chain: Iterator[ModelConverter]): Model = {
val javaType = Json.mapper().constructType(`type`)
getEnumerationInstance(javaType.getRawClass) match {
case Some(enumInstance) =>null // ignore scala enums
case Some(enumInstance) => null // ignore scala enums
case None =>
if (chain.hasNext()) {
val next = chain.next()
Expand Down
54 changes: 50 additions & 4 deletions src/test/scala/ModelPropertyParserTest.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io.swagger.converter._
import io.swagger.models.Model
import io.swagger.models.properties
import io.swagger.models.properties._
import models._
Expand Down Expand Up @@ -46,17 +47,51 @@ class ModelPropertyParserTest extends FlatSpec with Matchers {
}

it should "process Model with Scala BigDecimal as Number" in {
case class TestModel(field: BigDecimal)
case class TestModelWithBigDecimal(field: BigDecimal)

val converter = ModelConverters.getInstance()
val schemas = converter.readAll(classOf[TestModel]).asScala.toMap
val model = schemas.values.headOption
val schemas = converter.readAll(classOf[TestModelWithBigDecimal]).asScala.toMap
val model = findModel(schemas, "TestModelWithBigDecimal")
model should be ('defined)
val modelOpt = model.get.getProperties().get("field")
modelOpt shouldBe a [properties.DecimalProperty]
modelOpt.getRequired should be (true)
}

it should "process Model with Scala BigInt as Number" in {
case class TestModelWithBigInt(field: BigInt)

val converter = ModelConverters.getInstance()
val schemas = converter.readAll(classOf[TestModelWithBigInt]).asScala.toMap
val model = findModel(schemas, "TestModelWithBigInt")
model should be ('defined)
val modelOpt = model.get.getProperties().get("field")
modelOpt shouldBe a [properties.BaseIntegerProperty]
modelOpt.getRequired should be (true)
}

it should "process Model with Scala Option BigDecimal" in {
val converter = ModelConverters.getInstance()
val schemas = converter.readAll(classOf[ModelWOptionBigDecimal]).asScala.toMap
val model = schemas.get("ModelWOptionBigDecimal")
model should be ('defined)
val optBigDecimal = model.get.getProperties().get("optBigDecimal")
optBigDecimal should not be (null)
optBigDecimal shouldBe a [properties.DecimalProperty]
optBigDecimal.getRequired should be (false)
}

it should "process Model with Scala Option BigInt" in {
val converter = ModelConverters.getInstance()
val schemas = converter.readAll(classOf[ModelWOptionBigInt]).asScala.toMap
val model = schemas.get("ModelWOptionBigInt")
model should be ('defined)
val optBigDecimal = model.get.getProperties().get("optBigInt")
optBigDecimal should not be (null)
optBigDecimal shouldBe a [properties.BaseIntegerProperty]
optBigDecimal.getRequired should be (false)
}

it should "process all properties as required barring Option[_] or if overridden in annotation" in {
val schemas = ModelConverters
.getInstance()
Expand All @@ -78,4 +113,15 @@ class ModelPropertyParserTest extends FlatSpec with Matchers {
val forcedOptional = model.getProperties().get("forcedOptional")
forcedOptional.getRequired should be (false)
}
}

def findModel(schemas: Map[String, Model], name: String): Option[Model] = {
schemas.get(name) match {
case Some(m) => Some(m)
case None =>
schemas.keys.find { case k => k.startsWith(name) } match {
case Some(key) => schemas.get(key)
case None => schemas.values.headOption
}
}
}
}
7 changes: 7 additions & 0 deletions src/test/scala/models/ModelWOptionBigDecimal.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package models

import io.swagger.annotations.ApiModelProperty
import scala.annotation.meta.field

case class ModelWOptionBigDecimal(
@(ApiModelProperty @field)(value="this is an Option[BigDecimal] attribute") optBigDecimal: Option[BigDecimal])
7 changes: 7 additions & 0 deletions src/test/scala/models/ModelWOptionBigInt.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package models

import io.swagger.annotations.ApiModelProperty
import scala.annotation.meta.field

case class ModelWOptionBigInt(
@(ApiModelProperty @field)(value="this is an Option[BigInt] attribute") optBigInt: Option[BigInt])

0 comments on commit 611dfbc

Please sign in to comment.