Skip to content

Commit 56ee465

Browse files
committed
Beheer: update Quarkus example met invalide query parameters
1 parent afc09aa commit 56ee465

4 files changed

Lines changed: 154 additions & 25 deletions

File tree

examples/quarkus/pom.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1313
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
1414
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
15-
<quarkus.platform.version>3.21.1</quarkus.platform.version>
15+
<quarkus.platform.version>3.34.3</quarkus.platform.version>
1616
<skipITs>true</skipITs>
1717
<surefire-plugin.version>3.5.2</surefire-plugin.version>
1818
</properties>
@@ -44,7 +44,20 @@
4444
</dependency>
4545
<dependency>
4646
<groupId>io.quarkus</groupId>
47-
<artifactId>quarkus-junit5</artifactId>
47+
<artifactId>quarkus-rest-jackson</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>io.quarkus</groupId>
51+
<artifactId>quarkus-hibernate-validator</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.quarkiverse.resteasy-problem</groupId>
55+
<artifactId>quarkus-resteasy-problem</artifactId>
56+
<version>3.32.0</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>io.quarkus</groupId>
60+
<artifactId>quarkus-junit</artifactId>
4861
<scope>test</scope>
4962
</dependency>
5063
<dependency>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.acme;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import io.quarkiverse.resteasy.problem.HttpProblem;
6+
import jakarta.ws.rs.core.Response;
7+
import org.eclipse.microprofile.openapi.annotations.media.Schema;
8+
9+
import java.util.List;
10+
11+
public class BadRequestProblem extends HttpProblem {
12+
protected BadRequestProblem(String message, List<BadRequestDetails> errors) {
13+
super(
14+
builder()
15+
.withTitle("Bad hello request")
16+
.withStatus(Response.Status.BAD_REQUEST)
17+
.withDetail(message)
18+
.with("errors", errors));
19+
}
20+
21+
@Schema(name = "errors", description = "All bad request errors")
22+
public List<BadRequestDetails> errors;
23+
24+
@Schema(
25+
name = "BadRequestDetails",
26+
description = "Request details according to API Design Rules")
27+
public static class BadRequestDetails {
28+
@Schema(description = "Where the error occurs")
29+
public BadRequestLocation in;
30+
31+
@Schema(description = "Human-readable message describing the violation")
32+
public String detail;
33+
34+
@Schema(description = "A locator for the offending value")
35+
public String location;
36+
37+
@Schema(
38+
nullable = true,
39+
description =
40+
"A zero-based index position when multiple query parameters have the same name")
41+
@JsonInclude(value = JsonInclude.Include.NON_NULL)
42+
public Integer index;
43+
44+
private BadRequestDetails(
45+
BadRequestLocation in, String detail, String location, Integer index) {
46+
this.in = in;
47+
this.detail = detail;
48+
this.location = location;
49+
this.index = index;
50+
}
51+
52+
public static BadRequestDetails forQuery(
53+
BadRequestLocation in, String detail, Integer index) {
54+
return new BadRequestDetails(in, detail, "query", index);
55+
}
56+
}
57+
58+
public enum BadRequestLocation {
59+
@JsonProperty("body")
60+
Body,
61+
@JsonProperty("query")
62+
Query,
63+
;
64+
}
65+
}
Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,79 @@
11
package org.acme;
22

33
import jakarta.ws.rs.GET;
4+
import jakarta.ws.rs.POST;
45
import jakarta.ws.rs.Path;
5-
import jakarta.ws.rs.Produces;
66
import jakarta.ws.rs.core.MediaType;
77
import org.eclipse.microprofile.openapi.annotations.Operation;
88
import org.eclipse.microprofile.openapi.annotations.headers.Header;
99
import org.eclipse.microprofile.openapi.annotations.media.Content;
1010
import org.eclipse.microprofile.openapi.annotations.media.Schema;
1111
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
1212
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
13+
import org.jboss.resteasy.reactive.RestQuery;
14+
import org.jboss.resteasy.reactive.Separator;
15+
16+
import java.util.List;
17+
import java.util.Map;
1318

1419
@Path("/hello")
1520
@Tag(name = "greeting")
1621
public class GreetingResource {
1722

1823
@GET
19-
@Operation(
20-
description = "Hello world endpoint"
21-
)
24+
@Operation(description = "Hello world endpoint")
2225
@APIResponse(
2326
responseCode = "200",
2427
content = @Content(mediaType = MediaType.TEXT_PLAIN),
2528
headers = {
26-
@Header(
27-
name = OpenApiConstants.API_VERSION_HEADER_NAME,
28-
schema = @Schema(implementation = String.class)
29-
)
30-
}
31-
)
29+
@Header(
30+
name = OpenApiConstants.API_VERSION_HEADER_NAME,
31+
schema = @Schema(implementation = String.class))
32+
})
3233
public String hello() {
3334
return "Hello from Quarkus REST";
3435
}
36+
37+
@POST
38+
@Operation(description = "Test bad request with query parameter")
39+
@APIResponse(
40+
responseCode = "200",
41+
content = @Content(mediaType = MediaType.TEXT_PLAIN),
42+
headers = {
43+
@Header(
44+
name = OpenApiConstants.API_VERSION_HEADER_NAME,
45+
schema = @Schema(implementation = String.class))
46+
})
47+
@APIResponse(
48+
responseCode = "400",
49+
description = "Bad request response",
50+
content =
51+
@Content(
52+
mediaType = "application/problem+json",
53+
schema = @Schema(implementation = BadRequestProblem.class)))
54+
public String withInput(@RestQuery("query") @Separator(",") List<String> queryParam) {
55+
if (queryParam.isEmpty()) {
56+
throw new BadRequestProblem(
57+
"Missing required query parameter",
58+
List.of(
59+
BadRequestProblem.BadRequestDetails.forQuery(
60+
BadRequestProblem.BadRequestLocation.Query,
61+
"Query parameter is invalid",
62+
null)));
63+
}
64+
var queryParams = queryParam.iterator();
65+
for (var index = 0; queryParams.hasNext(); index++) {
66+
var param = queryParams.next();
67+
if (!param.equals("42")) {
68+
throw new BadRequestProblem(
69+
"Missing required query parameter",
70+
List.of(
71+
BadRequestProblem.BadRequestDetails.forQuery(
72+
BadRequestProblem.BadRequestLocation.Query,
73+
"Query parameter is invalid",
74+
index)));
75+
}
76+
}
77+
return "Successful response";
78+
}
3579
}

media/linter.yaml

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,26 +244,33 @@ rules:
244244
errors:
245245
type: object
246246
properties:
247-
properties:
247+
items:
248248
type: object
249249
properties:
250-
in: {}
251-
detail: {}
252-
location:
250+
properties:
253251
type: object
254252
properties:
255-
properties:
253+
in: {}
254+
detail: {}
255+
location:
256256
type: object
257257
properties:
258-
pointer: {}
259-
name: {}
260-
index: {}
261-
additionalProperties: false
262-
code: {}
258+
properties:
259+
type: object
260+
properties:
261+
pointer: {}
262+
name: {}
263+
index: {}
264+
additionalProperties: false
265+
code: {}
266+
required:
267+
- in
268+
- detail
269+
- location
263270
required:
264-
- in
265-
- detail
266-
additionalProperties: false
271+
- properties
272+
required:
273+
- items
267274
required:
268275
- errors
269276

0 commit comments

Comments
 (0)