Skip to content
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
3 changes: 3 additions & 0 deletions core/src/main/java/com/cosium/hal_mock_mvc/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ private ValidatedFormProperty<?> validate(
FormProperty<?> property, PropertyValidationOption... validationOptions) throws Exception {
Set<PropertyValidationOption> validationOptionSet =
Optional.ofNullable(validationOptions).map(Set::of).orElse(Set.of());
if (validationOptionSet.contains(PropertyValidationOption.Immediate.DO_NOT_FAIL_IF_NOT_VALID)) {
return ValidatedFormProperty.markAsValid(property);
}
TemplatePropertyRepresentation representation =
template.representation().propertyByName().get(property.name());
if (representation == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ enum Immediate implements PropertyValidationOption {
/** Do not fail if writing to a property not declared by the template */
DO_NOT_FAIL_IF_NOT_DECLARED,
/** Do not fail if writing to a property declared as read-only by the template */
DO_NOT_FAIL_IF_DECLARED_READ_ONLY
DO_NOT_FAIL_IF_DECLARED_READ_ONLY,
/** Do not fail if writing invalid value to a property */
DO_NOT_FAIL_IF_NOT_VALID,
}
}
35 changes: 35 additions & 0 deletions core/src/test/java/com/cosium/hal_mock_mvc/FormTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.cosium.hal_mock_mvc.PropertyValidationOption.Immediate;
import com.fasterxml.jackson.jr.ob.JSON;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
Expand Down Expand Up @@ -1843,6 +1844,40 @@ void test39() throws Exception {
.doesNotThrowAnyException();
}

@Test
@DisplayName("User can force an invalid property")
void test40() throws Exception {
myController.getResponseToSend =
JSON.std
.composeString()
.startObject()
.startObjectField("_links")
.startObjectField("self")
.put("href", "http://localhost/form-test:put")
.end()
.end()
.startObjectField("_templates")
.startObjectField("default")
.put("method", "PUT")
.startArrayField("properties")
.end()
.end()
.end()
.end()
.finish();

Form form =
HalMockMvc.builder(mockMvc)
.baseUri(linkTo(methodOn(MyController.class).get()).toUri())
.build()
.follow()
.templates()
.byKey("default")
.createForm();
assertThatCode(() -> form.withString("foo", "foo", Immediate.DO_NOT_FAIL_IF_NOT_VALID))
.doesNotThrowAnyException();
}

@Controller
public static class MyController {

Expand Down