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

Commit 611dfbc

Browse files
authored
Merge pull request #37 from swagger-api/develop
Merge from develop
2 parents 9edcd11 + 4d3f3dc commit 611dfbc

File tree

7 files changed

+97
-15
lines changed

7 files changed

+97
-15
lines changed

.travis.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
language: scala
22
jdk:
3+
- oraclejdk7
34
- oraclejdk8
45
scala:
5-
- 2.10.4
6-
- 2.11.4
6+
- 2.10.6
7+
- 2.11.8
8+
- 2.12.1
9+
matrix:
10+
exclude:
11+
- jdk: oraclejdk7
12+
scala: 2.12.1

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Swagger Scala Module
22

33
[![Build Status](https://travis-ci.org/swagger-api/swagger-scala-module.svg?branch=develop)](https://travis-ci.org/swagger-api/swagger-scala-module)
4+
[![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)
45

56
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.
67

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

2021
```
21-
"io.swagger" %% "swagger-scala-module" % "1.0.0",
22+
"io.swagger" %% "swagger-scala-module" % "1.0.3"
2223
```
2324

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

4445

45-
### To build from source (currently 1.0.0)
46+
### To build from source
4647
```
4748
sbt publishLocal
4849
```

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Defaults._
66

77
organization := "io.swagger"
88

9-
version := "1.0.3"
9+
version := "1.0.4"
1010

1111
scalaVersion := "2.11.8"
1212

src/main/scala/io/swagger/scala/converter/SwaggerScalaModelConverter.scala

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class SwaggerScalaModelConverter extends ModelConverter {
1919
SwaggerScalaModelConverter
2020

2121
override
22-
def resolveProperty(`type`: Type, context: ModelConverterContext,
22+
def resolveProperty(`type`: Type, context: ModelConverterContext,
2323
annotations: Array[Annotation] , chain: Iterator[ModelConverter]): Property = {
2424
val javaType = Json.mapper().constructType(`type`)
2525
val cls = javaType.getRawClass
@@ -40,17 +40,32 @@ class SwaggerScalaModelConverter extends ModelConverter {
4040
val dp = PrimitiveType.DECIMAL.createProperty()
4141
dp.setRequired(true)
4242
return dp
43+
} else if (cls.isAssignableFrom(classOf[BigInt])) {
44+
val dp = PrimitiveType.INT.createProperty()
45+
dp.setRequired(true)
46+
return dp
4347
}
4448
}
4549
}
4650

4751
// Unbox scala options
4852
`type` match {
49-
case rt: ReferenceType if isOption(cls) && chain.hasNext => rt.getContentType
53+
case rt: ReferenceType if isOption(cls) =>
5054
val nextType = rt.getContentType
51-
val nextResolved = chain.next().resolveProperty(nextType, context, annotations, chain)
52-
nextResolved.setRequired(false)
53-
nextResolved
55+
val nextResolved = {
56+
Option(resolveProperty(nextType, context, annotations, chain)) match {
57+
case Some(p) => Some(p)
58+
case None if chain.hasNext() =>
59+
Option(chain.next().resolveProperty(nextType, context, annotations, chain))
60+
case _ => None
61+
}
62+
}
63+
nextResolved match {
64+
case Some(nextResolved) =>
65+
nextResolved.setRequired(false)
66+
nextResolved
67+
case None => null
68+
}
5469
case t if chain.hasNext =>
5570
val nextResolved = chain.next().resolveProperty(t, context, annotations, chain)
5671
nextResolved.setRequired(true)
@@ -64,7 +79,7 @@ class SwaggerScalaModelConverter extends ModelConverter {
6479
def resolve(`type`: Type, context: ModelConverterContext, chain: Iterator[ModelConverter]): Model = {
6580
val javaType = Json.mapper().constructType(`type`)
6681
getEnumerationInstance(javaType.getRawClass) match {
67-
case Some(enumInstance) =>null // ignore scala enums
82+
case Some(enumInstance) => null // ignore scala enums
6883
case None =>
6984
if (chain.hasNext()) {
7085
val next = chain.next()

src/test/scala/ModelPropertyParserTest.scala

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import io.swagger.converter._
2+
import io.swagger.models.Model
23
import io.swagger.models.properties
34
import io.swagger.models.properties._
45
import models._
@@ -46,17 +47,51 @@ class ModelPropertyParserTest extends FlatSpec with Matchers {
4647
}
4748

4849
it should "process Model with Scala BigDecimal as Number" in {
49-
case class TestModel(field: BigDecimal)
50+
case class TestModelWithBigDecimal(field: BigDecimal)
5051

5152
val converter = ModelConverters.getInstance()
52-
val schemas = converter.readAll(classOf[TestModel]).asScala.toMap
53-
val model = schemas.values.headOption
53+
val schemas = converter.readAll(classOf[TestModelWithBigDecimal]).asScala.toMap
54+
val model = findModel(schemas, "TestModelWithBigDecimal")
5455
model should be ('defined)
5556
val modelOpt = model.get.getProperties().get("field")
5657
modelOpt shouldBe a [properties.DecimalProperty]
5758
modelOpt.getRequired should be (true)
5859
}
5960

61+
it should "process Model with Scala BigInt as Number" in {
62+
case class TestModelWithBigInt(field: BigInt)
63+
64+
val converter = ModelConverters.getInstance()
65+
val schemas = converter.readAll(classOf[TestModelWithBigInt]).asScala.toMap
66+
val model = findModel(schemas, "TestModelWithBigInt")
67+
model should be ('defined)
68+
val modelOpt = model.get.getProperties().get("field")
69+
modelOpt shouldBe a [properties.BaseIntegerProperty]
70+
modelOpt.getRequired should be (true)
71+
}
72+
73+
it should "process Model with Scala Option BigDecimal" in {
74+
val converter = ModelConverters.getInstance()
75+
val schemas = converter.readAll(classOf[ModelWOptionBigDecimal]).asScala.toMap
76+
val model = schemas.get("ModelWOptionBigDecimal")
77+
model should be ('defined)
78+
val optBigDecimal = model.get.getProperties().get("optBigDecimal")
79+
optBigDecimal should not be (null)
80+
optBigDecimal shouldBe a [properties.DecimalProperty]
81+
optBigDecimal.getRequired should be (false)
82+
}
83+
84+
it should "process Model with Scala Option BigInt" in {
85+
val converter = ModelConverters.getInstance()
86+
val schemas = converter.readAll(classOf[ModelWOptionBigInt]).asScala.toMap
87+
val model = schemas.get("ModelWOptionBigInt")
88+
model should be ('defined)
89+
val optBigDecimal = model.get.getProperties().get("optBigInt")
90+
optBigDecimal should not be (null)
91+
optBigDecimal shouldBe a [properties.BaseIntegerProperty]
92+
optBigDecimal.getRequired should be (false)
93+
}
94+
6095
it should "process all properties as required barring Option[_] or if overridden in annotation" in {
6196
val schemas = ModelConverters
6297
.getInstance()
@@ -78,4 +113,15 @@ class ModelPropertyParserTest extends FlatSpec with Matchers {
78113
val forcedOptional = model.getProperties().get("forcedOptional")
79114
forcedOptional.getRequired should be (false)
80115
}
81-
}
116+
117+
def findModel(schemas: Map[String, Model], name: String): Option[Model] = {
118+
schemas.get(name) match {
119+
case Some(m) => Some(m)
120+
case None =>
121+
schemas.keys.find { case k => k.startsWith(name) } match {
122+
case Some(key) => schemas.get(key)
123+
case None => schemas.values.headOption
124+
}
125+
}
126+
}
127+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package models
2+
3+
import io.swagger.annotations.ApiModelProperty
4+
import scala.annotation.meta.field
5+
6+
case class ModelWOptionBigDecimal(
7+
@(ApiModelProperty @field)(value="this is an Option[BigDecimal] attribute") optBigDecimal: Option[BigDecimal])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package models
2+
3+
import io.swagger.annotations.ApiModelProperty
4+
import scala.annotation.meta.field
5+
6+
case class ModelWOptionBigInt(
7+
@(ApiModelProperty @field)(value="this is an Option[BigInt] attribute") optBigInt: Option[BigInt])

0 commit comments

Comments
 (0)