DATAREST-683 - Enum values are now correctly i18ned in ALPS.
If enum translation is enabled, ALPS descriptors now expose the translated values correctly.
This commit is contained in:
@@ -50,6 +50,7 @@ import org.springframework.data.rest.core.mapping.SimpleResourceDescription;
|
||||
import org.springframework.data.rest.core.mapping.SupportedHttpMethods;
|
||||
import org.springframework.data.rest.webmvc.ProfileController;
|
||||
import org.springframework.data.rest.webmvc.RootResourceInformation;
|
||||
import org.springframework.data.rest.webmvc.json.EnumTranslator;
|
||||
import org.springframework.data.rest.webmvc.json.JacksonMetadata;
|
||||
import org.springframework.data.rest.webmvc.mapping.AssociationLinks;
|
||||
import org.springframework.hateoas.EntityLinks;
|
||||
@@ -62,6 +63,7 @@ import org.springframework.hateoas.alps.Doc;
|
||||
import org.springframework.hateoas.alps.Format;
|
||||
import org.springframework.hateoas.alps.Type;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
|
||||
@@ -84,6 +86,7 @@ public class RootResourceInformationToAlpsDescriptorConverter {
|
||||
private final MessageSourceAccessor messageSource;
|
||||
private final RepositoryRestConfiguration configuration;
|
||||
private final ObjectMapper mapper;
|
||||
private final EnumTranslator translator;
|
||||
|
||||
/**
|
||||
* Creates a new {@link RootResourceInformationToAlpsDescriptorConverter} instance.
|
||||
@@ -95,10 +98,11 @@ public class RootResourceInformationToAlpsDescriptorConverter {
|
||||
* @param messageSource must not be {@literal null}.
|
||||
* @param configuration must not be {@literal null}.
|
||||
* @param mapper must not be {@literal null}.
|
||||
* @param translator must not be {@literal null}.
|
||||
*/
|
||||
public RootResourceInformationToAlpsDescriptorConverter(ResourceMappings mappings, Repositories repositories,
|
||||
PersistentEntities entities, EntityLinks entityLinks, MessageSourceAccessor messageSource,
|
||||
RepositoryRestConfiguration configuration, ObjectMapper mapper) {
|
||||
RepositoryRestConfiguration configuration, ObjectMapper mapper, EnumTranslator translator) {
|
||||
|
||||
this.mappings = mappings;
|
||||
this.persistentEntities = entities;
|
||||
@@ -107,6 +111,7 @@ public class RootResourceInformationToAlpsDescriptorConverter {
|
||||
this.messageSource = messageSource;
|
||||
this.configuration = configuration;
|
||||
this.mapper = mapper;
|
||||
this.translator = translator;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -332,7 +337,7 @@ public class RootResourceInformationToAlpsDescriptorConverter {
|
||||
descriptor(). //
|
||||
type(Type.SEMANTIC).//
|
||||
name(propertyDefinition.getName()).//
|
||||
doc(getDocFor(propertyMapping.getDescription())).//
|
||||
doc(getDocFor(propertyMapping.getDescription(), property)).//
|
||||
build());
|
||||
}
|
||||
}
|
||||
@@ -402,12 +407,25 @@ public class RootResourceInformationToAlpsDescriptorConverter {
|
||||
}
|
||||
|
||||
private Doc getDocFor(ResourceDescription description) {
|
||||
return getDocFor(description, null);
|
||||
}
|
||||
|
||||
private Doc getDocFor(ResourceDescription description, PersistentProperty<?> property) {
|
||||
|
||||
if (description == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String message = resolveMessage(description);
|
||||
|
||||
// Manually post process the default message for enumerations if needed
|
||||
if (configuration.isEnableEnumTranslation() && property != null && property.getType().isEnum()) {
|
||||
if (description.isDefault()) {
|
||||
return new Doc(StringUtils.collectionToDelimitedString(
|
||||
translator.getValues((Class<? extends Enum<?>>) property.getType()), ", "), Format.TEXT);
|
||||
}
|
||||
}
|
||||
|
||||
return message == null ? null : new Doc(message, Format.TEXT);
|
||||
}
|
||||
|
||||
|
||||
@@ -781,7 +781,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
|
||||
ResourceMappings resourceMappings = resourceMappings();
|
||||
|
||||
return new RootResourceInformationToAlpsDescriptorConverter(resourceMappings, repositories, persistentEntities,
|
||||
entityLinks, messageSourceAccessor, config, objectMapper());
|
||||
entityLinks, messageSourceAccessor, config, objectMapper(), enumTranslator());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -17,12 +17,11 @@ package org.springframework.data.rest.webmvc.alps;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -57,6 +56,7 @@ public class AlpsControllerIntegrationTests extends AbstractControllerIntegratio
|
||||
|
||||
@Autowired WebApplicationContext context;
|
||||
@Autowired LinkDiscoverers discoverers;
|
||||
@Autowired RepositoryRestConfiguration configuration;
|
||||
|
||||
@Configuration
|
||||
static class Config extends RepositoryRestConfigurerAdapter {
|
||||
@@ -82,6 +82,11 @@ public class AlpsControllerIntegrationTests extends AbstractControllerIntegratio
|
||||
this.client = new TestMvcClient(mvc, this.discoverers);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
configuration.setEnableEnumTranslation(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-230
|
||||
*/
|
||||
@@ -177,4 +182,21 @@ public class AlpsControllerIntegrationTests extends AbstractControllerIntegratio
|
||||
// Exposes identifier if configured to
|
||||
.andExpect(jsonPath("$.alps.descriptors[*].descriptors[*].name", hasItems("id", "name")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-683
|
||||
*/
|
||||
@Test
|
||||
public void enumValueListingsAreTranslatedIfEnabled() throws Exception {
|
||||
|
||||
configuration.setEnableEnumTranslation(true);
|
||||
|
||||
Link profileLink = client.discoverUnique("profile");
|
||||
Link peopleLink = client.discoverUnique(profileLink, "people", MediaType.ALL);
|
||||
|
||||
client.follow(peopleLink)//
|
||||
.andExpect(jsonPath(
|
||||
"$.alps.descriptors[?(@.id == 'person-representation')].descriptors[?(@.name == 'gender')][0].doc.value",
|
||||
is("Male, Female, Undefined")));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user