Skip to content

Commit b45ccc3

Browse files
committed
changed example to objects to address #936
1 parent 6bdede2 commit b45ccc3

File tree

7 files changed

+103
-15
lines changed

7 files changed

+103
-15
lines changed

modules/swagger-core/src/test/scala/ModelSerializerTest.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,31 @@ class ModelSerializerTest extends FlatSpec with Matchers {
6868
val schemas = ModelConverters.getInstance().read(classOf[Manufacturers])
6969
Json.mapper().writeValueAsString(schemas) should be ("""{"Manufacturers":{"properties":{"countries":{"type":"array","uniqueItems":true,"items":{"type":"string"}}}}}""")
7070
}
71+
72+
it should "deserialize a model with object example" in {
73+
val json = """{
74+
"title": "Error",
75+
"type": "object",
76+
"properties": {
77+
"code": {
78+
"type": "integer",
79+
"format": "int32"
80+
},
81+
"message": {
82+
"type": "string"
83+
},
84+
"fields": {
85+
"type": "string"
86+
}
87+
},
88+
"example": {
89+
"code": 1,
90+
"message": "hello",
91+
"fields": "abc"
92+
}
93+
}"""
94+
95+
val model = Json.mapper().readValue(json, classOf[ModelImpl])
96+
Json.mapper().writeValueAsString(model.getExample()) should be ("""{"code":1,"message":"hello","fields":"abc"}""")
97+
}
7198
}

modules/swagger-models/src/main/java/com/wordnik/swagger/models/ArrayModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ArrayModel extends AbstractModel {
99
private String type;
1010
private String description;
1111
private Property items;
12-
private String example;
12+
private Object example;
1313

1414
public ArrayModel () {
1515
this.type = "array";
@@ -52,10 +52,10 @@ public void setProperties(Map<String, Property> properties) {
5252
this.properties = properties;
5353
}
5454

55-
public String getExample() {
55+
public Object getExample() {
5656
return example;
5757
}
58-
public void setExample(String example) {
58+
public void setExample(Object example) {
5959
this.example = example;
6060
}
6161

modules/swagger-models/src/main/java/com/wordnik/swagger/models/ComposedModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ComposedModel extends AbstractModel {
1212
private Model child;
1313
private List<RefModel> interfaces;
1414
private String description;
15-
private String example;
15+
private Object example;
1616

1717
public ComposedModel parent(Model model) {
1818
this.setParent(model);
@@ -41,10 +41,10 @@ public void setProperties(Map<String, Property> properties){
4141

4242
}
4343

44-
public String getExample() {
44+
public Object getExample() {
4545
return example;
4646
}
47-
public void setExample(String example) {
47+
public void setExample(Object example) {
4848
this.example = example;
4949
}
5050

modules/swagger-models/src/main/java/com/wordnik/swagger/models/Model.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public interface Model {
1111
Map<String, Property> getProperties();
1212
void setProperties(Map<String, Property> properties);
1313

14-
String getExample();
15-
void setExample(String example);
14+
Object getExample();
15+
void setExample(Object example);
1616

1717
ExternalDocs getExternalDocs();
1818

modules/swagger-models/src/main/java/com/wordnik/swagger/models/ModelImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ModelImpl extends AbstractModel {
1818
private Map<String, Property> properties;
1919
private boolean isSimple = false;
2020
private String description;
21-
private String example;
21+
private Object example;
2222
private Property additionalProperties;
2323
private String discriminator;
2424
private Xml xml;
@@ -39,7 +39,7 @@ public ModelImpl property(String key, Property property) {
3939
this.addProperty(key, property);
4040
return this;
4141
}
42-
public ModelImpl example(String example) {
42+
public ModelImpl example(Object example) {
4343
this.setExample(example);
4444
return this;
4545
}
@@ -158,15 +158,15 @@ public void setProperties(Map<String, Property> properties) {
158158
}
159159
}
160160

161-
public String getExample() {
161+
public Object getExample() {
162162
if(example == null) {
163163
// TODO: will add logic to construct examples based on payload here
164164
}
165165

166166
return example;
167167
}
168168

169-
public void setExample(String example) {
169+
public void setExample(Object example) {
170170
this.example = example;
171171
}
172172

modules/swagger-models/src/main/java/com/wordnik/swagger/models/RefModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class RefModel implements Model {
1212
private String description;
1313
private ExternalDocs externalDocs;
1414
private Map<String, Property> properties;
15-
private String example;
15+
private Object example;
1616

1717
public RefModel(){}
1818
public RefModel(String ref){
@@ -63,10 +63,10 @@ public String getSimpleRef() {
6363
}
6464

6565
@JsonIgnore
66-
public String getExample() {
66+
public Object getExample() {
6767
return example;
6868
}
69-
public void setExample(String example) {
69+
public void setExample(Object example) {
7070
this.example = example;
7171
}
7272

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright 2015 Reverb Technologies, 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.sample.resource;
18+
19+
import com.wordnik.swagger.sample.exception.ApiException;
20+
import com.wordnik.swagger.sample.exception.BadRequestException;
21+
import com.wordnik.swagger.sample.exception.NotFoundException;
22+
import com.wordnik.swagger.sample.model.ApiResponse;
23+
24+
import javax.ws.rs.ext.*;
25+
import javax.ws.rs.core.Response;
26+
import javax.ws.rs.core.Response.Status;
27+
28+
@Provider
29+
public class SampleExceptionMapper implements ExceptionMapper<Exception> {
30+
public Response toResponse(Exception exception) {
31+
if (exception instanceof javax.ws.rs.WebApplicationException) {
32+
javax.ws.rs.WebApplicationException e = (javax.ws.rs.WebApplicationException) exception;
33+
return Response
34+
.status(e.getResponse().getStatus())
35+
.entity(new ApiResponse(e.getResponse().getStatus(),
36+
exception.getMessage())).build();
37+
} else if (exception instanceof com.fasterxml.jackson.core.JsonParseException) {
38+
return Response.status(400)
39+
.entity(new ApiResponse(400, "bad input")).build();
40+
} else if (exception instanceof NotFoundException) {
41+
return Response
42+
.status(Status.NOT_FOUND)
43+
.entity(new ApiResponse(ApiResponse.ERROR, exception
44+
.getMessage())).build();
45+
} else if (exception instanceof BadRequestException) {
46+
return Response
47+
.status(Status.BAD_REQUEST)
48+
.entity(new ApiResponse(ApiResponse.ERROR, exception
49+
.getMessage())).build();
50+
} else if (exception instanceof ApiException) {
51+
return Response
52+
.status(Status.BAD_REQUEST)
53+
.entity(new ApiResponse(ApiResponse.ERROR, exception
54+
.getMessage())).build();
55+
} else {
56+
return Response.status(500)
57+
.entity(new ApiResponse(500, "something bad happened"))
58+
.build();
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)