Skip to content

Commit 0dd4eb2

Browse files
authored
Merge pull request #70 from avaje/feature/jex2x-with-slashAcceptingNamedPaths
Add support for Jex 2.0 with slash accepting named paths (rather than splat)
2 parents 1ee3bc4 + ca9ab94 commit 0dd4eb2

File tree

5 files changed

+82
-13
lines changed

5 files changed

+82
-13
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/PathSegments.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ static PathSegments parse(String fullPath) {
2828
final String name = section.substring(1);
2929
Segment segment = createSegment(name);
3030
segments.add(segment);
31-
chunks.named(segment.path(name));
31+
chunks.named(segment.path(name), ':');
3232

33-
} else if ((section.startsWith("{") && (section.endsWith("}")))) {
33+
} else if (isPathParameter(section)) {
3434
String name = section.substring(1, section.length() - 1);
3535
Segment segment = createSegment(name);
3636
segments.add(segment);
37-
chunks.named(segment.path(name));
37+
chunks.named(segment.path(name), section.charAt(0));
3838

3939
} else {
4040
Segment segment = createLiteralSegment(section);
@@ -44,10 +44,14 @@ static PathSegments parse(String fullPath) {
4444
}
4545
}
4646
}
47-
4847
return new PathSegments(chunks, segments);
4948
}
5049

50+
private static boolean isPathParameter(String section) {
51+
return section.startsWith("{") && section.endsWith("}")
52+
|| section.startsWith("<") && section.endsWith(">");
53+
}
54+
5155
private static Segment createLiteralSegment(String section) {
5256
return new Segment(section, true);
5357
}
@@ -250,8 +254,8 @@ String path(String section) {
250254
private static class Chunks {
251255
private final List<Chunk> chunks = new ArrayList<>();
252256

253-
void named(String name) {
254-
chunks.add(new Chunk(name));
257+
void named(String name, char firstChar) {
258+
chunks.add(new Chunk(name, firstChar));
255259
}
256260

257261
void literal(String val) {
@@ -269,7 +273,7 @@ String fullPath(String prefix, String suffix) {
269273

270274
private static class LiteralChunk extends Chunk {
271275
private LiteralChunk(String value) {
272-
super(value);
276+
super(value, ' ');
273277
}
274278

275279
@Override
@@ -280,12 +284,18 @@ void append(StringBuilder fullPath, String prefix, String suffix) {
280284

281285
private static class Chunk {
282286
final String value;
283-
private Chunk(String value) {
287+
final char firstChar;
288+
private Chunk(String value, char firstChar) {
284289
this.value = value;
290+
this.firstChar = firstChar;
285291
}
286292

287293
void append(StringBuilder fullPath, String prefix, String suffix) {
288-
fullPath.append(prefix).append(value).append(suffix);
294+
if ('<' == firstChar) {
295+
fullPath.append('<').append(value).append('>');
296+
} else {
297+
fullPath.append(prefix).append(value).append(suffix);
298+
}
289299
}
290300
}
291301

tests/test-jex/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<properties>
1818
<maven.deploy.skip>true</maven.deploy.skip>
1919
<main.class>org.example.myapp.Main</main.class>
20-
<jex.version>1.6</jex.version>
20+
<jex.version>2.0</jex.version>
2121
<swagger.version>2.0.8</swagger.version>
2222
<jackson.version>2.12.3</jackson.version>
2323
<avaje-inject.version>6.1</avaje-inject.version>
@@ -38,6 +38,12 @@
3838
<version>${jex.version}</version>
3939
</dependency>
4040

41+
<dependency>
42+
<groupId>io.avaje</groupId>
43+
<artifactId>avaje-jex-jetty</artifactId>
44+
<version>${jex.version}</version>
45+
</dependency>
46+
4147
<dependency>
4248
<groupId>com.fasterxml.jackson.core</groupId>
4349
<artifactId>jackson-databind</artifactId>

tests/test-jex/src/main/java/org/example/web/HelloController.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ String name(String name) {
3232
}
3333

3434
@Produces("text/plain")
35-
@Get("splat/{name}/*/other/*")
35+
@Get("splat/{name}/<s0>/other/<s1>")
3636
String splat(String name, Context ctx) {
37-
return "got name:" + name + " splat0:" + ctx.splat(0) + " splat1:" + ctx.splat(1);
37+
return "got name:" + name + " splat0:" + ctx.pathParam("s0") + " splat1:" + ctx.pathParam("s1");
38+
}
39+
40+
@Produces("text/plain")
41+
@Get("splat2/{name}/<nam0>/other/<nam1>")
42+
String splat2(String name, String nam0, String nam1) {
43+
return "got name:" + name + " splat0:" + nam0 + " splat1:" + nam1;
3844
}
3945

4046
@Valid

tests/test-jex/src/main/resources/public/openapi.json

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
}
9191
}
9292
},
93-
"/splat/{name}/*/other/*" : {
93+
"/splat/{name}/<s0>/other/<s1>" : {
9494
"get" : {
9595
"tags" : [ ],
9696
"summary" : "",
@@ -116,6 +116,47 @@
116116
}
117117
}
118118
}
119+
},
120+
"/splat2/{name}/<nam0>/other/<nam1>" : {
121+
"get" : {
122+
"tags" : [ ],
123+
"summary" : "",
124+
"description" : "",
125+
"parameters" : [ {
126+
"name" : "name",
127+
"in" : "path",
128+
"required" : true,
129+
"schema" : {
130+
"type" : "string"
131+
}
132+
}, {
133+
"name" : "nam0",
134+
"in" : "path",
135+
"required" : true,
136+
"schema" : {
137+
"type" : "string"
138+
}
139+
}, {
140+
"name" : "nam1",
141+
"in" : "path",
142+
"required" : true,
143+
"schema" : {
144+
"type" : "string"
145+
}
146+
} ],
147+
"responses" : {
148+
"200" : {
149+
"description" : "",
150+
"content" : {
151+
"text/plain" : {
152+
"schema" : {
153+
"type" : "string"
154+
}
155+
}
156+
}
157+
}
158+
}
159+
}
119160
}
120161
},
121162
"components" : {

tests/test-jex/src/test/java/org/example/web/HelloControllerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ void splat() {
4343
assertEquals("got name:one splat0:a/b splat1:x/y/z", client.request().path("splat/one/a/b/other/x/y/z").GET().asString().body());
4444
}
4545

46+
@Test
47+
void splat2() {
48+
assertEquals("got name:one splat0:a/b splat1:x/y/z", client.request().path("splat2/one/a/b/other/x/y/z").GET().asString().body());
49+
}
50+
51+
4652
@Test
4753
void validation() {
4854

0 commit comments

Comments
 (0)