Skip to content

Commit

Permalink
Allow custom serdes to override default ones (#1421). (#2103)
Browse files Browse the repository at this point in the history
- @ElideTypeConverter annotation removed from a bunch of default serdes
- updated serdes added to the ElideSettingsBuilder
- registering of the 'isoDate' object mapper module has been removed from a JsonApiMapper constructor
in favor of registering it in a common way with all the others serdes during Elide instantiating
- serde scalars registering reworked so they are not depend on having @ElideTypeConverter annotation

Co-authored-by: Pavlo Kolesnykov <[email protected]>
  • Loading branch information
edu-chk and Pavlo Kolesnykov authored May 18, 2021
1 parent 4f0ff5f commit 9facb56
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 119 deletions.
2 changes: 1 addition & 1 deletion elide-core/src/main/java/com/yahoo/elide/Elide.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Elide(ElideSettings elideSettings) {
this.mapper = elideSettings.getMapper();
this.transactionRegistry = new TransactionRegistry();

elideSettings.getSerdes().forEach(CoerceUtil::register);
elideSettings.getSerdes().forEach((type, serde) -> registerCustomSerde(type, serde, type.getSimpleName()));

registerCustomSerde();
}
Expand Down
16 changes: 16 additions & 0 deletions elide-core/src/main/java/com/yahoo/elide/ElideSettingsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@
import com.yahoo.elide.core.security.executors.VerbosePermissionExecutor;
import com.yahoo.elide.core.utils.coerce.converters.EpochToDateConverter;
import com.yahoo.elide.core.utils.coerce.converters.ISO8601DateSerde;
import com.yahoo.elide.core.utils.coerce.converters.InstantSerde;
import com.yahoo.elide.core.utils.coerce.converters.OffsetDateTimeSerde;
import com.yahoo.elide.core.utils.coerce.converters.Serde;
import com.yahoo.elide.core.utils.coerce.converters.TimeZoneSerde;
import com.yahoo.elide.core.utils.coerce.converters.URLSerde;
import com.yahoo.elide.jsonapi.JsonApiMapper;
import com.yahoo.elide.jsonapi.links.JSONApiLinks;

import java.net.URL;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -76,6 +83,7 @@ public ElideSettingsBuilder(DataStore dataStore) {

//By default, Elide supports epoch based dates.
this.withEpochDates();
this.withDefaultSerdes();
}

public ElideSettings build() {
Expand Down Expand Up @@ -196,6 +204,14 @@ public ElideSettingsBuilder withEpochDates() {
return this;
}

public ElideSettingsBuilder withDefaultSerdes() {
serdes.put(Instant.class, new InstantSerde());
serdes.put(OffsetDateTime.class, new OffsetDateTimeSerde());
serdes.put(TimeZone.class, new TimeZoneSerde());
serdes.put(URL.class, new URLSerde());
return this;
}

public ElideSettingsBuilder withJSONApiLinks(JSONApiLinks links) {
this.enableJsonLinks = true;
this.jsonApiLinks = links;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*
* Uses the semantics of {@link java.time.format.DateTimeFormatter#ISO_INSTANT}
*/
@ElideTypeConverter(type = Instant.class, name = "Instant")
public class InstantSerde implements Serde<String, Instant> {
@Override
public Instant deserialize(final String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
/**
* Serde class for bidirectional conversion from OffsetDateTime type to String.
*/
@ElideTypeConverter(type = OffsetDateTime.class, name = "OffsetDateTime")
public class OffsetDateTimeSerde implements Serde<String, OffsetDateTime> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
/**
* Serde class for bidirectional conversion from TimeZone type to String.
*/
@ElideTypeConverter(type = TimeZone.class, name = "TimeZone")
public class TimeZoneSerde implements Serde<String, TimeZone> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.net.MalformedURLException;
import java.net.URL;

@ElideTypeConverter(type = URL.class, name = "URL")
public class URLSerde implements Serde<String, URL> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@
*/
package com.yahoo.elide.jsonapi;

import com.yahoo.elide.core.utils.coerce.CoerceUtil;
import com.yahoo.elide.core.utils.coerce.converters.Serde;
import com.yahoo.elide.jsonapi.models.JsonApiDocument;
import com.yahoo.elide.jsonapi.models.Patch;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
Expand All @@ -34,23 +26,6 @@ public class JsonApiMapper {
*/
public JsonApiMapper() {
this.mapper = new ObjectMapper();

mapper.registerModule(
new SimpleModule("isoDate", Version.unknownVersion())
.addSerializer(Date.class, new JsonSerializer<Date>() {
@Override
public void serialize(Date date,
JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
Serde<?, Date> serde = CoerceUtil.lookup(Date.class);

jsonGenerator.writeObject(serde.serialize(date));
}
}
)
);

mapper.registerModule(JsonApiSerializer.getModule());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2021, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/

package com.yahoo.elide;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.yahoo.elide.core.datastore.DataStore;
import com.yahoo.elide.core.utils.coerce.converters.InstantSerde;
import com.yahoo.elide.core.utils.coerce.converters.OffsetDateTimeSerde;
import com.yahoo.elide.core.utils.coerce.converters.Serde;
import com.yahoo.elide.core.utils.coerce.converters.TimeZoneSerde;
import com.yahoo.elide.core.utils.coerce.converters.URLSerde;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import java.net.URL;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.Map;
import java.util.TimeZone;


class ElideSettingsBuilderTest {

private ElideSettingsBuilder testInstance;

@Mock
private DataStore dataStore;

@BeforeEach
public void setUp() {
testInstance = new ElideSettingsBuilder(dataStore);
}

@Test
@SuppressWarnings("rawtypes")
public void shouldBuildSettingsWithDefaultSerdes() {
Map<Class, Serde> serdes = testInstance.build().getSerdes();

assertEquals(InstantSerde.class, serdes.get(Instant.class).getClass());
assertEquals(OffsetDateTimeSerde.class, serdes.get(OffsetDateTime.class).getClass());
assertEquals(TimeZoneSerde.class, serdes.get(TimeZone.class).getClass());
assertEquals(URLSerde.class, serdes.get(URL.class).getClass());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.yahoo.elide.core.type.Type;
import com.yahoo.elide.core.utils.coerce.CoerceUtil;
import com.yahoo.elide.core.utils.coerce.converters.ElideTypeConverter;
import com.yahoo.elide.core.utils.coerce.converters.Serde;
import graphql.Scalars;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLArgument;
Expand Down Expand Up @@ -66,16 +65,13 @@ public GraphQLConversionUtils(EntityDictionary entityDictionary, NonEntityDictio
}

private void registerCustomScalars() {
for (Class serdeType : CoerceUtil.getSerdes().keySet()) {
Serde serde = CoerceUtil.lookup(serdeType);
ElideTypeConverter elideTypeConverter = serde.getClass()
.getAnnotation(ElideTypeConverter.class);
if (elideTypeConverter != null) {
SerdeCoercing serdeCoercing = new SerdeCoercing(ERROR_MESSAGE, serde);
scalarMap.put(ClassType.of(elideTypeConverter.type()), new GraphQLScalarType(elideTypeConverter.name(),
elideTypeConverter.description(), serdeCoercing));
}
}
CoerceUtil.getSerdes().forEach((type, serde) -> {
SerdeCoercing<?, ?> serdeCoercing = new SerdeCoercing<>(ERROR_MESSAGE, serde);
ElideTypeConverter elideTypeConverter = serde.getClass().getAnnotation(ElideTypeConverter.class);
String name = elideTypeConverter != null ? elideTypeConverter.name() : type.getSimpleName();
String description = elideTypeConverter != null ? elideTypeConverter.description() : type.getSimpleName();
scalarMap.put(ClassType.of(type), new GraphQLScalarType(name, description, serdeCoercing));
});
}

/**
Expand Down

0 comments on commit 9facb56

Please sign in to comment.