Skip to content

Commit

Permalink
Merge pull request #459 from eclipse/fix-entity-list
Browse files Browse the repository at this point in the history
Fix serialization when parameter is generic
  • Loading branch information
otaviojava authored Dec 4, 2023
2 parents 0fc8d9d + cb1f869 commit c483551
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version

== [Unreleased]

=== Fixed

- Fix serialization when the entity has constructor with simple generic types

== [1.0.3] - 2023-12-02

=== Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private <T> T convertEntityByConstructor(List<Column> columns, EntityMetadata ma
.filter(c -> c.name().equals(parameter.name()))
.findFirst();
column.ifPresentOrElse(c -> {
ParameterConverter converter = ParameterConverter.of(parameter);
ParameterConverter converter = ParameterConverter.of(parameter, getEntities());
converter.convert(this, c, parameter, builder);
}, builder::addEmptyParameter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.jnosql.communication.TypeReference;
import org.eclipse.jnosql.communication.column.Column;
import org.eclipse.jnosql.mapping.metadata.ConstructorBuilder;
import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata;
import org.eclipse.jnosql.mapping.metadata.GenericParameterMetaData;
import org.eclipse.jnosql.mapping.metadata.ParameterMetaData;

Expand Down Expand Up @@ -83,11 +84,20 @@ abstract void convert(ColumnEntityConverter converter,
Column column, ParameterMetaData metaData,
ConstructorBuilder builder);

static ParameterConverter of(ParameterMetaData parameter) {
static ParameterConverter of(ParameterMetaData parameter, EntitiesMetadata entities) {
return switch (parameter.mappingType()) {
case COLLECTION -> COLLECTION;
case COLLECTION -> validateCollection(parameter, entities);
case ENTITY -> ENTITY;
default -> DEFAULT;
};
}

private static ParameterConverter validateCollection(ParameterMetaData parameter, EntitiesMetadata entities) {
GenericParameterMetaData genericParameter = (GenericParameterMetaData) parameter;
Class<?> type = genericParameter.elementType();
if (entities.findByClassName(type.getName()).isPresent()) {
return COLLECTION;
}
return DEFAULT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.jnosql.mapping.column;

import jakarta.inject.Inject;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.jnosql.communication.TypeReference;
import org.eclipse.jnosql.communication.column.Column;
import org.eclipse.jnosql.communication.column.ColumnEntity;
Expand All @@ -26,6 +27,7 @@
import org.eclipse.jnosql.mapping.column.entities.constructor.BookUser;
import org.eclipse.jnosql.mapping.column.entities.constructor.Computer;
import org.eclipse.jnosql.mapping.column.entities.constructor.PetOwner;
import org.eclipse.jnosql.mapping.column.entities.constructor.SuperHero;
import org.eclipse.jnosql.mapping.column.spi.ColumnExtension;
import org.eclipse.jnosql.mapping.reflection.Reflections;
import org.eclipse.jnosql.mapping.spi.EntityMetadataExtension;
Expand Down Expand Up @@ -180,4 +182,20 @@ void shouldConverterEntityBookReleaseOnStringYear() {
assertEquals("Joshua Bloch", book.getAuthor());
assertEquals(Year.of(2001), book.getYear());
}

@Test
void shouldConvertHero() {
ColumnEntity communication = ColumnEntity.of("SuperHero");
communication.add("_id", "10L");
communication.add("name", "Otavio");
communication.add("powers", List.of("speed", "strength"));

SuperHero hero = this.converter.toEntity(communication);
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(hero.id()).isEqualTo("10L");
softly.assertThat(hero.name()).isEqualTo("Otavio");
softly.assertThat(hero.powers()).contains("speed", "strength");
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.mapping.column.entities.constructor;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

import java.util.List;

@Entity
public record SuperHero(@Id String id, @Column String name, @Column List<String> powers) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private <T> T convertEntityByConstructor(List<Document> documents, EntityMetadat
.filter(c -> c.name().equals(parameter.name()))
.findFirst();
document.ifPresentOrElse(c -> {
ParameterConverter converter = ParameterConverter.of(parameter);
ParameterConverter converter = ParameterConverter.of(parameter, getEntities());
converter.convert(this, c, parameter, builder);
}, builder::addEmptyParameter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.jnosql.communication.TypeReference;
import org.eclipse.jnosql.communication.document.Document;
import org.eclipse.jnosql.mapping.metadata.ConstructorBuilder;
import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata;
import org.eclipse.jnosql.mapping.metadata.GenericParameterMetaData;
import org.eclipse.jnosql.mapping.metadata.ParameterMetaData;

Expand Down Expand Up @@ -84,11 +85,20 @@ abstract void convert(DocumentEntityConverter converter,
Document document, ParameterMetaData metaData,
ConstructorBuilder builder);

static ParameterConverter of(ParameterMetaData parameter) {
static ParameterConverter of(ParameterMetaData parameter, EntitiesMetadata entities) {
return switch (parameter.mappingType()) {
case COLLECTION -> COLLECTION;
case COLLECTION -> validateCollection(parameter, entities);
case ENTITY -> ENTITY;
default -> DEFAULT;
};
}

private static ParameterConverter validateCollection(ParameterMetaData parameter, EntitiesMetadata entities) {
GenericParameterMetaData genericParameter = (GenericParameterMetaData) parameter;
Class<?> type = genericParameter.elementType();
if (entities.findByClassName(type.getName()).isPresent()) {
return COLLECTION;
}
return DEFAULT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.jnosql.mapping.document;

import jakarta.inject.Inject;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.jnosql.communication.TypeReference;
import org.eclipse.jnosql.communication.document.Document;
import org.eclipse.jnosql.communication.document.DocumentEntity;
Expand All @@ -25,6 +26,7 @@
import org.eclipse.jnosql.mapping.document.entities.constructor.BookUser;
import org.eclipse.jnosql.mapping.document.entities.constructor.Computer;
import org.eclipse.jnosql.mapping.document.entities.constructor.PetOwner;
import org.eclipse.jnosql.mapping.document.entities.constructor.SuperHero;
import org.eclipse.jnosql.mapping.document.spi.DocumentExtension;
import org.eclipse.jnosql.mapping.reflection.Reflections;
import org.eclipse.jnosql.mapping.spi.EntityMetadataExtension;
Expand Down Expand Up @@ -179,5 +181,19 @@ void shouldConverterEntityBookReleaseOnStringYear() {
assertEquals(Year.of(2001), book.getYear());
}

@Test
void shouldConvertHero() {
DocumentEntity communication = DocumentEntity.of("SuperHero");
communication.add("_id", "10L");
communication.add("name", "Otavio");
communication.add("powers", List.of("speed", "strength"));

SuperHero hero = this.converter.toEntity(communication);
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(hero.id()).isEqualTo("10L");
softly.assertThat(hero.name()).isEqualTo("Otavio");
softly.assertThat(hero.powers()).contains("speed", "strength");
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.mapping.document.entities.constructor;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

import java.util.List;

@Entity
public record SuperHero(@Id String id, @Column String name, @Column List<String> powers) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.jnosql.mapping.test.entities.constructor.Computer;
import org.eclipse.jnosql.mapping.test.entities.constructor.PetOwner;
import org.eclipse.jnosql.mapping.test.entities.constructor.Smartphone;
import org.eclipse.jnosql.mapping.test.entities.constructor.SuperHero;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -128,6 +129,20 @@ void shouldConvertMapParameter() {
assertEquals(DefaultGenericParameterMetaData.class, map.getClass());
}

@Test
void shouldConvertListParameter() {
Constructor<Foo> constructor = (Constructor<Foo>) SuperHero.class.getDeclaredConstructors()[0];
ParameterMetaData powers = ParameterMetaDataBuilder.of(constructor.getParameters()[2]);
Assertions.assertNotNull(powers);
Assertions.assertFalse(powers.isId());
Assertions.assertEquals("powers", powers.name());
Assertions.assertEquals(List.class, powers.type());
Assertions.assertEquals(MappingType.COLLECTION, powers.mappingType());
Assertions.assertTrue(powers.converter().isEmpty());
assertEquals(DefaultGenericParameterMetaData.class, powers.getClass());
}


static class Foo{
private Map<String, String> map;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.mapping.test.entities.constructor;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

import java.util.List;

@Entity
public record SuperHero(@Id String id, @Column String name, @Column List<String> powers) {
}

0 comments on commit c483551

Please sign in to comment.