Support for HAL-FORMS value element#2039
Open
reda-alaoui wants to merge 1 commit intospring-projects:mainfrom
Open
Support for HAL-FORMS value element#2039reda-alaoui wants to merge 1 commit intospring-projects:mainfrom
reda-alaoui wants to merge 1 commit intospring-projects:mainfrom
Conversation
5828e78 to
e643c37
Compare
|
Regarding the first considered alternative (Retrieve the value directly from the payload instance), maybe using public class Payload {
// constructor, getters, setters
@NotNull(groups = Submitted.class)
private String foo;
@NotNull(groups = Submitted.class)
private String bar;
}
public class MyController {
@GetMapping
public ResponseEntity<?> list() {
Link selfLink = selfLink.andAffordance(afford(methodOn(MyController.class).create(new Payload("hello", null))));
return ResponseEntity.ok(new RepresentationModel<>(selfLink));
}
@PostMapping
public ResponseEntity<?> create(@RequestBody @Validated(Submitted.class) Payload payload) {
return ResponseEntity.created().build();
}
} |
Contributor
Author
|
@kalgon , when I look at this, I see an annotation hell ^^ |
Contributor
Author
|
@odrotbohm, could we have some feedback on this? I'd need the same thing for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1717
Behaviour before this change
Let be:
Calling GET will return something like:
{ "_templates": { "default": { "properties": [ { "name": "foo" }, { "name": "bar" } ] } } }We want to be able to assign a default value
helloto attributefooin theHAL-FORMSpayload to obtain something like this:{ "_templates": { "default": { "properties": [ { "name": "foo", "value": "hello" }, { "name": "bar" } ] } } }Solution brought by this change
The implemented solution is heavily inspired from
HalFormsOptionsFactory. A consumer can provide a value creator, taking a property metadata as input and returning a value of typeStringas output.Considered alternatives
Retrieve the value directly from the payload instance
To do that, this kind of consumer code would be needed:
baris verified for non-nullity inPayloadconstructor.On the 2nd point, some may argue that
jakarta.validation.constraints.NotNullshould be used instead of the in-house constructor validation. On that my opinion is as follow:@NotNullshould be avoided when plain simple java code is able to enforce the same constraintnullvalue assigned to a@NotNullproperty, at worst will fail the compilationRetrieve the value from a new annotation on the considered property
This would force the consumer to know the value before compilation.