Skip to content

Commit aeda643

Browse files
committed
merged from develop_2.0
2 parents 279a695 + e9ac84a commit aeda643

File tree

187 files changed

+11582
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+11582
-9
lines changed

LICENSE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright 2015 Reverb Technologies, Inc.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
6+
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.

modules/swagger-core/src/main/resources/logback.xml renamed to modules/swagger-core/src/test/resources/logback-test.xml

File renamed without changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package converter
2+
3+
import com.wordnik.swagger.core.SwaggerSpec
4+
import com.wordnik.swagger.core.util.ModelUtil
5+
import models._
6+
import com.wordnik.swagger.model._
7+
import com.wordnik.swagger.converter._
8+
import org.json4s._
9+
import org.json4s.jackson.Serialization.write
10+
import org.json4s.jackson._
11+
12+
import org.junit.runner.RunWith
13+
import org.scalatest.junit.JUnitRunner
14+
import org.scalatest.FlatSpec
15+
import org.scalatest.Matchers
16+
17+
@RunWith(classOf[JUnitRunner])
18+
class BoxedTypesTest extends FlatSpec with Matchers {
19+
implicit val formats = SwaggerSerializers.formats
20+
21+
"ModelConverters" should "format a BoxedType" in {
22+
val model = ModelConverters.read(classOf[BoxedTypesIssue31]).getOrElse(fail("no model found"))
23+
model.properties.size should be(5)
24+
write(model) should be( """{"id":"BoxedTypesIssue31","description":"Options of boxed types produces an Object ref instead of correct type","properties":{"stringSeq":{"type":"array","items":{"type":"string"}},"stringOpt":{"type":"string"},"intSeq":{"type":"array","description":"Integers in a Sequence Box","items":{"$ref":"Object"}},"intOpt":{"$ref":"Object","description":"Integer in an Option Box"},"justInt":{"type":"integer","format":"int32"}}}""")
25+
}
26+
27+
it should "format a BoxedTypeWithDataType provided in the annotation for the boxed object types" in {
28+
val model = ModelConverters.read(classOf[BoxedTypesIssue31WithDataType]).getOrElse(fail("no model found"))
29+
model.properties.size should be(5)
30+
write(model) should be( """{"id":"BoxedTypesIssue31WithDataType","description":"Options of boxed types produces an Object ref instead of correct type, but can be overcome with dataType","properties":{"stringSeq":{"type":"array","items":{"type":"string"}},"stringOpt":{"type":"string"},"intSeq":{"type":"array","description":"Integers in a Sequence Box","items":{"type":"integer","format":"int32"}},"intOpt":{"type":"integer","format":"int32","description":"Integer in an Option Box"},"justInt":{"type":"integer","format":"int32"}}}""")
31+
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package converter
2+
3+
import org.junit.runner.RunWith
4+
import org.scalatest.junit.JUnitRunner
5+
import org.scalatest.FlatSpec
6+
import org.scalatest.Matchers
7+
8+
@RunWith(classOf[JUnitRunner])
9+
class ModelUtilTest extends FlatSpec with Matchers {
10+
import com.wordnik.swagger.core.util.ModelUtil._
11+
12+
"ModelUtil cleanDataType" should "convert a fully-qualified primitive type to a SwaggerTypes primitive" in {
13+
val primitiveName = "java.lang.Integer"
14+
val cleanName = cleanDataType(primitiveName)
15+
cleanName should equal ("int")
16+
}
17+
18+
it should "convert a primitive type simple name to a SwaggerTypes primitive" in {
19+
val primitiveName = "Integer"
20+
val cleanName = cleanDataType(primitiveName)
21+
cleanName should equal ("int")
22+
}
23+
24+
it should "convert the inner class of a container to a SwaggerTypes primitive" in {
25+
val origName = "List[java.lang.Integer]"
26+
val cleanName = cleanDataType(origName)
27+
cleanName should equal ("List[int]")
28+
}
29+
30+
it should "return a fully-qualified class name unchanged" in {
31+
val fqcn = "com.wordnik.swagger.core.ModelUtil"
32+
val cleanName = cleanDataType(fqcn)
33+
cleanName should equal (fqcn)
34+
}
35+
36+
it should "return a container with a fully-qualified inner class name unchanged" in {
37+
val fqcn = "List[com.wordnik.swagger.core.ModelUtil]"
38+
val cleanName = cleanDataType(fqcn)
39+
cleanName should equal (fqcn)
40+
}
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package converter
2+
3+
import models._
4+
5+
import com.wordnik.swagger.converter._
6+
7+
import com.wordnik.swagger.core.util._
8+
import org.scalatest.{Matchers, FlatSpec}
9+
10+
class ModelWithOptionalFieldsTest extends FlatSpec with Matchers {
11+
val models = ModelConverters.readAll(classOf[ModelWithOptionalFields])
12+
JsonSerializer.asJson(models) should be ("""[{"id":"ModelWithOptionalFields","properties":{"string":{"type":"string"},"integer":{"type":"integer","format":"int32"}}}]""")
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package converter
2+
3+
import models._
4+
5+
import com.wordnik.swagger.converter._
6+
import com.wordnik.swagger.core.util._
7+
import com.wordnik.swagger.model._
8+
import com.wordnik.swagger.annotations._
9+
10+
import org.junit.runner.RunWith
11+
import org.scalatest.junit.JUnitRunner
12+
import org.scalatest.FlatSpec
13+
import org.scalatest.Matchers
14+
15+
@RunWith(classOf[JUnitRunner])
16+
class RequiredFieldModelTest extends FlatSpec with Matchers {
17+
it should "apply read only flag when ApiProperty annotation first" in {
18+
val model = ModelConverters.read(classOf[ApiFirstRequiredFieldModel]).getOrElse(fail("no model found"))
19+
val prop = model.properties.getOrElse("a", fail("didn't find property a"))
20+
prop.required should be (true)
21+
}
22+
23+
it should "apply read only flag when XmlElement annotation first" in {
24+
val model = ModelConverters.read(classOf[XmlFirstRequiredFieldModel]).getOrElse(fail("no model found"))
25+
val prop = model.properties.getOrElse("a", fail("didn't find property a"))
26+
prop.required should be (true)
27+
}
28+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package converter
2+
3+
import models._
4+
5+
import com.wordnik.swagger.core.SwaggerSpec
6+
import com.wordnik.swagger.core.util.ModelUtil
7+
import com.wordnik.swagger.model._
8+
import com.wordnik.swagger.converter._
9+
10+
import org.json4s._
11+
import org.json4s.jackson.Serialization.write
12+
import org.json4s.jackson._
13+
14+
import org.junit.runner.RunWith
15+
import org.scalatest.junit.JUnitRunner
16+
import org.scalatest.FlatSpec
17+
import org.scalatest.Matchers
18+
19+
@RunWith(classOf[JUnitRunner])
20+
class ScalaEnumTest extends FlatSpec with Matchers {
21+
implicit val formats = SwaggerSerializers.formats
22+
23+
"ModelConverters" should "format a class with a Scala Enum type specifying allowable enum values" in {
24+
val model = ModelConverters.read(classOf[SModelWithEnum]).getOrElse(fail("no model found"))
25+
model.properties.size should be(2)
26+
val modelStr = write(model)
27+
// Desired behavior is to have the
28+
// "type": "string"
29+
// "enum": ["TALL", "GRANDE", "VENTI"]
30+
modelStr should equal ("""{"id":"SModelWithEnum","description":"Scala model containing an Enumeration Value that is annotated with the dataType of the Enumeration class","properties":{"label":{"type":"string","description":"Textual label"},"orderSize":{"type":"string","description":"Order Size","enum":["GRANDE","TALL","VENTI"]}}}""")
31+
}
32+
33+
it should "represent the Scala Enum Value type as a string without specifying allowable values if dataType is not specified" in {
34+
val model = ModelConverters.read(classOf[SModelWithEnumNoDataType]).getOrElse(fail("no model found"))
35+
model.properties.size should be(2)
36+
val modelStr = write(model)
37+
modelStr should equal ("""{"id":"SModelWithEnumNoDataType","description":"Scala model containing an Enumeration Value that is not annotated with the dataType of the Enumeration class","properties":{"label":{"type":"string","description":"Textual label"},"orderSize":{"type":"string","description":"Order Size"}}}""")
38+
}
39+
40+
it should "handle a Scala Enum Value type as a string without specifying allowable values if the dataType is badly specified" in {
41+
val model = ModelConverters.read(classOf[SModelWithEnumBadDataType]).getOrElse(fail("no model found"))
42+
model.properties.size should be(2)
43+
val modelStr = write(model)
44+
// If the dataType cannot be resolved to an Enumeration class, just gracefully behave as if the dataType is not specified
45+
modelStr should equal ("""{"id":"SModelWithEnumBadDataType","description":"Scala model containing an Enumeration Value that is incorrectly annotated with a bad dataType of the Enumeration class","properties":{"label":{"type":"string","description":"Textual label"},"orderSize":{"type":"string","description":"Order Size"}}}""")
46+
}
47+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package models;
2+
3+
import com.wordnik.swagger.annotations.*;
4+
5+
import javax.xml.bind.annotation.*;
6+
7+
@XmlRootElement
8+
@XmlAccessorType(XmlAccessType.FIELD)
9+
@ApiModel( value = "aaa")
10+
public class ApiFirstRequiredFieldModel {
11+
@ApiModelProperty( value = "bla", required = true )
12+
@XmlElement(name="a")
13+
public String getA() {
14+
return "aaa";
15+
}
16+
17+
public String getC() {
18+
return "kkk";
19+
};
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package models
2+
3+
import com.wordnik.swagger.annotations.{ ApiModel, ApiModelProperty }
4+
5+
import scala.annotation.meta.field
6+
7+
// Issue #31: https://github.com/gettyimages/spray-swagger/issues/31
8+
// It would be nice if the Seq[Int] and Option[Int] could create the proper spec, but due
9+
// to erasure the parameterized types are only identified as Object
10+
@ApiModel(description = "Options of boxed types produces an Object ref instead of correct type")
11+
case class BoxedTypesIssue31(stringSeq: Seq[String], stringOpt: Option[String],
12+
@(ApiModelProperty @field)(value = "Integers in a Sequence Box") intSeq: Seq[Int],
13+
@(ApiModelProperty @field)(value = "Integer in an Option Box") intOpt: Option[Int],
14+
justInt: Int)
15+
16+
// Get around the erasure by providing the dataType explicitly using the dataType common names.
17+
@ApiModel(description = "Options of boxed types produces an Object ref instead of correct type, but can be overcome with dataType")
18+
case class BoxedTypesIssue31WithDataType(stringSeq: Seq[String], stringOpt: Option[String],
19+
@(ApiModelProperty @field)(value = "Integers in a Sequence Box", dataType = "List[int]") intSeq: Seq[Int],
20+
@(ApiModelProperty @field)(value = "Integer in an Option Box", dataType = "int") intOpt: Option[Int],
21+
justInt: Int)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package models;
2+
3+
import com.google.common.base.Optional;
4+
5+
public class ModelWithOptionalFields {
6+
public Optional<String> string;
7+
public Optional<Integer> integer;
8+
}

0 commit comments

Comments
 (0)