Skip to content

Commit

Permalink
#1114 - Provide HalFormsConfiguration API to order fields.
Browse files Browse the repository at this point in the history
Register various patterns of sorting based on types.
  • Loading branch information
gregturn committed Apr 30, 2020
1 parent a9527a3 commit c30ead9
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 15 deletions.
14 changes: 13 additions & 1 deletion src/main/java/org/springframework/hateoas/AffordanceModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ static InputPayloadMetadata from(PayloadMetadata metadata) {
* @return
*/
List<String> getI18nCodes();

Optional<ResolvableType> getType();
}

/**
Expand Down Expand Up @@ -210,7 +212,17 @@ public <T extends Named> T customize(T target, Function<PropertyMetadata, T> cus
public List<String> getI18nCodes() {
return Collections.emptyList();
}
}

@Override
public Optional<ResolvableType> getType() {

if (metadata instanceof InputPayloadMetadata) {
((InputPayloadMetadata) metadata).getType();
}

return Optional.empty();
}
}

/**
* Metadata about the property model of a representation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,10 @@ private String getNameOrDefault() {

String name = method.toString().toLowerCase();

ResolvableType type = TypeBasedPayloadMetadata.class.isInstance(inputMetdata) //
? TypeBasedPayloadMetadata.class.cast(inputMetdata).getType() //
: null;

if (type == null) {
return name;
}

Class<?> resolvedType = type.resolve();

return resolvedType == null ? name : name.concat(resolvedType.getSimpleName());
return inputMetdata.getType() //
.map(ResolvableType::resolve) //
.map(resolvedType -> resolvedType == null ? name : name.concat(resolvedType.getSimpleName())) //
.orElse(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Function;
Expand All @@ -39,7 +40,7 @@
*/
class TypeBasedPayloadMetadata implements InputPayloadMetadata {

private final @Getter(AccessLevel.PACKAGE) ResolvableType type;
private final ResolvableType type;
private final SortedMap<String, PropertyMetadata> properties;

TypeBasedPayloadMetadata(ResolvableType type, Stream<PropertyMetadata> properties) {
Expand Down Expand Up @@ -93,4 +94,9 @@ public List<String> getI18nCodes() {

return Arrays.asList(type.getName(), type.getSimpleName());
}

@Override
public Optional<ResolvableType> getType() {
return Optional.ofNullable(this.type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -36,6 +38,7 @@ public class HalFormsConfiguration {

private final @Getter HalConfiguration halConfiguration;
private final Map<Class<?>, String> patterns = new HashMap<>();
private final Map<Class<?>, List<String>> fieldOrder = new HashMap<>();

/**
* Creates a new {@link HalFormsConfiguration} backed by a default {@link HalConfiguration}.
Expand All @@ -60,4 +63,15 @@ public HalFormsConfiguration registerPattern(Class<?> type, String pattern) {
Optional<String> getTypePatternFor(ResolvableType type) {
return Optional.ofNullable(patterns.get(type.resolve(Object.class)));
}

public HalFormsConfiguration withFieldOrderFor(Class<?> type, String... fieldNames) {

this.fieldOrder.put(type, Arrays.asList(fieldNames));

return this;
}

Optional<List<String>> getFieldOrderFor(ResolvableType type) {
return Optional.ofNullable(fieldOrder.get(type.resolve(Object.class)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public Map<String, HalFormsTemplate> findTemplates(RepresentationModel<?> resour
.map(property -> it.hasHttpMethod(HttpMethod.PATCH) ? property.withRequired(false) : property)
.collect(Collectors.toList());

propertiesWithPrompt = sorted(propertiesWithPrompt, it.getInput());

HalFormsTemplate template = HalFormsTemplate.forMethod(it.getHttpMethod()) //
.withProperties(propertiesWithPrompt);

Expand All @@ -89,6 +91,32 @@ public Map<String, HalFormsTemplate> findTemplates(RepresentationModel<?> resour
return templates;
}

private List<HalFormsProperty> sorted(List<HalFormsProperty> properties, InputPayloadMetadata input) {

return input.getType() //
.flatMap(configuration::getFieldOrderFor) //
.map(fieldsToSortBy -> {

List<HalFormsProperty> propertiesToSort = new ArrayList<>(properties);
List<HalFormsProperty> sortedProperties = new ArrayList<>();

for (String propertyName : fieldsToSortBy) {
properties.stream() //
.filter(halFormsProperty -> halFormsProperty.getName().equals(propertyName)).findFirst()
.ifPresent(halFormsProperty -> {
sortedProperties.add(halFormsProperty);
propertiesToSort.remove(halFormsProperty);
});
}

// Whatever properties weren't listed, add them at the end.
sortedProperties.addAll(propertiesToSort);

return sortedProperties;
}) //
.orElse(properties);
}

public PropertyCustomizations forMetadata(InputPayloadMetadata metadata) {
return new PropertyCustomizations(metadata);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.assertj.core.api.Assertions.*;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;

Expand Down Expand Up @@ -113,8 +114,8 @@ public PayloadMetadataAssert(PayloadMetadata actual) {

public PayloadMetadataAssert isBackedBy(Class<?> type) {

Assertions.assertThat(actual).isInstanceOfSatisfying(TypeBasedPayloadMetadata.class, it -> {
Assertions.assertThat(it.getType()).isEqualTo(ResolvableType.forClass(type));
assertThat(actual).isInstanceOfSatisfying(TypeBasedPayloadMetadata.class, it -> {
assertThat(it.getType()).isEqualTo(Optional.of(ResolvableType.forClass(type)));
});

return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.springframework.hateoas.mediatype.hal.forms;

import lombok.Data;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.hateoas.mediatype.Affordances;
import org.springframework.hateoas.mediatype.MessageResolver;
import org.springframework.http.HttpMethod;

import static org.assertj.core.api.Assertions.*;

public class HalFormsPropertyOrderingUnitTest {

private RepresentationModel<?> model;

@BeforeEach
void setUp() {

this.model = new RepresentationModel<>();

this.model.add(Affordances.of(Link.of("/example")) //
.afford(HttpMethod.POST) //
.withInput(Thing.class) //
.toLink());
}

@Test
void noCustomOrdering() {

HalFormsConfiguration halFormsConfiguration = new HalFormsConfiguration();

assertThat(createTemplate(halFormsConfiguration).getProperties()).flatExtracting(HalFormsProperty::getName)
.containsExactly("a", "b", "z");

}

@Test
void specifyAllProperties() {

HalFormsConfiguration halFormsConfiguration = new HalFormsConfiguration() //
.withFieldOrderFor(Thing.class, "z", "b", "a");

assertThat(createTemplate(halFormsConfiguration).getProperties()).flatExtracting(HalFormsProperty::getName)
.containsExactly("z", "b", "a");
}

@Test
void specifySomeProperties() {

HalFormsConfiguration halFormsConfiguration = new HalFormsConfiguration() //
.withFieldOrderFor(Thing.class, "z");

assertThat(createTemplate(halFormsConfiguration).getProperties()).flatExtracting(HalFormsProperty::getName)
.containsExactly("z", "a", "b");
}

private HalFormsTemplate createTemplate(HalFormsConfiguration halFormsConfiguration) {
return new HalFormsTemplateBuilder(halFormsConfiguration, MessageResolver.DEFAULTS_ONLY).findTemplates(this.model)
.get("default");
}

@Data
private static class Thing {

private String a;
private String b;
private String z;
}
}

0 comments on commit c30ead9

Please sign in to comment.