Skip to content

Commit

Permalink
#1516 - Support for NamingBase in property translation for HAL.
Browse files Browse the repository at this point in the history
We now also support the newly NamingBase introduced in Jackson 2.12 as a replacement for PropertyNamingStrategyBase.
  • Loading branch information
odrotbohm committed Apr 14, 2021
1 parent e939852 commit 91e17f1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.PropertyNamingStrategies.NamingBase;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.PropertyNamingStrategyBase;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.std.ContainerDeserializerBase;
Expand Down Expand Up @@ -843,12 +844,13 @@ private EmbeddedMapper(LinkRelationProvider relProvider, CurieProvider curieProv
*/
public EmbeddedMapper with(@Nullable PropertyNamingStrategy strategy) {

if (!(strategy instanceof PropertyNamingStrategyBase)) {
return this;
}
Function<String, String> mapper = strategy instanceof PropertyNamingStrategyBase
? ((PropertyNamingStrategyBase) strategy)::translate
: strategy instanceof NamingBase ? ((NamingBase) strategy)::translate : null;

return new EmbeddedMapper(relProvider, curieProvider, preferCollectionRels,
((PropertyNamingStrategyBase) strategy)::translate);
return mapper == null
? this
: new EmbeddedMapper(relProvider, curieProvider, preferCollectionRels, mapper);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.TypeFactory;
Expand Down Expand Up @@ -643,6 +644,13 @@ void rendersLinksWhenMapEntrySortingIsEnabled() throws Exception {
.writeValueAsString(new RepresentationModel<>().add(Link.of("/href")));
}

@Test // #1516
void considersNamingBase() throws Exception {

mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
.writeValueAsString(new RepresentationModel<>().add(Link.of("/href", "fooBar")));
}

@Relation(collectionRelation = "someSample")
static class SomeSample {
@JsonProperty String name;
Expand Down

0 comments on commit 91e17f1

Please sign in to comment.