Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add lazy fetch for ObjectMapper in the json-ld context #4737

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void setupModule(SetupContext context) {
}
};
mapper.registerModule(module);
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be part of a separate PR for the sake of clarity? It could also deserve a test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already present in the code base, I just move it here centrally.
A test make sense 👍

return mapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.Set;
import java.util.function.Supplier;

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA;
Expand All @@ -41,16 +42,16 @@ public class JsonObjectFromDataAddressDspaceTransformer extends AbstractNamespac

private static final Set<String> EXCLUDED_PROPERTIES = Set.of(EDC_DATA_ADDRESS_TYPE_PROPERTY);
private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;
private final Supplier<ObjectMapper> objectMapperSupplier;

public JsonObjectFromDataAddressDspaceTransformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper) {
this(jsonFactory, mapper, new JsonLdNamespace(DSPACE_SCHEMA));
public JsonObjectFromDataAddressDspaceTransformer(JsonBuilderFactory jsonFactory, Supplier<ObjectMapper> objectMapperSupplier) {
this(jsonFactory, objectMapperSupplier, new JsonLdNamespace(DSPACE_SCHEMA));
}

public JsonObjectFromDataAddressDspaceTransformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper, JsonLdNamespace namespace) {
public JsonObjectFromDataAddressDspaceTransformer(JsonBuilderFactory jsonFactory, Supplier<ObjectMapper> objectMapperSupplier, JsonLdNamespace namespace) {
super(DataAddress.class, JsonObject.class, namespace);
this.jsonFactory = jsonFactory;
this.mapper = mapper;
this.objectMapperSupplier = objectMapperSupplier;
}

@Override
Expand All @@ -75,7 +76,7 @@ private JsonObject endpointProperty(String key, Object value) {
if (value instanceof String stringVal) {
builder.add(forNamespace(ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM), stringVal);
} else {
builder.add(forNamespace(ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM), mapper.convertValue(value, JsonObject.class));
builder.add(forNamespace(ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM), objectMapperSupplier.get().convertValue(value, JsonObject.class));
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.Set;
import java.util.function.Supplier;

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.Namespaces.DSPACE_SCHEMA_2024_1;
Expand All @@ -41,16 +42,16 @@ public class JsonObjectFromDataAddressDspace2024Transformer extends AbstractName

private static final Set<String> EXCLUDED_PROPERTIES = Set.of(EDC_DATA_ADDRESS_TYPE_PROPERTY);
private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;
private final Supplier<ObjectMapper> objectMapperSupplier;

public JsonObjectFromDataAddressDspace2024Transformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper) {
this(jsonFactory, mapper, new JsonLdNamespace(DSPACE_SCHEMA_2024_1));
public JsonObjectFromDataAddressDspace2024Transformer(JsonBuilderFactory jsonFactory, Supplier<ObjectMapper> objectMapperSupplier) {
this(jsonFactory, objectMapperSupplier, new JsonLdNamespace(DSPACE_SCHEMA_2024_1));
}

public JsonObjectFromDataAddressDspace2024Transformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper, JsonLdNamespace namespace) {
public JsonObjectFromDataAddressDspace2024Transformer(JsonBuilderFactory jsonFactory, Supplier<ObjectMapper> objectMapperSupplier, JsonLdNamespace namespace) {
super(DataAddress.class, JsonObject.class, namespace);
this.jsonFactory = jsonFactory;
this.mapper = mapper;
this.objectMapperSupplier = objectMapperSupplier;
}

@Override
Expand All @@ -75,7 +76,7 @@ private JsonObject endpointProperty(String key, Object value) {
if (value instanceof String stringVal) {
builder.add(forNamespace(ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM), stringVal);
} else {
builder.add(forNamespace(ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM), mapper.convertValue(value, JsonObject.class));
builder.add(forNamespace(ENDPOINT_PROPERTY_VALUE_PROPERTY_TERM), objectMapperSupplier.get().convertValue(value, JsonObject.class));
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE;

public class JsonObjectFromCriterionTransformer extends AbstractJsonLdTransformer<Criterion, JsonObject> {
private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;
private final Supplier<ObjectMapper> objectMapperSupplier;

public JsonObjectFromCriterionTransformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper) {
public JsonObjectFromCriterionTransformer(JsonBuilderFactory jsonFactory, Supplier<ObjectMapper> objectMapperSupplier) {
super(Criterion.class, JsonObject.class);
this.jsonFactory = jsonFactory;
this.mapper = mapper;
this.objectMapperSupplier = objectMapperSupplier;
}

@Override
Expand All @@ -56,7 +58,7 @@ public JsonObjectFromCriterionTransformer(JsonBuilderFactory jsonFactory, Object

private void addValue(JsonObjectBuilder builder, String field, Object value, TransformerContext context) {
try {
builder.add(field, mapper.convertValue(value, JsonValue.class));
builder.add(field, objectMapperSupplier.get().convertValue(value, JsonValue.class));
} catch (IllegalArgumentException e) {
context.problem()
.invalidProperty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.function.Supplier;

import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_SOURCE_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_TRANSFER_TYPES;
Expand All @@ -38,12 +39,12 @@

public class JsonObjectFromDataPlaneInstanceTransformer extends AbstractJsonLdTransformer<DataPlaneInstance, JsonObject> {
private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;
private final Supplier<ObjectMapper> objectMapperSupplier;

public JsonObjectFromDataPlaneInstanceTransformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper) {
public JsonObjectFromDataPlaneInstanceTransformer(JsonBuilderFactory jsonFactory, Supplier<ObjectMapper> objectMapperSupplier) {
super(DataPlaneInstance.class, JsonObject.class);
this.jsonFactory = jsonFactory;
this.mapper = mapper;
this.objectMapperSupplier = objectMapperSupplier;
}

@Override
Expand All @@ -56,7 +57,7 @@ public JsonObjectFromDataPlaneInstanceTransformer(JsonBuilderFactory jsonFactory

if (dataPlaneInstance.getProperties() != null && !dataPlaneInstance.getProperties().isEmpty()) {
var propBuilder = jsonFactory.createObjectBuilder();
transformProperties(dataPlaneInstance.getProperties(), propBuilder, mapper, context);
transformProperties(dataPlaneInstance.getProperties(), propBuilder, objectMapperSupplier.get(), context);
builder.add(PROPERTIES, propBuilder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.function.Supplier;

import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_DEST_TYPES;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.ALLOWED_SOURCE_TYPES;
Expand All @@ -40,12 +41,12 @@

public class JsonObjectFromDataPlaneInstanceV3Transformer extends AbstractJsonLdTransformer<DataPlaneInstance, JsonObject> {
private final JsonBuilderFactory jsonFactory;
private final ObjectMapper mapper;
private final Supplier<ObjectMapper> objectMapperSupplier;

public JsonObjectFromDataPlaneInstanceV3Transformer(JsonBuilderFactory jsonFactory, ObjectMapper mapper) {
public JsonObjectFromDataPlaneInstanceV3Transformer(JsonBuilderFactory jsonFactory, Supplier<ObjectMapper> objectMapperSupplier) {
super(DataPlaneInstance.class, JsonObject.class);
this.jsonFactory = jsonFactory;
this.mapper = mapper;
this.objectMapperSupplier = objectMapperSupplier;
}

@Override
Expand All @@ -59,7 +60,7 @@ public JsonObjectFromDataPlaneInstanceV3Transformer(JsonBuilderFactory jsonFacto

if (dataPlaneInstance.getProperties() != null && !dataPlaneInstance.getProperties().isEmpty()) {
var propBuilder = jsonFactory.createObjectBuilder();
transformProperties(dataPlaneInstance.getProperties(), propBuilder, mapper, context);
transformProperties(dataPlaneInstance.getProperties(), propBuilder, objectMapperSupplier.get(), context);
builder.add(PROPERTIES, propBuilder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;

import java.util.function.Supplier;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE;
Expand All @@ -33,11 +35,11 @@
* Converts from a generic property as a {@link JsonObject} in JSON-LD expanded form to a Java Object.
*/
public class JsonValueToGenericTypeTransformer extends AbstractJsonLdTransformer<JsonValue, Object> {
private final ObjectMapper mapper;
private final Supplier<ObjectMapper> objectMapperSupplier;

public JsonValueToGenericTypeTransformer(ObjectMapper mapper) {
public JsonValueToGenericTypeTransformer(Supplier<ObjectMapper> objectMapperSupplier) {
super(JsonValue.class, Object.class);
this.mapper = mapper;
this.objectMapperSupplier = objectMapperSupplier;
}

@Override
Expand Down Expand Up @@ -65,7 +67,7 @@ public Object transform(@NotNull JsonValue value, @NotNull TransformerContext co

private Object toJavaType(JsonObject object, TransformerContext context) {
try {
return mapper.readValue(object.toString(), Object.class);
return objectMapperSupplier.get().readValue(object.toString(), Object.class);
} catch (JsonProcessingException e) {
context.reportProblem(format("Failed to read value: %s", e.getMessage()));
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
class JsonObjectFromDataAddressDspaceTransformerTest {

private final JsonObjectFromDataAddressDspaceTransformer transformer = new JsonObjectFromDataAddressDspaceTransformer(
Json.createBuilderFactory(Map.of()), JacksonJsonLd.createObjectMapper());
Json.createBuilderFactory(Map.of()), JacksonJsonLd::createObjectMapper);
private final TransformerContext context = mock();

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class JsonObjectFromDataAddressDspace2024TransformerTest {

private final JsonObjectFromDataAddressDspace2024Transformer transformer = new JsonObjectFromDataAddressDspace2024Transformer(
Json.createBuilderFactory(Map.of()), JacksonJsonLd.createObjectMapper());
Json.createBuilderFactory(Map.of()), JacksonJsonLd::createObjectMapper);
private final TransformerContext context = mock();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class JsonObjectFromCriterionTransformerTest {

@BeforeEach
void setUp() {
transformer = new JsonObjectFromCriterionTransformer(Json.createBuilderFactory(Map.of()), mapper);
transformer = new JsonObjectFromCriterionTransformer(Json.createBuilderFactory(Map.of()), () -> mapper);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.json.Json;
import jakarta.json.JsonString;
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance;
import org.eclipse.edc.jsonld.util.JacksonJsonLd;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -33,7 +34,6 @@
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.URL;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstanceStates.AVAILABLE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper;
import static org.mockito.Mockito.mock;

class JsonObjectFromDataPlaneInstanceTransformerTest {
Expand All @@ -43,7 +43,7 @@ class JsonObjectFromDataPlaneInstanceTransformerTest {

@BeforeEach
void setUp() {
transformer = new JsonObjectFromDataPlaneInstanceTransformer(Json.createBuilderFactory(Map.of()), createObjectMapper());
transformer = new JsonObjectFromDataPlaneInstanceTransformer(Json.createBuilderFactory(Map.of()), JacksonJsonLd::createObjectMapper);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.json.Json;
import jakarta.json.JsonString;
import org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance;
import org.eclipse.edc.jsonld.util.JacksonJsonLd;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -35,7 +36,6 @@
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstance.URL;
import static org.eclipse.edc.connector.dataplane.selector.spi.instance.DataPlaneInstanceStates.AVAILABLE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper;
import static org.mockito.Mockito.mock;

class JsonObjectFromDataPlaneInstanceV3TransformerTest {
Expand All @@ -45,7 +45,7 @@ class JsonObjectFromDataPlaneInstanceV3TransformerTest {

@BeforeEach
void setUp() {
transformer = new JsonObjectFromDataPlaneInstanceV3Transformer(Json.createBuilderFactory(Map.of()), createObjectMapper());
transformer = new JsonObjectFromDataPlaneInstanceV3Transformer(Json.createBuilderFactory(Map.of()), JacksonJsonLd::createObjectMapper);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import jakarta.json.Json;
import jakarta.json.JsonValue;
import org.eclipse.edc.jsonld.spi.JsonLdKeywords;
import org.eclipse.edc.jsonld.util.JacksonJsonLd;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper;
import static org.eclipse.edc.spi.query.Criterion.CRITERION_OPERAND_LEFT;
import static org.eclipse.edc.spi.query.Criterion.CRITERION_OPERAND_RIGHT;
import static org.eclipse.edc.spi.query.Criterion.CRITERION_OPERATOR;
Expand All @@ -38,7 +38,7 @@
class JsonObjectToCriterionTransformerTest {

private final JsonObjectToCriterionTransformer transformer = new JsonObjectToCriterionTransformer();
private final JsonValueToGenericTypeTransformer genericTypeTransformer = new JsonValueToGenericTypeTransformer(createObjectMapper());
private final JsonValueToGenericTypeTransformer genericTypeTransformer = new JsonValueToGenericTypeTransformer(JacksonJsonLd::createObjectMapper);

@Test
void transform() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jakarta.json.JsonValue;
import org.eclipse.edc.jsonld.TitaniumJsonLd;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.jsonld.util.JacksonJsonLd;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.transform.spi.ProblemBuilder;
Expand All @@ -31,7 +32,6 @@
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VOCAB;
import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper;
import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE;
import static org.eclipse.edc.spi.constants.CoreConstants.EDC_PREFIX;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -46,7 +46,7 @@ class JsonObjectToDataAddressTransformerTest {
private static final String CUSTOM_PAYLOAD_NAME = "max";
private static final String TEST_TYPE = "test-type";
private static final String TEST_KEY_NAME = "test-key-name";
private final JsonValueToGenericTypeTransformer objectTransformer = new JsonValueToGenericTypeTransformer(createObjectMapper());
private final JsonValueToGenericTypeTransformer objectTransformer = new JsonValueToGenericTypeTransformer(JacksonJsonLd::createObjectMapper);
private final JsonLd jsonLd = new TitaniumJsonLd(mock(Monitor.class));
private JsonObjectToDataAddressTransformer transformer;
private TransformerContext transformerContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class JsonValueToGenericPropertyTransformerTest {

@BeforeEach
void setUp() {
transformer = new JsonValueToGenericTypeTransformer(mapper);
transformer = new JsonValueToGenericTypeTransformer(() -> mapper);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

import static org.eclipse.edc.connector.controlplane.asset.spi.domain.Asset.EDC_CATALOG_ASSET_TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;

public class JsonObjectFromAssetTransformer extends AbstractJsonLdTransformer<Asset, JsonObject> {
private final ObjectMapper mapper;
private final Supplier<ObjectMapper> objectMapperSupplier;
private final JsonBuilderFactory jsonFactory;

public JsonObjectFromAssetTransformer(JsonBuilderFactory jsonFactory, ObjectMapper jsonLdMapper) {
public JsonObjectFromAssetTransformer(JsonBuilderFactory jsonFactory, Supplier<ObjectMapper> objectMapperSupplier) {
super(Asset.class, JsonObject.class);
this.jsonFactory = jsonFactory;
this.mapper = jsonLdMapper;
this.objectMapperSupplier = objectMapperSupplier;
}

@Override
Expand All @@ -44,12 +46,12 @@ public JsonObjectFromAssetTransformer(JsonBuilderFactory jsonFactory, ObjectMapp
.add(TYPE, Asset.EDC_ASSET_TYPE);

var propBuilder = jsonFactory.createObjectBuilder();
transformProperties(asset.getProperties(), propBuilder, mapper, context);
transformProperties(asset.getProperties(), propBuilder, objectMapperSupplier.get(), context);
builder.add(Asset.EDC_ASSET_PROPERTIES, propBuilder);

if (asset.getPrivateProperties() != null && !asset.getPrivateProperties().isEmpty()) {
var privatePropBuilder = jsonFactory.createObjectBuilder();
transformProperties(asset.getPrivateProperties(), privatePropBuilder, mapper, context);
transformProperties(asset.getPrivateProperties(), privatePropBuilder, objectMapperSupplier.get(), context);
builder.add(Asset.EDC_ASSET_PRIVATE_PROPERTIES, privatePropBuilder);
}

Expand Down
Loading
Loading