Skip to content

Add support for Jex 2.0 with slash accepting named paths (rather than splat) #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ static PathSegments parse(String fullPath) {
final String name = section.substring(1);
Segment segment = createSegment(name);
segments.add(segment);
chunks.named(segment.path(name));
chunks.named(segment.path(name), ':');

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

} else {
Segment segment = createLiteralSegment(section);
Expand All @@ -44,10 +44,14 @@ static PathSegments parse(String fullPath) {
}
}
}

return new PathSegments(chunks, segments);
}

private static boolean isPathParameter(String section) {
return section.startsWith("{") && section.endsWith("}")
|| section.startsWith("<") && section.endsWith(">");
}

private static Segment createLiteralSegment(String section) {
return new Segment(section, true);
}
Expand Down Expand Up @@ -250,8 +254,8 @@ String path(String section) {
private static class Chunks {
private final List<Chunk> chunks = new ArrayList<>();

void named(String name) {
chunks.add(new Chunk(name));
void named(String name, char firstChar) {
chunks.add(new Chunk(name, firstChar));
}

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

private static class LiteralChunk extends Chunk {
private LiteralChunk(String value) {
super(value);
super(value, ' ');
}

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

private static class Chunk {
final String value;
private Chunk(String value) {
final char firstChar;
private Chunk(String value, char firstChar) {
this.value = value;
this.firstChar = firstChar;
}

void append(StringBuilder fullPath, String prefix, String suffix) {
fullPath.append(prefix).append(value).append(suffix);
if ('<' == firstChar) {
fullPath.append('<').append(value).append('>');
} else {
fullPath.append(prefix).append(value).append(suffix);
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion tests/test-jex/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<main.class>org.example.myapp.Main</main.class>
<jex.version>1.6</jex.version>
<jex.version>2.0</jex.version>
<swagger.version>2.0.8</swagger.version>
<jackson.version>2.12.3</jackson.version>
<avaje-inject.version>6.1</avaje-inject.version>
Expand All @@ -38,6 +38,12 @@
<version>${jex.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex-jetty</artifactId>
<version>${jex.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ String name(String name) {
}

@Produces("text/plain")
@Get("splat/{name}/*/other/*")
@Get("splat/{name}/<s0>/other/<s1>")
String splat(String name, Context ctx) {
return "got name:" + name + " splat0:" + ctx.splat(0) + " splat1:" + ctx.splat(1);
return "got name:" + name + " splat0:" + ctx.pathParam("s0") + " splat1:" + ctx.pathParam("s1");
}

@Produces("text/plain")
@Get("splat2/{name}/<nam0>/other/<nam1>")
String splat2(String name, String nam0, String nam1) {
return "got name:" + name + " splat0:" + nam0 + " splat1:" + nam1;
}

@Valid
Expand Down
43 changes: 42 additions & 1 deletion tests/test-jex/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
}
}
},
"/splat/{name}/*/other/*" : {
"/splat/{name}/<s0>/other/<s1>" : {
"get" : {
"tags" : [ ],
"summary" : "",
Expand All @@ -116,6 +116,47 @@
}
}
}
},
"/splat2/{name}/<nam0>/other/<nam1>" : {
"get" : {
"tags" : [ ],
"summary" : "",
"description" : "",
"parameters" : [ {
"name" : "name",
"in" : "path",
"required" : true,
"schema" : {
"type" : "string"
}
}, {
"name" : "nam0",
"in" : "path",
"required" : true,
"schema" : {
"type" : "string"
}
}, {
"name" : "nam1",
"in" : "path",
"required" : true,
"schema" : {
"type" : "string"
}
} ],
"responses" : {
"200" : {
"description" : "",
"content" : {
"text/plain" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
}
},
"components" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ void splat() {
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());
}

@Test
void splat2() {
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());
}


@Test
void validation() {

Expand Down