Skip to content

Commit 151084e

Browse files
committed
Swagger and Raml not working properly fix #378
1 parent 88dca30 commit 151084e

13 files changed

Lines changed: 289 additions & 119 deletions

File tree

coverage-report/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@
458458
<version>${project.version}</version>
459459
</dependency>
460460

461+
<dependency>
462+
<groupId>org.jooby</groupId>
463+
<artifactId>jooby-raml</artifactId>
464+
<version>${project.version}</version>
465+
</dependency>
466+
461467
<dependency>
462468
<groupId>org.jooby</groupId>
463469
<artifactId>jooby-aws</artifactId>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.MediaType;
4+
import org.jooby.issues.i378.Cat;
5+
import org.jooby.raml.Raml;
6+
import org.jooby.swagger.SwaggerUI;
7+
import org.jooby.test.ServerFeature;
8+
import org.junit.Test;
9+
10+
public class Issue378 extends ServerFeature {
11+
12+
{
13+
/**
14+
* Produces Cat object
15+
*
16+
* Next line
17+
*/
18+
use("/api/cat/")
19+
/**
20+
* @param name Cat's name
21+
*
22+
* @return Returns a cat {@link Cat}
23+
*/
24+
.get("/:name", req -> {
25+
Cat cat = new Cat();
26+
cat.setName(req.param("name").value());
27+
28+
return cat;
29+
})
30+
.produces(MediaType.json);
31+
32+
new Raml().install(this);
33+
new SwaggerUI().install(this);
34+
}
35+
36+
@Test
37+
public void shouldGenerateAValidRaml() throws Exception {
38+
request()
39+
.get("/raml/api.raml")
40+
.expect("#%RAML 1.0\n" +
41+
"title: issues API\n" +
42+
"version: 0.0.0\n" +
43+
"protocols: [HTTP]\n" +
44+
"baseUri: http://localhost:9999/\n" +
45+
"mediaType: application/json\n" +
46+
"types:\n" +
47+
" Cat:\n" +
48+
" type: object\n" +
49+
" properties:\n" +
50+
" name:\n" +
51+
" type: string\n" +
52+
"/api/cat/{name}:\n" +
53+
" uriParameters:\n" +
54+
" name:\n" +
55+
" type: string\n" +
56+
" description: 'Cat''s name'\n" +
57+
" required: true\n" +
58+
" description: |-\n" +
59+
" Produces Cat object\n" +
60+
" Next line\n" +
61+
" get:\n" +
62+
" responses:\n" +
63+
" 200:\n" +
64+
" description: 'Returns a cat {@link Cat}'\n" +
65+
" body:\n" +
66+
" application/json:\n" +
67+
" type: Cat");
68+
}
69+
70+
@Test
71+
public void shouldGenerateAValidSwagger() throws Exception {
72+
request()
73+
.get("/swagger/swagger.json")
74+
.expect("{\n" +
75+
" \"swagger\" : \"2.0\",\n" +
76+
" \"info\" : {\n" +
77+
" \"version\" : \"0.0.0\",\n" +
78+
" \"title\" : \"issues API\"\n" +
79+
" },\n" +
80+
" \"basePath\" : \"/\",\n" +
81+
" \"tags\" : [ {\n" +
82+
" \"name\" : \"cat\",\n" +
83+
" \"description\" : \"Produces Cat object\\nNext line\"\n" +
84+
" } ],\n" +
85+
" \"schemes\" : [ \"http\" ],\n" +
86+
" \"consumes\" : [ \"application/json\" ],\n" +
87+
" \"produces\" : [ \"application/json\" ],\n" +
88+
" \"paths\" : {\n" +
89+
" \"/api/cat/{name}\" : {\n" +
90+
" \"get\" : {\n" +
91+
" \"tags\" : [ \"cat\" ],\n" +
92+
" \"produces\" : [ \"application/json\" ],\n" +
93+
" \"parameters\" : [ {\n" +
94+
" \"name\" : \"name\",\n" +
95+
" \"in\" : \"path\",\n" +
96+
" \"description\" : \"Cat's name\",\n" +
97+
" \"required\" : true,\n" +
98+
" \"type\" : \"string\"\n" +
99+
" } ],\n" +
100+
" \"responses\" : {\n" +
101+
" \"200\" : {\n" +
102+
" \"description\" : \"Returns a cat {@link Cat}\",\n" +
103+
" \"schema\" : {\n" +
104+
" \"$ref\" : \"#/definitions/Cat\"\n" +
105+
" }\n" +
106+
" }\n" +
107+
" }\n" +
108+
" }\n" +
109+
" }\n" +
110+
" },\n" +
111+
" \"definitions\" : {\n" +
112+
" \"Cat\" : {\n" +
113+
" \"type\" : \"object\",\n" +
114+
" \"properties\" : {\n" +
115+
" \"name\" : {\n" +
116+
" \"type\" : \"string\"\n" +
117+
" }\n" +
118+
" }\n" +
119+
" }\n" +
120+
" }\n" +
121+
"}");
122+
}
123+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.jooby.issues.i378;
2+
3+
public class Cat {
4+
private String name;
5+
6+
public String getName() {
7+
return name;
8+
}
9+
10+
public void setName(String name) {
11+
this.name = name;
12+
}
13+
}

coverage-report/src/test/java/org/jooby/swagger/SwaggerFeature.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public void json() throws Exception {
5252
" \"get\" : {\n" +
5353
" \"tags\" : [ \"pets\" ],\n" +
5454
" \"summary\" : \"Pets.list\",\n" +
55-
" \"consumes\" : [ \"*/*\" ],\n" +
56-
" \"produces\" : [ \"*/*\" ],\n" +
5755
" \"parameters\" : [ {\n" +
5856
" \"name\" : \"size\",\n" +
5957
" \"in\" : \"query\",\n" +
@@ -73,8 +71,6 @@ public void json() throws Exception {
7371
" \"post\" : {\n" +
7472
" \"tags\" : [ \"pets\" ],\n" +
7573
" \"summary\" : \"Pets.create\",\n" +
76-
" \"consumes\" : [ \"*/*\" ],\n" +
77-
" \"produces\" : [ \"*/*\" ],\n" +
7874
" \"parameters\" : [ {\n" +
7975
" \"in\" : \"body\",\n" +
8076
" \"name\" : \"pet\",\n" +
@@ -97,8 +93,6 @@ public void json() throws Exception {
9793
" \"get\" : {\n" +
9894
" \"tags\" : [ \"pets\" ],\n" +
9995
" \"summary\" : \"Pets.get\",\n" +
100-
" \"consumes\" : [ \"*/*\" ],\n" +
101-
" \"produces\" : [ \"*/*\" ],\n" +
10296
" \"parameters\" : [ {\n" +
10397
" \"name\" : \"id\",\n" +
10498
" \"in\" : \"path\",\n" +
@@ -153,8 +147,6 @@ public void json() throws Exception {
153147
" \"get\" : {\n" +
154148
" \"tags\" : [ \"pets\" ],\n" +
155149
" \"summary\" : \"Pets.list\",\n" +
156-
" \"consumes\" : [ \"*/*\" ],\n" +
157-
" \"produces\" : [ \"*/*\" ],\n" +
158150
" \"parameters\" : [ {\n" +
159151
" \"name\" : \"size\",\n" +
160152
" \"in\" : \"query\",\n" +
@@ -174,8 +166,6 @@ public void json() throws Exception {
174166
" \"post\" : {\n" +
175167
" \"tags\" : [ \"pets\" ],\n" +
176168
" \"summary\" : \"Pets.create\",\n" +
177-
" \"consumes\" : [ \"*/*\" ],\n" +
178-
" \"produces\" : [ \"*/*\" ],\n" +
179169
" \"parameters\" : [ {\n" +
180170
" \"in\" : \"body\",\n" +
181171
" \"name\" : \"pet\",\n" +
@@ -198,8 +188,6 @@ public void json() throws Exception {
198188
" \"get\" : {\n" +
199189
" \"tags\" : [ \"pets\" ],\n" +
200190
" \"summary\" : \"Pets.get\",\n" +
201-
" \"consumes\" : [ \"*/*\" ],\n" +
202-
" \"produces\" : [ \"*/*\" ],\n" +
203191
" \"parameters\" : [ {\n" +
204192
" \"name\" : \"id\",\n" +
205193
" \"in\" : \"path\",\n" +
@@ -259,10 +247,6 @@ public void yml() throws Exception {
259247
" tags:\n" +
260248
" - \"pets\"\n" +
261249
" summary: \"Pets.list\"\n" +
262-
" consumes:\n" +
263-
" - \"*/*\"\n" +
264-
" produces:\n" +
265-
" - \"*/*\"\n" +
266250
" parameters:\n" +
267251
" - name: \"size\"\n" +
268252
" in: \"query\"\n" +
@@ -278,10 +262,6 @@ public void yml() throws Exception {
278262
" tags:\n" +
279263
" - \"pets\"\n" +
280264
" summary: \"Pets.create\"\n" +
281-
" consumes:\n" +
282-
" - \"*/*\"\n" +
283-
" produces:\n" +
284-
" - \"*/*\"\n" +
285265
" parameters:\n" +
286266
" - in: \"body\"\n" +
287267
" name: \"pet\"\n" +
@@ -298,10 +278,6 @@ public void yml() throws Exception {
298278
" tags:\n" +
299279
" - \"pets\"\n" +
300280
" summary: \"Pets.get\"\n" +
301-
" consumes:\n" +
302-
" - \"*/*\"\n" +
303-
" produces:\n" +
304-
" - \"*/*\"\n" +
305281
" parameters:\n" +
306282
" - name: \"id\"\n" +
307283
" in: \"path\"\n" +
@@ -346,10 +322,6 @@ public void yml() throws Exception {
346322
" tags:\n" +
347323
" - \"pets\"\n" +
348324
" summary: \"Pets.list\"\n" +
349-
" consumes:\n" +
350-
" - \"*/*\"\n" +
351-
" produces:\n" +
352-
" - \"*/*\"\n" +
353325
" parameters:\n" +
354326
" - name: \"size\"\n" +
355327
" in: \"query\"\n" +
@@ -365,10 +337,6 @@ public void yml() throws Exception {
365337
" tags:\n" +
366338
" - \"pets\"\n" +
367339
" summary: \"Pets.create\"\n" +
368-
" consumes:\n" +
369-
" - \"*/*\"\n" +
370-
" produces:\n" +
371-
" - \"*/*\"\n" +
372340
" parameters:\n" +
373341
" - in: \"body\"\n" +
374342
" name: \"pet\"\n" +
@@ -385,10 +353,6 @@ public void yml() throws Exception {
385353
" tags:\n" +
386354
" - \"pets\"\n" +
387355
" summary: \"Pets.get\"\n" +
388-
" consumes:\n" +
389-
" - \"*/*\"\n" +
390-
" produces:\n" +
391-
" - \"*/*\"\n" +
392356
" parameters:\n" +
393357
" - name: \"id\"\n" +
394358
" in: \"path\"\n" +

jooby-raml/src/main/java/org/jooby/internal/raml/Doc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class Doc {
4040
private static final Logger log = LoggerFactory.getLogger(Raml.class);
4141

4242
public static String toMarkDown(final String html) {
43-
Document doc = Jsoup.parseBodyFragment(html);
43+
Document doc = Jsoup.parseBodyFragment(html.replace("\n", "<br>"));
4444
StringBuilder buff = new StringBuilder();
4545
recurseElement(doc.body(), buff);
4646
return buff.toString();
@@ -52,13 +52,13 @@ public static String toYaml(final String text, final int level) {
5252
.filter(l -> l.trim().length() > 0)
5353
.count();
5454
if (count == 1) {
55-
return text.trim();
55+
return "'" + text.trim().replace("'", "''") + "'";
5656
}
5757
StringBuilder indent = new StringBuilder();
5858
for (int i = 0; i < level + 2; i++) {
5959
indent.append(" ");
6060
}
61-
return "|\n" + lines.stream()
61+
return "|-\n" + lines.stream()
6262
.map(line -> {
6363
if (line.trim().length() > 0) {
6464
return indent + line;

jooby-raml/src/main/java/org/jooby/internal/raml/RamlBuilder.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.Map;
2929
import java.util.Set;
3030
import java.util.function.Consumer;
31+
import java.util.regex.Matcher;
32+
import java.util.regex.Pattern;
3133
import java.util.stream.Collectors;
3234

3335
import javax.inject.Inject;
@@ -57,17 +59,10 @@ private static class Resource {
5759
private String mediaType;
5860

5961
public Resource(final String pattern, final String mediaType) {
60-
this.pattern = normalize(pattern);
62+
this.pattern = pattern;
6163
this.mediaType = mediaType;
6264
}
6365

64-
private String normalize(final String pattern) {
65-
if (pattern.startsWith("/:")) {
66-
return "/{" + pattern.substring(2) + "}";
67-
}
68-
return pattern;
69-
}
70-
7166
List<RouteSpec> routes() {
7267
List<RouteSpec> routes = new ArrayList<>();
7368
routes.addAll(this.routes);
@@ -103,7 +98,7 @@ private String toString(final int level) {
10398
if (deep.size() == 1) {
10499
routes = deep;
105100
List<String> snested = Splitter.on("/").trimResults().omitEmptyStrings()
106-
.splitToList(deep.get(0).pattern());
101+
.splitToList(normalize(deep.get(0).pattern()));
107102
pattern = normalize("/"
108103
+ snested.subList(level, snested.size()).stream().collect(Collectors.joining("/")));
109104
children = Collections.emptySet();
@@ -215,7 +210,7 @@ private String toString(final int level) {
215210
statusCodes.remove(rsp.statusCode());
216211
statusCodes.forEach((sc, msg) -> {
217212
buff.append(indent(level + 3)).append(sc).append(":\n");
218-
buff.append(indent(level + 4)).append("description: ").append(msg).append("\n");
213+
buff.append(indent(level + 4)).append("description: ").append(Doc.parse(msg, level + 8)).append("\n");
219214
});
220215
}
221216
}
@@ -254,6 +249,8 @@ private CharSequence param(final RouteParam p, final int level) {
254249

255250
}
256251

252+
private static final Pattern VAR = Pattern.compile("\\:((?:[^/]+)+?)");
253+
257254
private Config conf;
258255

259256
@Inject
@@ -296,10 +293,11 @@ private List<Resource> tree(final List<RouteSpec> routes, final String mediaType
296293
List<Resource> result = new ArrayList<>();
297294
Map<String, Resource> hash = new HashMap<>();
298295
for (RouteSpec route : routes) {
296+
String pattern = normalize(route.pattern());
299297
List<String> segments = Splitter.on('/')
300298
.trimResults()
301299
.omitEmptyStrings()
302-
.splitToList(route.pattern());
300+
.splitToList(pattern);
303301
String prev = "/";
304302
Resource root = null;
305303
for (int i = 0; i < segments.size(); i++) {
@@ -319,7 +317,7 @@ private List<Resource> tree(final List<RouteSpec> routes, final String mediaType
319317
root = resource;
320318
prev = it + "/";
321319
}
322-
hash.get(route.pattern()).routes.add(route);
320+
hash.get(pattern).routes.add(route);
323321
}
324322
return result;
325323
}
@@ -331,4 +329,18 @@ private static String indent(final int level) {
331329
}
332330
return buff.toString();
333331
}
332+
333+
private static String normalize(final String pattern) {
334+
Matcher matcher = VAR.matcher(pattern);
335+
StringBuilder result = new StringBuilder();
336+
int end = 0;
337+
while (matcher.find()) {
338+
result.append(pattern, end, matcher.start());
339+
result.append("{").append(matcher.group(1)).append("}");
340+
end = matcher.end();
341+
}
342+
result.append(pattern, end, pattern.length());
343+
return result.toString();
344+
}
345+
334346
}

0 commit comments

Comments
 (0)