Skip to content

Commit 51e0076

Browse files
committed
merged from master
1 parent d9dca92 commit 51e0076

File tree

102 files changed

+15207
-0
lines changed

Some content is hidden

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

102 files changed

+15207
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package converter
2+
3+
import converter.models._
4+
5+
import com.wordnik.swagger.model._
6+
import com.wordnik.swagger.converter._
7+
8+
import org.junit.runner.RunWith
9+
import org.scalatest.junit.JUnitRunner
10+
import org.scalatest.FlatSpec
11+
import org.scalatest.matchers.ShouldMatchers
12+
import converter.models.JCovariantGetter
13+
14+
@RunWith(classOf[JUnitRunner])
15+
class CovariantGetterTest extends FlatSpec with ShouldMatchers {
16+
implicit val formats = SwaggerSerializers.formats
17+
18+
it should "read a getter with covariant return type" in {
19+
val model = ModelConverters.read(classOf[JCovariantGetter.Sub]).getOrElse(fail("no model found"))
20+
val myProperty = model.properties.get("myProperty")
21+
myProperty should not be (None)
22+
myProperty.get.qualifiedType should be ("java.lang.Integer")
23+
val myOtherProperty = model.properties.get("myOtherProperty")
24+
myOtherProperty should not be (None)
25+
myOtherProperty.get.qualifiedType should be ("java.lang.Integer")
26+
}
27+
28+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package converter
2+
3+
import converter.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 java.util.Date
11+
12+
import scala.reflect.BeanProperty
13+
14+
import org.junit.runner.RunWith
15+
import org.scalatest.junit.JUnitRunner
16+
import org.scalatest.FlatSpec
17+
import org.scalatest.matchers.ShouldMatchers
18+
19+
@RunWith(classOf[JUnitRunner])
20+
class HiddenFieldTest extends FlatSpec with ShouldMatchers {
21+
it should "ignore a hidden field" in {
22+
val model = ModelConverters.read(classOf[ModelWithHiddenFields]).getOrElse(fail("no model found"))
23+
model.id should be ("ModelWithHiddenFields")
24+
model.properties.size should be (2)
25+
26+
val idValue = model.properties("id")
27+
idValue.`type` should be ("long")
28+
idValue.required should be (true)
29+
30+
val nameValue = model.properties("name")
31+
nameValue.`type` should be ("string")
32+
nameValue.required should be (true)
33+
}
34+
}
35+
36+
class ModelWithHiddenFields {
37+
@BeanProperty
38+
@ApiModelProperty(required=true)
39+
var id: Long = _
40+
41+
@BeanProperty
42+
@ApiModelProperty(required=true, hidden=false)
43+
var name: String = _
44+
45+
@BeanProperty
46+
@ApiModelProperty(hidden=true)
47+
var password: String = _
48+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package converter
2+
3+
import models._
4+
5+
import com.wordnik.swagger.converter._
6+
7+
import com.wordnik.swagger.core.util._
8+
9+
import java.util.Date
10+
11+
import org.junit.runner.RunWith
12+
import org.scalatest.junit.JUnitRunner
13+
import org.scalatest.FlatSpec
14+
import org.scalatest.matchers.ShouldMatchers
15+
16+
@RunWith(classOf[JUnitRunner])
17+
class JsonPropertyModelTest extends FlatSpec with ShouldMatchers {
18+
val models = ModelConverters.readAll(classOf[ModelWithJsonProperty])
19+
JsonSerializer.asJson(models) should be ("""[{"id":"ModelWithJsonProperty","properties":{"theCount":{"type":"integer","format":"int32"}}}]""")
20+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package converter
2+
3+
import converter.models._
4+
5+
import com.wordnik.swagger.converter._
6+
import com.wordnik.swagger.model._
7+
8+
import com.wordnik.swagger.annotations._
9+
import com.wordnik.swagger.converter._
10+
import com.wordnik.swagger.core.util._
11+
import com.wordnik.swagger.model._
12+
13+
import scala.reflect.BeanProperty
14+
import scala.collection.mutable.LinkedHashMap
15+
16+
import org.junit.runner.RunWith
17+
import org.scalatest.junit.JUnitRunner
18+
import org.scalatest.FlatSpec
19+
import org.scalatest.matchers.ShouldMatchers
20+
21+
import javax.xml.bind.annotation._
22+
23+
@RunWith(classOf[JUnitRunner])
24+
class PropertyAnnotationTest extends FlatSpec with ShouldMatchers {
25+
it should "read annotations on a property" in {
26+
val a = ModelConverters.readAll(classOf[ModelWithAnnotationOnProperty])
27+
JsonSerializer.asJson(a) should be ("""[{"id":"ModelWithAnnotationOnProperty","description":"my annotated model","properties":{"count":{"type":"integer","format":"int32","description":"the count of items"}}}]""")
28+
29+
/*
30+
[
31+
{
32+
"id": "ModelWithAnnotationOnProperty",
33+
"description": "my annotated model",
34+
"properties": {
35+
"count": {
36+
"type": "integer",
37+
"format": "int32",
38+
"description": "the count of items"
39+
}
40+
}
41+
}
42+
]
43+
*/
44+
}
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package converter
2+
3+
import model._
4+
5+
import com.wordnik.swagger.annotations._
6+
import com.wordnik.swagger.converter._
7+
import com.wordnik.swagger.core.util._
8+
import com.wordnik.swagger.model._
9+
10+
import org.joda.time.DateTime
11+
12+
import scala.collection.mutable.LinkedHashMap
13+
import scala.annotation.target.field
14+
import javax.xml.datatype.XMLGregorianCalendar
15+
16+
import org.junit.runner.RunWith
17+
import org.scalatest.junit.JUnitRunner
18+
import org.scalatest.FlatSpec
19+
import org.scalatest.matchers.ShouldMatchers
20+
21+
@RunWith(classOf[JUnitRunner])
22+
class XMLGregorianCalendarTest extends FlatSpec with ShouldMatchers {
23+
it should "read a model with XMLGregorianCalendar" in {
24+
val models = ModelConverters.readAll(classOf[ModelWithCalendar])
25+
models.size should be (1) // don't create a Joda DateTime object
26+
27+
val model = models.head
28+
val nameProperty = model.properties("name")
29+
nameProperty.`type` should be ("string")
30+
nameProperty.position should be (2)
31+
nameProperty.description should be (Some("name of the model"))
32+
33+
val dateTimeProperty = model.properties("createdAt")
34+
dateTimeProperty.`type` should be ("Date")
35+
dateTimeProperty.position should be (1)
36+
dateTimeProperty.required should be (true)
37+
dateTimeProperty.description should be (Some("creation timestamp"))
38+
39+
println(JsonSerializer.asJson(models.head))
40+
}
41+
}
42+
43+
44+
case class ModelWithCalendar (
45+
@(ApiModelProperty @field)(value = "name of the model", position = 2) name: String,
46+
@(ApiModelProperty @field)(value = "creation timestamp", required = true, position = 1) createdAt: XMLGregorianCalendar)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package converter.models;
2+
3+
public abstract class JCovariantGetter {
4+
5+
public Object getMyProperty() {
6+
return "42";
7+
}
8+
9+
public Object getMyOtherProperty() {
10+
return "42";
11+
}
12+
13+
public static class Sub extends JCovariantGetter {
14+
15+
@Override
16+
public Integer getMyProperty() {
17+
return 42;
18+
}
19+
20+
@Override
21+
public Integer getMyOtherProperty() {
22+
return 42;
23+
}
24+
25+
}
26+
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package converter.models;
2+
3+
import com.wordnik.swagger.annotations.*;
4+
5+
@ApiModel(description = "my annotated model")
6+
public class ModelWithAnnotationOnProperty {
7+
@ApiModelProperty(value = "the count of items", position = 1)
8+
private Integer count;
9+
10+
public void setCount(Integer count) {
11+
this.count = count;
12+
}
13+
14+
public Integer getCount() {
15+
return count;
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package converter.models;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
public class ModelWithJsonProperty {
5+
@JsonProperty("theCount")
6+
private Integer count;
7+
8+
public void setCount(Integer count) {
9+
this.count = count;
10+
}
11+
12+
public Integer getCount() {
13+
return count;
14+
}
15+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2013 Wordnik, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.wordnik.swagger.jaxrs.json;
18+
19+
import javax.ws.rs.Produces;
20+
21+
import javax.ws.rs.core.MediaType;
22+
import javax.ws.rs.ext.Provider;
23+
24+
import com.wordnik.swagger.core.util.JsonUtil;
25+
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
26+
import com.fasterxml.jackson.databind.*;
27+
28+
29+
30+
import com.fasterxml.jackson.module.scala.DefaultScalaModule;
31+
32+
import com.fasterxml.jackson.core.JsonGenerator.Feature;
33+
import com.fasterxml.jackson.databind.*;
34+
import com.fasterxml.jackson.annotation.*;
35+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
36+
37+
38+
@Provider
39+
@Produces(MediaType.APPLICATION_JSON)
40+
public class JacksonJsonProvider extends JacksonJaxbJsonProvider {
41+
private static ObjectMapper commonMapper = null;
42+
43+
public JacksonJsonProvider() {
44+
if(commonMapper == null){
45+
ObjectMapper mapper = new ObjectMapper();
46+
47+
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
48+
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
49+
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
50+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
51+
52+
commonMapper = mapper;
53+
}
54+
super.setMapper(commonMapper);
55+
}
56+
57+
58+
}

0 commit comments

Comments
 (0)