spring-projects / spring-hateoas

Spring HATEOAS - Library to support implementing representations for hyper-text driven REST web services.
https://spring.io/projects/spring-hateoas
Apache License 2.0
1.04k stars 477 forks source link

HalForm property value not set #1717

Open ecattez opened 2 years ago

ecattez commented 2 years ago

For the example, I've got products with product references.

Here's the code to set "select-product" relation.

private static ProductDto withSelectProductAffordance(ProductDto productDto) {
        SelectProductRequest request = SelectProductRequest.builder()
                .reference(productDto.getReference())
                .build();

        Link selectProductLink = linkTo(methodOn(SelectProductResource.class)
                .selectProduct(request))
                .withRel(ProductCatalogRelations.SELECT_PRODUCT);

        Link affordance = selectProductLink
                .andAffordance(afford(methodOn(SelectProductResource.class)
                        .selectProduct(request)));

        return productDto.add(affordance);
    }

Here's the current result of the collection of products:

{
  "_embedded": {
    "products": [
      {
        "reference": "product-001",
        "_links": {
          "select-product": {
            "href": "http://localhost:8080/api/select-product"
          }
        },
        "_templates": {
          "default": { ... },
          "selectProduct": {
            "method": "post",
            "properties": [
              {
                "name": "reference",
                "required": true,
                "type": "text"
              }
            ],
            "target": "http://localhost:8080/api/select-product"
          }
        }
      },
      { ... }
    ]
  }
}

I would expect to have product reference already set in selectProduct template like below:

"properties": [
  {
    "name": "reference",
    "required": true,
    "type": "text",
    "value": "product-001"
  }
]

It seems that value does not exist in AffordanceModel#PropertyMetadata but is a valid in the spec: https://rwcbook.github.io/hal-forms/#_code_value_code

I would also expect the template name to be the same as the link rel.

Ickbinet commented 2 years ago

"I would also expect the template name to be the same as the link rel."

A link can have more than one template associated. The association is defined via the templates target or if no target is set via the self link.

"value": "product-001"

property.value is only set for options with a selected value, which is imho a bug. At least default values should be set.

What is your idea for an algo to detect that the value should be "product-001"?

ecattez commented 2 years ago

I think it should take care of the request body, in my case SelectProductRequest. As I fulfilled explicitely the methodOn(...) with the request body, it should use the specified value.

It works like a charm with query params and path variables. So it should works with request body.