Skip to content

Allow extending HalFormsProperty behind HAL-FORMS specification #2345

@reda-alaoui

Description

@reda-alaoui

We'd like to add custom attributes to HalFormsProperty. This seems allowed by HAL-FORMS specification but it seems currently impossible with Spring HATEAOS.

IMO, the best extension point would be somewhere here:

return model.createProperties((payload, metadata) -> {
String inputType = metadata.getInputType();
HalFormsOptions options = optionsFactory.getOptions(payload, metadata);
HalFormsProperty property = new HalFormsProperty()
.withName(metadata.getName())
.withRequired(metadata.isRequired()) //
.withReadOnly(metadata.isReadOnly())
.withMin(metadata.getMin())
.withMax(metadata.getMax())
.withMinLength(metadata.getMinLength())
.withMaxLength(metadata.getMaxLength())
.withRegex(lookupRegex(metadata)) //
.withType(inputType) //
.withValue(options != null ? options.getSelectedValue() : null) //
.withOptions(options);
Function<String, I18nedPropertyMetadata> factory = I18nedPropertyMetadata.factory(payload, property);
return Optional.of(property)
.map(it -> i18n(it, factory.apply("_placeholder"), it::withPlaceholder))
.map(it -> i18n(it, factory.apply("_prompt"), it::withPrompt))
.map(it -> model.hasHttpMethod(HttpMethod.PATCH) ? it.withRequired(false) : it)
.orElse(property);
});

I guess that would mean adding a free Map to HalFormsProperty and a PropertyCustomizer SPI looking like this:

interface PropertyCustomizer {
  HalFormsProperty customize(HalFormsProperty property, InputPayloadMetadata payload, PropertyMetadata metadata, PropertyCreationContext context);
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions