eclipse-vertx/vertx-openapi

Extend RequestParameter with getters that supports default values

pk-work opened this issue · 2 comments

If you have e.g. a query parameter that is optional and not part of the request, the RequestValidator will create a new RequestParameterImpl(null) and stores it in the ValidatedRequest.

If I now want to access the value I have always to do a null check:

RequestParameter myList = validatedRequest.getQuery().get("myList");
List<String> list = myList.isNull() ? List.of() : myList .getJsonArray().getList();

I'd like to have convenience methods, like #getJsonArray(JsonArray default)

RequestParameter myList = validatedRequest.getQuery().get("myList");
List<String> list = myList.getJsonArray(new JsonArray()).getList();

What do you think?

thced commented

Maybe support Supplier, for lazy behavior?

Hey @pk-work / @thced Happy new years! I saw this issue and it seemed pretty simple to me so I took a stab at it. I added convenience functions that take in a Supplier so if the value is null, it will return that default value instead. If you want to take a look! #52