From 3d7458b5f6912c6ccd553cda8345ca6c62826506 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 12 Oct 2021 13:52:34 +0200 Subject: [PATCH] #1662 - HAL FORMS template target must be a URL. We now expand the affordance link before we add it to the HAL FORMS template. Also see #1660. --- .../hal/forms/HalFormsTemplateBuilder.java | 2 +- .../forms/HalFormsTemplateBuilderUnitTest.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsTemplateBuilder.java b/src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsTemplateBuilder.java index 89764293..d08eb5c4 100644 --- a/src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsTemplateBuilder.java +++ b/src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsTemplateBuilder.java @@ -70,7 +70,7 @@ class HalFormsTemplateBuilder { .withProperties(factory.createProperties(it)) .withContentType(it.getInput().getPrimaryMediaType()); - String target = it.getLink().getHref(); + String target = it.getLink().expand().getHref(); if (selfLink == null || !target.equals(selfLink.getHref())) { template = template.withTarget(target); diff --git a/src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsTemplateBuilderUnitTest.java b/src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsTemplateBuilderUnitTest.java index 48c78e08..48f8c138 100644 --- a/src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsTemplateBuilderUnitTest.java +++ b/src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsTemplateBuilderUnitTest.java @@ -230,6 +230,22 @@ class HalFormsTemplateBuilderUnitTest { assertThat(HalFormsTemplate.forMethod(HttpMethod.POST).getMethod()).isEqualTo("POST"); } + @Test + void expandsAffordanceLinkForFormTarget() { + + HalFormsConfiguration configuration = new HalFormsConfiguration(); + + RepresentationModel models = new RepresentationModel<>( + Affordances.of(Link.of("/example{?foo}", LinkRelation.of("example"))) // + .afford(HttpMethod.POST) // + .toLink()); + + Map templates = new HalFormsTemplateBuilder(configuration, MessageResolver.DEFAULTS_ONLY) + .findTemplates(models); + + assertThat(templates.get("default").getTarget()).endsWith("/example"); + } + @Getter static class PatternExample extends RepresentationModel {