Skip to content

Commit 855099e

Browse files
committed
swagger module for MVC routes fix jooby-project#130
1 parent 470cd70 commit 855099e

28 files changed

Lines changed: 1477 additions & 41 deletions

File tree

coverage-report/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@
300300
<version>${project.version}</version>
301301
</dependency>
302302

303+
<dependency>
304+
<groupId>org.jooby</groupId>
305+
<artifactId>jooby-swagger</artifactId>
306+
<version>${project.version}</version>
307+
</dependency>
308+
303309
<dependency>
304310
<groupId>org.pac4j</groupId>
305311
<artifactId>pac4j-http</artifactId>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.jooby.swagger;
2+
3+
4+
public class Pet {
5+
6+
private int id;
7+
8+
private String name;
9+
10+
public int getId() {
11+
return id;
12+
}
13+
14+
public void setId(final int id) {
15+
this.id = id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(final String name) {
23+
this.name = name;
24+
}
25+
26+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.jooby.swagger;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Optional;
6+
7+
import javax.inject.Singleton;
8+
9+
import org.jooby.Err;
10+
import org.jooby.mvc.Body;
11+
import org.jooby.mvc.GET;
12+
import org.jooby.mvc.POST;
13+
import org.jooby.mvc.Path;
14+
15+
import com.google.common.collect.Lists;
16+
17+
@Path("/pets")
18+
@Singleton
19+
public class Pets {
20+
21+
private Map<Integer, Pet> pets = new HashMap<>();
22+
23+
@GET
24+
@Path("/{id}")
25+
public Pet get(final int id) {
26+
Pet pet = pets.get(id);
27+
if (pet == null) {
28+
throw new Err(404, "" + id);
29+
}
30+
return pet;
31+
}
32+
33+
@GET
34+
public Iterable<Pet> list(final Optional<Integer> size) {
35+
return Lists.newArrayList(pets.values()).subList(0, size.orElse(pets.values().size()));
36+
}
37+
38+
@POST
39+
public Pet create(@Body final Pet pet) {
40+
pet.setId(pets.size() + 1);
41+
pets.put(pet.getId(), pet);
42+
return pet;
43+
}
44+
45+
}
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
package org.jooby.swagger;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class SwaggerFeature extends ServerFeature {
7+
8+
{
9+
use(new SwaggerUI());
10+
11+
use(Pets.class);
12+
}
13+
14+
@Test
15+
public void ui() throws Exception {
16+
request()
17+
.get("/swagger")
18+
.expect(200)
19+
.startsWith("<!DOCTYPE html>\n" +
20+
"<html>\n" +
21+
"<head>\n" +
22+
" <title>Swagger UI</title>");
23+
24+
request()
25+
.get("/swagger/pets")
26+
.expect(200)
27+
.startsWith("<!DOCTYPE html>\n" +
28+
"<html>\n" +
29+
"<head>\n" +
30+
" <title>Swagger UI</title>");
31+
}
32+
33+
@Test
34+
public void json() throws Exception {
35+
request()
36+
.get("/swagger/swagger.json")
37+
.expect("{\n" +
38+
" \"swagger\" : \"2.0\",\n" +
39+
" \"info\" : {\n" +
40+
" \"title\" : \"swagger\"\n" +
41+
" },\n" +
42+
" \"basePath\" : \"/\",\n" +
43+
" \"tags\" : [ {\n" +
44+
" \"name\" : \"pets\"\n" +
45+
" } ],\n" +
46+
" \"schemes\" : [ \"http\" ],\n" +
47+
" \"consumes\" : [ \"application/json\" ],\n" +
48+
" \"produces\" : [ \"application/json\" ],\n" +
49+
" \"paths\" : {\n" +
50+
" \"/pets\" : {\n" +
51+
" \"get\" : {\n" +
52+
" \"tags\" : [ \"pets\" ],\n" +
53+
" \"parameters\" : [ {\n" +
54+
" \"name\" : \"size\",\n" +
55+
" \"in\" : \"query\",\n" +
56+
" \"required\" : false,\n" +
57+
" \"type\" : \"integer\",\n" +
58+
" \"format\" : \"int32\"\n" +
59+
" } ]\n" +
60+
" },\n" +
61+
" \"post\" : {\n" +
62+
" \"tags\" : [ \"pets\" ],\n" +
63+
" \"parameters\" : [ {\n" +
64+
" \"in\" : \"body\",\n" +
65+
" \"name\" : \"pet\",\n" +
66+
" \"required\" : true,\n" +
67+
" \"schema\" : {\n" +
68+
" \"$ref\" : \"#/definitions/Pet\"\n" +
69+
" }\n" +
70+
" } ]\n" +
71+
" }\n" +
72+
" },\n" +
73+
" \"/pets/{id}\" : {\n" +
74+
" \"get\" : {\n" +
75+
" \"tags\" : [ \"pets\" ],\n" +
76+
" \"parameters\" : [ {\n" +
77+
" \"name\" : \"id\",\n" +
78+
" \"in\" : \"path\",\n" +
79+
" \"required\" : true,\n" +
80+
" \"type\" : \"integer\",\n" +
81+
" \"format\" : \"int32\"\n" +
82+
" } ]\n" +
83+
" }\n" +
84+
" }\n" +
85+
" },\n" +
86+
" \"definitions\" : {\n" +
87+
" \"Pet\" : {\n" +
88+
" \"type\" : \"object\",\n" +
89+
" \"properties\" : {\n" +
90+
" \"id\" : {\n" +
91+
" \"type\" : \"integer\",\n" +
92+
" \"format\" : \"int32\"\n" +
93+
" },\n" +
94+
" \"name\" : {\n" +
95+
" \"type\" : \"string\"\n" +
96+
" }\n" +
97+
" }\n" +
98+
" }\n" +
99+
" }\n" +
100+
"}");
101+
102+
request()
103+
.get("/swagger/pets/swagger.json")
104+
.expect("{\n" +
105+
" \"swagger\" : \"2.0\",\n" +
106+
" \"info\" : {\n" +
107+
" \"title\" : \"swagger\"\n" +
108+
" },\n" +
109+
" \"basePath\" : \"/\",\n" +
110+
" \"tags\" : [ {\n" +
111+
" \"name\" : \"pets\"\n" +
112+
" } ],\n" +
113+
" \"schemes\" : [ \"http\" ],\n" +
114+
" \"consumes\" : [ \"application/json\" ],\n" +
115+
" \"produces\" : [ \"application/json\" ],\n" +
116+
" \"paths\" : {\n" +
117+
" \"/pets\" : {\n" +
118+
" \"get\" : {\n" +
119+
" \"tags\" : [ \"pets\" ],\n" +
120+
" \"parameters\" : [ {\n" +
121+
" \"name\" : \"size\",\n" +
122+
" \"in\" : \"query\",\n" +
123+
" \"required\" : false,\n" +
124+
" \"type\" : \"integer\",\n" +
125+
" \"format\" : \"int32\"\n" +
126+
" } ]\n" +
127+
" },\n" +
128+
" \"post\" : {\n" +
129+
" \"tags\" : [ \"pets\" ],\n" +
130+
" \"parameters\" : [ {\n" +
131+
" \"in\" : \"body\",\n" +
132+
" \"name\" : \"pet\",\n" +
133+
" \"required\" : true,\n" +
134+
" \"schema\" : {\n" +
135+
" \"$ref\" : \"#/definitions/Pet\"\n" +
136+
" }\n" +
137+
" } ]\n" +
138+
" }\n" +
139+
" },\n" +
140+
" \"/pets/{id}\" : {\n" +
141+
" \"get\" : {\n" +
142+
" \"tags\" : [ \"pets\" ],\n" +
143+
" \"parameters\" : [ {\n" +
144+
" \"name\" : \"id\",\n" +
145+
" \"in\" : \"path\",\n" +
146+
" \"required\" : true,\n" +
147+
" \"type\" : \"integer\",\n" +
148+
" \"format\" : \"int32\"\n" +
149+
" } ]\n" +
150+
" }\n" +
151+
" }\n" +
152+
" },\n" +
153+
" \"definitions\" : {\n" +
154+
" \"Pet\" : {\n" +
155+
" \"type\" : \"object\",\n" +
156+
" \"properties\" : {\n" +
157+
" \"id\" : {\n" +
158+
" \"type\" : \"integer\",\n" +
159+
" \"format\" : \"int32\"\n" +
160+
" },\n" +
161+
" \"name\" : {\n" +
162+
" \"type\" : \"string\"\n" +
163+
" }\n" +
164+
" }\n" +
165+
" }\n" +
166+
" }\n" +
167+
"}");
168+
}
169+
170+
@Test
171+
public void yml() throws Exception {
172+
request()
173+
.get("/swagger/swagger.yml")
174+
.expect("---\n" +
175+
"swagger: \"2.0\"\n" +
176+
"info:\n" +
177+
" title: \"swagger\"\n" +
178+
"basePath: \"/\"\n" +
179+
"tags:\n" +
180+
"- name: \"pets\"\n" +
181+
"schemes:\n" +
182+
"- \"http\"\n" +
183+
"consumes:\n" +
184+
"- \"application/json\"\n" +
185+
"produces:\n" +
186+
"- \"application/json\"\n" +
187+
"paths:\n" +
188+
" /pets:\n" +
189+
" get:\n" +
190+
" tags:\n" +
191+
" - \"pets\"\n" +
192+
" parameters:\n" +
193+
" - name: \"size\"\n" +
194+
" in: \"query\"\n" +
195+
" required: false\n" +
196+
" type: \"integer\"\n" +
197+
" format: \"int32\"\n" +
198+
" post:\n" +
199+
" tags:\n" +
200+
" - \"pets\"\n" +
201+
" parameters:\n" +
202+
" - in: \"body\"\n" +
203+
" name: \"pet\"\n" +
204+
" required: true\n" +
205+
" schema:\n" +
206+
" $ref: \"#/definitions/Pet\"\n" +
207+
" /pets/{id}:\n" +
208+
" get:\n" +
209+
" tags:\n" +
210+
" - \"pets\"\n" +
211+
" parameters:\n" +
212+
" - name: \"id\"\n" +
213+
" in: \"path\"\n" +
214+
" required: true\n" +
215+
" type: \"integer\"\n" +
216+
" format: \"int32\"\n" +
217+
"definitions:\n" +
218+
" Pet:\n" +
219+
" type: \"object\"\n" +
220+
" properties:\n" +
221+
" id:\n" +
222+
" type: \"integer\"\n" +
223+
" format: \"int32\"\n" +
224+
" name:\n" +
225+
" type: \"string\"\n" +
226+
"");
227+
228+
request()
229+
.get("/swagger/pets/swagger.yml")
230+
.expect("---\n" +
231+
"swagger: \"2.0\"\n" +
232+
"info:\n" +
233+
" title: \"swagger\"\n" +
234+
"basePath: \"/\"\n" +
235+
"tags:\n" +
236+
"- name: \"pets\"\n" +
237+
"schemes:\n" +
238+
"- \"http\"\n" +
239+
"consumes:\n" +
240+
"- \"application/json\"\n" +
241+
"produces:\n" +
242+
"- \"application/json\"\n" +
243+
"paths:\n" +
244+
" /pets:\n" +
245+
" get:\n" +
246+
" tags:\n" +
247+
" - \"pets\"\n" +
248+
" parameters:\n" +
249+
" - name: \"size\"\n" +
250+
" in: \"query\"\n" +
251+
" required: false\n" +
252+
" type: \"integer\"\n" +
253+
" format: \"int32\"\n" +
254+
" post:\n" +
255+
" tags:\n" +
256+
" - \"pets\"\n" +
257+
" parameters:\n" +
258+
" - in: \"body\"\n" +
259+
" name: \"pet\"\n" +
260+
" required: true\n" +
261+
" schema:\n" +
262+
" $ref: \"#/definitions/Pet\"\n" +
263+
" /pets/{id}:\n" +
264+
" get:\n" +
265+
" tags:\n" +
266+
" - \"pets\"\n" +
267+
" parameters:\n" +
268+
" - name: \"id\"\n" +
269+
" in: \"path\"\n" +
270+
" required: true\n" +
271+
" type: \"integer\"\n" +
272+
" format: \"int32\"\n" +
273+
"definitions:\n" +
274+
" Pet:\n" +
275+
" type: \"object\"\n" +
276+
" properties:\n" +
277+
" id:\n" +
278+
" type: \"integer\"\n" +
279+
" format: \"int32\"\n" +
280+
" name:\n" +
281+
" type: \"string\"\n" +
282+
"");
283+
}
284+
285+
}

0 commit comments

Comments
 (0)