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 89764293a..a7ed4de63 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 @@ -18,6 +18,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.function.Supplier; import java.util.stream.Stream; import org.springframework.context.MessageSourceResolvable; @@ -56,16 +57,19 @@ public Map findTemplates(RepresentationModel resour Map templates = new HashMap<>(); Link selfLink = resource.getLink(IanaLinkRelations.SELF).orElse(null); - resource.getLinks().stream() // + Supplier> streamOfAffordances = () -> resource.getLinks().stream() // .flatMap(it -> it.getAffordances().stream()) // .map(it -> it.getAffordanceModel(MediaTypes.HAL_FORMS_JSON)) // .peek(it -> { Assert.notNull(it, "No HAL Forms affordance model found but expected!"); }) // - .map(HalFormsAffordanceModel.class::cast) // - .filter(it -> !it.hasHttpMethod(HttpMethod.GET)) // + .map(HalFormsAffordanceModel.class::cast) + .filter(it -> !it.hasHttpMethod(HttpMethod.GET)); + + long numberOfAffordances = streamOfAffordances.get().count(); + + streamOfAffordances.get() .forEach(it -> { - HalFormsTemplate template = HalFormsTemplate.forMethod(it.getHttpMethod()) // .withProperties(factory.createProperties(it)) .withContentType(it.getInput().getPrimaryMediaType()); @@ -75,11 +79,11 @@ public Map findTemplates(RepresentationModel resour if (selfLink == null || !target.equals(selfLink.getHref())) { template = template.withTarget(target); } - template = applyTo(template, TemplateTitle.of(it, templates.isEmpty())); - templates.put(templates.isEmpty() ? "default" : it.getName(), template); + templates.put(templates.isEmpty() ? + (numberOfAffordances == 1 ? "default" : it.getName()) : it.getName(), template); }); - + return templates; } diff --git a/src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsWebMvcIntegrationTest.java b/src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsWebMvcIntegrationTest.java index a5814db39..6e6421e51 100644 --- a/src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsWebMvcIntegrationTest.java +++ b/src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsWebMvcIntegrationTest.java @@ -76,11 +76,11 @@ void singleEmployee() throws Exception { .andExpect(jsonPath("$._links['employees'].href", is("http://localhost/employees"))) .andExpect(jsonPath("$._templates.*", hasSize(2))) - .andExpect(jsonPath("$._templates['default'].method", is("put"))) - .andExpect(jsonPath("$._templates['default'].properties[0].name", is("name"))) - .andExpect(jsonPath("$._templates['default'].properties[0].required").value(true)) - .andExpect(jsonPath("$._templates['default'].properties[1].name", is("role"))) - .andExpect(jsonPath("$._templates['default'].properties[1].required").doesNotExist()) + .andExpect(jsonPath("$._templates['updateEmployee'].method", is("put"))) + .andExpect(jsonPath("$._templates['updateEmployee'].properties[0].name", is("name"))) + .andExpect(jsonPath("$._templates['updateEmployee'].properties[0].required").value(true)) + .andExpect(jsonPath("$._templates['updateEmployee'].properties[1].name", is("role"))) + .andExpect(jsonPath("$._templates['updateEmployee'].properties[1].required").doesNotExist()) .andExpect(jsonPath("$._templates['partiallyUpdateEmployee'].method", is("patch"))) .andExpect(jsonPath("$._templates['partiallyUpdateEmployee'].properties[0].name", is("name")))