From 4ca49060f1ec2fcbc0388d3e6efdd76732cfcfea Mon Sep 17 00:00:00 2001 From: Enrico Risa Date: Mon, 15 Jul 2024 13:17:46 +0200 Subject: [PATCH] fix: apply polymorphic to Dataset for serde (#4362) * fix: apply polymorphic to Dataset for serde * chore: deps file --- DEPENDENCIES | 4 ++-- .../controlplane/catalog/spi/Catalog.java | 2 ++ .../controlplane/catalog/spi/Dataset.java | 5 ++++- .../catalog/spi/CatalogSerializationTest.java | 15 +++++++++++++-- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/DEPENDENCIES b/DEPENDENCIES index ed454617e70..6eb4c27ff47 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -82,7 +82,7 @@ maven/mavencentral/com.networknt/json-schema-validator/1.0.76, Apache-2.0, appro maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.28, Apache-2.0, approved, clearlydefined maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.40, Apache-2.0, approved, #15156 maven/mavencentral/com.puppycrawl.tools/checkstyle/10.17.0, LGPL-2.1-or-later AND (Apache-2.0 AND LGPL-2.1-or-later) AND Apache-2.0, approved, #15077 -maven/mavencentral/com.samskivert/jmustache/1.15, BSD-2-Clause, approved, clearlydefined +maven/mavencentral/com.samskivert/jmustache/1.15, BSD-2-Clause AND BSD-3-Clause, approved, clearlydefined maven/mavencentral/com.squareup.okhttp3/okhttp-dnsoverhttps/4.12.0, Apache-2.0, approved, #11159 maven/mavencentral/com.squareup.okhttp3/okhttp/4.12.0, Apache-2.0, approved, #15227 maven/mavencentral/com.squareup.okhttp3/okhttp/4.9.3, Apache-2.0 AND MPL-2.0, approved, #3225 @@ -225,7 +225,7 @@ maven/mavencentral/org.apache.maven.doxia/doxia-module-xdoc/1.12.0, Apache-2.0, maven/mavencentral/org.apache.maven.doxia/doxia-sink-api/1.12.0, Apache-2.0, approved, clearlydefined maven/mavencentral/org.apache.velocity.tools/velocity-tools-generic/3.1, Apache-2.0, approved, #9331 maven/mavencentral/org.apache.velocity/velocity-engine-core/2.3, Apache-2.0, approved, #2478 -maven/mavencentral/org.apache.velocity/velocity-engine-scripting/2.3, Apache-2.0, approved, clearlydefined +maven/mavencentral/org.apache.velocity/velocity-engine-scripting/2.3, Apache-2.0, restricted, clearlydefined maven/mavencentral/org.apache.xbean/xbean-reflect/3.7, Apache-2.0, approved, clearlydefined maven/mavencentral/org.apiguardian/apiguardian-api/1.1.2, Apache-2.0, approved, clearlydefined maven/mavencentral/org.assertj/assertj-core/3.26.0, Apache-2.0, approved, #14886 diff --git a/spi/control-plane/catalog-spi/src/main/java/org/eclipse/edc/connector/controlplane/catalog/spi/Catalog.java b/spi/control-plane/catalog-spi/src/main/java/org/eclipse/edc/connector/controlplane/catalog/spi/Catalog.java index fe48f6f27ee..99aad9fe8d8 100644 --- a/spi/control-plane/catalog-spi/src/main/java/org/eclipse/edc/connector/controlplane/catalog/spi/Catalog.java +++ b/spi/control-plane/catalog-spi/src/main/java/org/eclipse/edc/connector/controlplane/catalog/spi/Catalog.java @@ -15,6 +15,7 @@ package org.eclipse.edc.connector.controlplane.catalog.spi; +import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; @@ -25,6 +26,7 @@ * Entity representing a Catalog */ @JsonDeserialize(builder = Catalog.Builder.class) +@JsonTypeName("dataspaceconnector:catalog") public class Catalog extends Dataset { protected final List datasets = new ArrayList<>(); protected List dataServices = new ArrayList<>(); diff --git a/spi/control-plane/catalog-spi/src/main/java/org/eclipse/edc/connector/controlplane/catalog/spi/Dataset.java b/spi/control-plane/catalog-spi/src/main/java/org/eclipse/edc/connector/controlplane/catalog/spi/Dataset.java index cdd58d9520e..a704a200cc5 100644 --- a/spi/control-plane/catalog-spi/src/main/java/org/eclipse/edc/connector/controlplane/catalog/spi/Dataset.java +++ b/spi/control-plane/catalog-spi/src/main/java/org/eclipse/edc/connector/controlplane/catalog/spi/Dataset.java @@ -16,9 +16,11 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import org.eclipse.edc.policy.model.Policy; +import org.eclipse.edc.spi.types.domain.Polymorphic; import java.util.ArrayList; import java.util.HashMap; @@ -33,7 +35,8 @@ * different policies and may be available via different distributions. */ @JsonDeserialize(builder = Dataset.Builder.class) -public class Dataset { +@JsonTypeName("dataspaceconnector:dataset") +public class Dataset implements Polymorphic { /** * Policies under which this Dataset is available. diff --git a/spi/control-plane/catalog-spi/src/test/java/org/eclipse/edc/connector/controlplane/catalog/spi/CatalogSerializationTest.java b/spi/control-plane/catalog-spi/src/test/java/org/eclipse/edc/connector/controlplane/catalog/spi/CatalogSerializationTest.java index 3e2d2d5892d..ce97ac6ebcf 100644 --- a/spi/control-plane/catalog-spi/src/test/java/org/eclipse/edc/connector/controlplane/catalog/spi/CatalogSerializationTest.java +++ b/spi/control-plane/catalog-spi/src/test/java/org/eclipse/edc/connector/controlplane/catalog/spi/CatalogSerializationTest.java @@ -17,6 +17,7 @@ import org.eclipse.edc.json.JacksonTypeManager; import org.eclipse.edc.policy.model.Policy; import org.eclipse.edc.spi.types.TypeManager; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.List; @@ -28,6 +29,11 @@ class CatalogSerializationTest { private final TypeManager typeManager = new JacksonTypeManager(); + @BeforeEach + void beforeAll() { + typeManager.registerTypes(Catalog.class, Dataset.class); + } + @Test void verifySerialization() { var catalog = createCatalog(); @@ -35,7 +41,9 @@ void verifySerialization() { var json = typeManager.writeValueAsString(catalog); var deserialized = typeManager.readValue(json, Catalog.class); - assertThat(deserialized).usingRecursiveComparison().isEqualTo(catalog); + assertThat(catalog).usingRecursiveComparison().isEqualTo(deserialized); + + assertThat(catalog.getDatasets().get(1)).isInstanceOf(Catalog.class); } @@ -51,10 +59,13 @@ private Catalog createCatalog() { .distributions(List.of(distribution)) .build(); + + var nestedCatalog = Catalog.Builder.newInstance().id("nestedId").participantId("participantId").build(); + return Catalog.Builder.newInstance() .id("id") .dataServices(List.of(dataService)) - .datasets(List.of(dataset)) + .datasets(List.of(dataset, nestedCatalog)) .property("prop", "propValue") .build(); }