Skip to content

Commit 8f75544

Browse files
committed
feat(web-validation): replace deprecated SchemaParser with new SchemaRepository
Signed-off-by: Pascal Krause <[email protected]>
1 parent d790e40 commit 8f75544

28 files changed

+635
-409
lines changed

vertx-web-api-service/src/main/java/examples/ApiCodegenExamples.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import io.vertx.ext.web.handler.HttpException;
1616
import io.vertx.ext.web.validation.ValidationHandler;
1717
import io.vertx.ext.web.validation.builder.ValidationHandlerBuilder;
18-
import io.vertx.json.schema.SchemaParser;
18+
import io.vertx.json.schema.SchemaRepository;
1919
import io.vertx.serviceproxy.ServiceBinder;
2020

2121
import static io.vertx.ext.web.validation.builder.Bodies.json;
@@ -29,7 +29,8 @@
2929
@Source
3030
public class ApiCodegenExamples {
3131

32-
public void mountHandler(EventBus eventBus, Router router, ValidationHandler validationHandler) {
32+
public void mountHandler(EventBus eventBus, Router router,
33+
ValidationHandler validationHandler) {
3334
router
3435
.get("/hello")
3536
.handler(validationHandler)
@@ -39,33 +40,38 @@ public void mountHandler(EventBus eventBus, Router router, ValidationHandler val
3940
);
4041
}
4142

42-
public void mountHandlerWithTimeout(EventBus eventBus, Router router, ValidationHandler validationHandler) {
43+
public void mountHandlerWithTimeout(EventBus eventBus, Router router,
44+
ValidationHandler validationHandler) {
4345
router
4446
.get("/hello")
4547
.handler(validationHandler)
4648
.handler(
4749
RouteToEBServiceHandler
48-
.build(eventBus, "greeters.myapplication", "hello", new DeliveryOptions().setSendTimeout(1000))
50+
.build(eventBus, "greeters.myapplication", "hello",
51+
new DeliveryOptions().setSendTimeout(1000))
4952
);
5053
}
5154

52-
public void serviceMountExample(EventBus eventBus, Router router, SchemaParser schemaParser) {
55+
public void serviceMountExample(EventBus eventBus, Router router,
56+
SchemaRepository repository) {
5357
router.get("/api/transactions")
5458
.handler(
55-
ValidationHandlerBuilder.create(schemaParser)
59+
ValidationHandlerBuilder.create(repository)
5660
.queryParameter(optionalParam("from", stringSchema()))
5761
.queryParameter(optionalParam("to", stringSchema()))
5862
.build()
5963
).handler(
60-
RouteToEBServiceHandler.build(eventBus, "transactions.myapplication", "getTransactionsList")
64+
RouteToEBServiceHandler.build(eventBus, "transactions.myapplication",
65+
"getTransactionsList")
6166
);
6267
router.post("/api/transactions")
6368
.handler(
64-
ValidationHandlerBuilder.create(schemaParser)
69+
ValidationHandlerBuilder.create(repository)
6570
.body(json(objectSchema()))
6671
.build()
6772
).handler(
68-
RouteToEBServiceHandler.build(eventBus, "transactions.myapplication", "putTransaction")
73+
RouteToEBServiceHandler.build(eventBus, "transactions.myapplication",
74+
"putTransaction")
6975
);
7076
}
7177

@@ -80,7 +86,9 @@ public void serviceMount(Vertx vertx) {
8086
.register(TransactionService.class, transactionService);
8187
}
8288

83-
public void implGetTransactionsListSuccess(String from, String to, ServiceRequest context, Handler<AsyncResult<ServiceResponse>> resultHandler) {
89+
public void implGetTransactionsListSuccess(String from, String to,
90+
ServiceRequest context,
91+
Handler<AsyncResult<ServiceResponse>> resultHandler) {
8492
// Your business logic
8593
resultHandler.handle(
8694
Future.succeededFuture(
@@ -89,7 +97,9 @@ public void implGetTransactionsListSuccess(String from, String to, ServiceReques
8997
);
9098
}
9199

92-
public void implGetTransactionsListFailure(String from, String to, ServiceRequest context, Handler<AsyncResult<ServiceResponse>> resultHandler) {
100+
public void implGetTransactionsListFailure(String from, String to,
101+
ServiceRequest context,
102+
Handler<AsyncResult<ServiceResponse>> resultHandler) {
93103
// Return a failed result
94104
resultHandler.handle(
95105
Future.failedFuture(

vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/RouteToEBServiceHandlerTest.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,18 @@
2424

2525
import static io.vertx.ext.web.validation.builder.Bodies.json;
2626
import static io.vertx.ext.web.validation.builder.Parameters.param;
27-
import static io.vertx.ext.web.validation.testutils.TestRequest.*;
28-
import static io.vertx.json.schema.draft7.dsl.Schemas.*;
27+
import static io.vertx.ext.web.validation.testutils.TestRequest.bodyResponse;
28+
import static io.vertx.ext.web.validation.testutils.TestRequest.emptyResponse;
29+
import static io.vertx.ext.web.validation.testutils.TestRequest.jsonBodyResponse;
30+
import static io.vertx.ext.web.validation.testutils.TestRequest.statusCode;
31+
import static io.vertx.ext.web.validation.testutils.TestRequest.statusMessage;
32+
import static io.vertx.ext.web.validation.testutils.TestRequest.testRequest;
33+
import static io.vertx.json.schema.draft7.dsl.Schemas.anyOf;
34+
import static io.vertx.json.schema.draft7.dsl.Schemas.arraySchema;
35+
import static io.vertx.json.schema.draft7.dsl.Schemas.intSchema;
36+
import static io.vertx.json.schema.draft7.dsl.Schemas.objectSchema;
37+
import static io.vertx.json.schema.draft7.dsl.Schemas.ref;
38+
import static io.vertx.json.schema.draft7.dsl.Schemas.stringSchema;
2939

3040
/**
3141
* @author Francesco Guardiani @slinkydeveloper
@@ -52,7 +62,7 @@ public void serviceProxyTypedTest(Vertx vertx, VertxTestContext testContext) {
5262
.post("/testE/:id")
5363
.handler(BodyHandler.create())
5464
.handler(
55-
ValidationHandler.builder(parser)
65+
ValidationHandler.builder(schemaRepo)
5666
.pathParameter(param("id", intSchema()))
5767
.body(json(objectSchema().property("value", intSchema())))
5868
.build()
@@ -64,7 +74,7 @@ public void serviceProxyTypedTest(Vertx vertx, VertxTestContext testContext) {
6474
.post("/testF/:id")
6575
.handler(BodyHandler.create())
6676
.handler(
67-
ValidationHandler.builder(parser)
77+
ValidationHandler.builder(schemaRepo)
6878
.pathParameter(param("id", intSchema()))
6979
.body(json(
7080
anyOf(
@@ -106,7 +116,7 @@ public void serviceProxyDataObjectTest(Vertx vertx, VertxTestContext testContext
106116
.post("/test")
107117
.handler(BodyHandler.create())
108118
.handler(
109-
ValidationHandler.builder(parser)
119+
ValidationHandler.builder(schemaRepo)
110120
.body(json(ref(JsonPointer.fromURI(URI.create("filter.json")))))
111121
.build()
112122
).handler(
@@ -135,7 +145,7 @@ public void emptyOperationResultTest(Vertx vertx, VertxTestContext testContext)
135145
router
136146
.get("/test")
137147
.handler(
138-
ValidationHandler.builder(parser).build()
148+
ValidationHandler.builder(schemaRepo).build()
139149
).handler(
140150
RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testEmptyServiceResponse")
141151
);
@@ -157,7 +167,7 @@ public void authorizedUserTest(Vertx vertx, VertxTestContext testContext) {
157167
router
158168
.get("/test")
159169
.handler(
160-
ValidationHandler.builder(parser).build()
170+
ValidationHandler.builder(schemaRepo).build()
161171
).handler(rc -> {
162172
rc.setUser(User.fromName("slinkydeveloper")); // Put user mock into context
163173
rc.next();
@@ -183,7 +193,7 @@ public void extraPayloadTest(Vertx vertx, VertxTestContext testContext) {
183193
router
184194
.get("/test")
185195
.handler(
186-
ValidationHandler.builder(parser).build()
196+
ValidationHandler.builder(schemaRepo).build()
187197
).handler(
188198
RouteToEBServiceHandler
189199
.build(vertx.eventBus(), "someAddress", "extraPayload")
@@ -210,7 +220,7 @@ public void serviceProxyManualFailureTest(Vertx vertx, VertxTestContext testCont
210220
.post("/testFailure")
211221
.handler(BodyHandler.create())
212222
.handler(
213-
ValidationHandler.builder(parser)
223+
ValidationHandler.builder(schemaRepo)
214224
.body(json(
215225
objectSchema()
216226
.requiredProperty("hello", stringSchema())
@@ -227,7 +237,7 @@ public void serviceProxyManualFailureTest(Vertx vertx, VertxTestContext testCont
227237
.post("/testException")
228238
.handler(BodyHandler.create())
229239
.handler(
230-
ValidationHandler.builder(parser)
240+
ValidationHandler.builder(schemaRepo)
231241
.body(json(
232242
objectSchema()
233243
.requiredProperty("hello", stringSchema())
@@ -262,14 +272,14 @@ public void binaryDataTest(Vertx vertx, VertxTestContext testContext) {
262272
.get("/test")
263273
.handler(BodyHandler.create())
264274
.handler(
265-
ValidationHandler.builder(parser).build()
275+
ValidationHandler.builder(schemaRepo).build()
266276
).handler(
267277
RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "binaryTest")
268278
);
269279

270280
testRequest(client, HttpMethod.GET, "/test")
271281
.expect(statusCode(200), statusMessage("OK"))
272-
.expect(bodyResponse(Buffer.buffer(new byte[] {(byte) 0xb0}), "application/octet-stream"))
282+
.expect(bodyResponse(Buffer.buffer(new byte[]{(byte) 0xb0}), "application/octet-stream"))
273283
.send(testContext, checkpoint);
274284
}
275285

@@ -284,7 +294,7 @@ public void authorizationPropagationTest(Vertx vertx, VertxTestContext testConte
284294
router
285295
.get("/test")
286296
.handler(
287-
ValidationHandler.builder(parser).build()
297+
ValidationHandler.builder(schemaRepo).build()
288298
).handler(rc -> {
289299
// patch the request to include authorization header
290300
rc.request().headers().add(HttpHeaders.AUTHORIZATION, "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");

vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/futures/RouteToEBServiceFuturesHandlerTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
import org.junit.jupiter.api.extension.ExtendWith;
1818

1919
import static io.vertx.ext.web.validation.builder.Parameters.param;
20-
import static io.vertx.ext.web.validation.testutils.TestRequest.*;
20+
import static io.vertx.ext.web.validation.testutils.TestRequest.jsonBodyResponse;
21+
import static io.vertx.ext.web.validation.testutils.TestRequest.statusCode;
22+
import static io.vertx.ext.web.validation.testutils.TestRequest.statusMessage;
23+
import static io.vertx.ext.web.validation.testutils.TestRequest.testRequest;
2124
import static io.vertx.json.schema.draft7.dsl.Schemas.intSchema;
2225

2326
@SuppressWarnings("unchecked")
@@ -42,7 +45,7 @@ public void serviceProxyTypedTestWithRequestParameter(final Vertx vertx, final V
4245
router
4346
.post("/testFutureWithRequestParameter/:param")
4447
.handler(BodyHandler.create())
45-
.handler(ValidationHandler.builder(parser).pathParameter(param("param", intSchema())).build())
48+
.handler(ValidationHandler.builder(schemaRepo).pathParameter(param("param", intSchema())).build())
4649
.handler(
4750
RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testFutureWithRequestParameter"));
4851

@@ -63,7 +66,7 @@ public void serviceProxyTypedTestWithIntParameter(final Vertx vertx, final Vertx
6366
router
6467
.post("/testFutureWithIntParameter/:param")
6568
.handler(BodyHandler.create())
66-
.handler(ValidationHandler.builder(parser).pathParameter(param("param", intSchema())).build())
69+
.handler(ValidationHandler.builder(schemaRepo).pathParameter(param("param", intSchema())).build())
6770
.handler(
6871
RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testFutureWithIntParameter"));
6972

@@ -84,7 +87,7 @@ public void serviceProxyTypedTest(final Vertx vertx, final VertxTestContext test
8487
router
8588
.post("/testFuture")
8689
.handler(BodyHandler.create())
87-
.handler(ValidationHandler.builder(parser).build())
90+
.handler(ValidationHandler.builder(schemaRepo).build())
8891
.handler(
8992
RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testFuture"));
9093

vertx-web-validation/src/main/asciidoc/index.adoc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,21 @@ Vert.x Web Validation provides an easy to use API to build an handler that perfo
5757
== Creating the `ValidationHandler`
5858

5959
This module provides an easy to use builder API to create your {@link io.vertx.ext.web.validation.ValidationHandler}, the {@link io.vertx.core.Handler} that performs the parsing and validation of the request.
60-
To create this builder use {@link io.vertx.ext.web.validation.builder.ValidationHandlerBuilder#create(SchemaParser)}.
61-
The provided {@link io.vertx.json.schema.SchemaParser} will be used to parse all schemas created with https://vertx.io/docs/vertx-json-schema/$lang/[Vert.x Json Schema DSL]
60+
To create this builder use {@link io.vertx.ext.web.validation.builder.ValidationHandlerBuilder#create(SchemaRepository)}.
6261

6362
=== Defining parameters
6463

6564
You can define parameters located in four different locations of your request: query, cookie, header, path.
6665

67-
Every parameter is represented by a {@link io.vertx.ext.web.validation.impl.parameter.ParameterProcessor},
68-
that you can easily create with methods provided in {@link io.vertx.ext.web.validation.builder.Parameters}:
66+
Every parameter is represented by a {@link io.vertx.ext.web.validation.impl.parameter.ParameterProcessor}, that you can easily create with methods provided in {@link io.vertx.ext.web.validation.builder.Parameters}:
6967

7068
[source,$lang]
7169
----
7270
{@link examples.WebValidationExamples#parameters}
7371
----
7472

75-
Note that all these methods requires a schema that validator can use to perform the validation. The schema is also used to infer the correct parser
73+
Note that all these methods requires a schema that validator can use to perform the validation.
74+
The schema is also used to infer the correct parser
7675

7776
While header and path parameters allows only simple parameters, query and cookie allows complex parameters like exploded and deep object:
7877

@@ -85,8 +84,7 @@ For more info on all available parameters, look at {@link io.vertx.ext.web.valid
8584

8685
=== Defining request bodies
8786

88-
Every body type is represented by a {@link io.vertx.ext.web.validation.impl.parameter.ParameterProcessor}
89-
and matches with request body using `Content-type` header.
87+
Every body type is represented by a {@link io.vertx.ext.web.validation.impl.parameter.ParameterProcessor} and matches with request body using `Content-type` header.
9088
You can define one or multiple bodies that the `ValidationHandler` should manage.
9189
If no matching body processor is found, the validation **won't** fail unless you specified the body required predicate explained below
9290

@@ -98,8 +96,8 @@ You can easily create these processor with methods provided in {@link io.vertx.e
9896
----
9997

10098
In this example the `ValidationHandler` will be able to manage two different body types that consistently parse and validate.
101-
In particular the form body will be converted to a json object. When you retrieve the parsed result, you don't need to care
102-
if the request body was a form or a json
99+
In particular the form body will be converted to a json object.
100+
When you retrieve the parsed result, you don't need to care if the request body was a form or a json
103101

104102
For more info on all available body processors, look at {@link io.vertx.ext.web.validation.builder.Bodies} documentation.
105103

@@ -133,8 +131,7 @@ The `ValidationHandler` will place the parsed values into {@link io.vertx.ext.we
133131

134132
== Manage the failures
135133

136-
Every time a `ValidationHandler` encounters both a parsing or a validation failure, it fails the `RoutingContext` with 400 status code and
137-
an instance of a subclass of {@link io.vertx.ext.web.validation.BadRequestException} as cause.
134+
Every time a `ValidationHandler` encounters both a parsing or a validation failure, it fails the `RoutingContext` with 400 status code and an instance of a subclass of {@link io.vertx.ext.web.validation.BadRequestException} as cause.
138135
To learn how to manage failures, look at https://vertx.io/docs/vertx-web/java/#_error_handling[Vert.x Web doc] and {@link io.vertx.ext.web.Router#errorHandler(int,Handler)} method.
139136

140137
The possible subclasses of {@link io.vertx.ext.web.validation.BadRequestException} are:

0 commit comments

Comments
 (0)