assertThat(response.stream().outputAsString()).isEqualTo("{\"errors\":[{\"msg\":\"The 'message' parameter is missing\"}]}");
}
+ @Test
+ public void required_parameter_is_not_set_but_not_called_as_mandatory() {
+ ValidatingRequest request = new TestRequest().setPath("/api/system/test-required-parameter");
+
+ DumbResponse response = new DumbResponse();
+ underTest.execute(request, response);
+
+ assertThat(response.stream().outputAsString()).isEqualTo("{\"errors\":[{\"msg\":\"The 'required-parameter' parameter is missing\"}]}");
+ }
+
@Test
public void optional_parameter_is_not_set() {
ValidatingRequest request = new TestRequest().setMethod("GET").setPath("/api/system/print").setParam("message", "Hello World");
}
});
+ // parameter "required-parameter" is required
+ NewAction helloWorld = createNewDefaultAction(newController, "test-required-parameter");
+ helloWorld.createParam("required-parameter").setDescription("required message").setRequired(true);
+ helloWorld.setHandler((request, response) -> {
+ String param = request.param("required-parameter");
+ });
+
createNewDefaultAction(newController, "fail_with_client_abort_exception")
.setHandler((request, response) -> {
throw new IllegalStateException("fail!", new ClientAbortException());
@CheckForNull
public String param(String key) {
WebService.Param definition = action.param(key);
- String valueOrDefault = defaultString(readParam(key, definition), definition.defaultValue());
- String value = valueOrDefault == null ? null : CharMatcher.WHITESPACE.trimFrom(valueOrDefault);
+
+ String rawValue = readParam(key, definition);
+ String rawValueOrDefault = defaultString(rawValue, definition.defaultValue());
+ String value = rawValueOrDefault == null ? null : CharMatcher.WHITESPACE.trimFrom(rawValueOrDefault);
+ validateRequiredValue(key, definition, rawValue);
if (value == null) {
return null;
}
validatePossibleValues(key, value, definition);
- validateMaximumLength(key, definition, valueOrDefault);
- validateMinimumLength(key, definition, valueOrDefault);
+ validateMaximumLength(key, definition, rawValueOrDefault);
+ validateMinimumLength(key, definition, rawValueOrDefault);
validateMaximumValue(key, definition, value);
return value;
}
checkArgument(valueAsInt <= maximumValue, "'%s' value (%s) must be less than %s", key, valueAsInt, maximumValue);
}
+ private static void validateRequiredValue(String key, WebService.Param definition, String value) {
+ boolean required = definition.isRequired();
+ if (required) {
+ checkArgument(value != null, format(MSG_PARAMETER_MISSING, key));
+ }
+ }
+
private static int validateAsNumeric(String key, String value) {
try {
return Integer.parseInt(value);