Skip to content

Commit

Permalink
## 2.3.0
Browse files Browse the repository at this point in the history
* For standard codecs a wrapper was added to enable to use them within polymorphic structures
* when PolymorphicCodecs are registered within the chain of codecs, Polymorphia will use them when encoding polymorphic types (instead of using a reflection based codec)
* PojoCodecProvider.Builder was enriched by two methods to enable excluding classes when building the domain model
* fine tuning while identifying polymorphic types
* type hierarchy was broken if super classes are not part of the domain model but some other superclass in the class hierarchy
* adding support to register a specialized ClassResolver if neither org.springframework.spring-core or org.reflections.reflections are sufficient when resolving classes
  • Loading branch information
webermich committed Feb 4, 2018
1 parent bd6490b commit 82674f8
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.bild.backend</groupId>
<artifactId>polymorphia</artifactId>
<version>2.2.0</version>
<version>2.3.0</version>

<name>${project.groupId}:${project.artifactId}</name>

Expand Down
10 changes: 10 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
Release Notes
=======

## 2.3.0
* For standard codecs a wrapper was added to enable to use them within polymorphic structures
* when PolymorphicCodecs are registered within the chain of codecs, Polymorphia will use them when encoding polymorphic types (instead of using a reflection based codec)
* PojoCodecProvider.Builder was enriched by two methods to enable excluding classes when building the domain model
* fine tuning while identifying polymorphic types
* type hierarchy was broken if super classes are not part of the domain model but some other superclass in the class hierarchy
* adding support to register a specialized ClassResolver if neither org.springframework.spring-core or org.reflections.reflections are sufficient when resolving classes


## 2.2.0
* mongo version > 3.5 support
* changed enum handling
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/de/bild/codec/PojoContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,17 @@ public synchronized <T> PolymorphicCodec<T> resolve(Type type, TypeCodecRegistry

// if there is a standard codec provided for a type, that needs to be stored along with
// polymorphic Type information "_t", we need to wrap that codec
/** Alternatively a user could always provide a {@link CodecResolver} in order to override this default handling **/
// if codecMap contains a mapping for type (may be LazyCodec) that means, that within the chain of registered codecs,
/** Alternatively a user could always provide a {@link CodecResolver} in order to override this default handling
* Additionally a user can register a {@link PolymorphicCodec} within teh CodecRegistry chain.
* That codec can be used to encode/decode a type in polymorphic contexts as well as non-polymorphic contexts
* For an example: {@link AnyThingTest} **/
// if codecMap contains a mapping for type (may be LazyCodec), this means, that within the chain of registered codecs,
// there is no codec able to handle the type -> so we need to skip this part and create a reflection based codec
if (!codecMap.containsKey(type)) {
Codec<T> standardCodec = typeCodecRegistry.getCodec(type);
if (!(standardCodec instanceof TypeCodec)) {
if (standardCodec instanceof PolymorphicCodec) {
return (PolymorphicCodec)standardCodec; // lovely, user provided a PolymorphicCodec
} else if (!(standardCodec instanceof TypeCodec)){
return new PolymorphicCodecWrapper<>(standardCodec);
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/de/bild/codec/TypesModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public TypesModel(final Set<Class<?>> classes,
final Set<Class<? extends Annotation>> ignoreAnnotations,
Set<Predicate<String>> ignoreTypesMatchingClassNamePredicates,
Set<Class<?>> ignoreClasses,
ClassResolver classResolver) {
ClassResolver specializedClassResolver) {
this.ignoreAnnotations = ignoreAnnotations != null ? ignoreAnnotations : Collections.emptySet();
this.ignoreTypesMatchingClassNamePredicates = ignoreTypesMatchingClassNamePredicates != null ? ignoreTypesMatchingClassNamePredicates : Collections.emptySet();
this.ignoreClasses = ignoreClasses != null ? ignoreClasses : Collections.emptySet();
this.classResolver = classResolver != null ? classResolver : getClassResolver();
this.classResolver = specializedClassResolver != null ? specializedClassResolver : getClassResolver();

// first index all direct classes
if (classes != null) {
Expand All @@ -54,7 +54,7 @@ public TypesModel(final Set<Class<?>> classes,

if (packages != null && !packages.isEmpty()) {
for (String aPackage : packages) {
for (Class<?> clazz : classResolver.getClassesForPackage(aPackage)) {
for (Class<?> clazz : this.classResolver.getClassesForPackage(aPackage)) {
indexClass(clazz);
}
}
Expand Down Expand Up @@ -88,9 +88,6 @@ default Pattern getPatternForPackage(String packageName) {
* @return an indexer or throws {@link IllegalStateException}
*/
private ClassResolver getClassResolver() {
if (classResolver != null) {
return classResolver;
}
// now depending on library, index packages
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/de/bild/codec/NullHandlingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ public void basicTest() throws JSONException {

BasePojo readBasePojo = primitivePojoCodec.decode(new JsonReader(stringWriter.toString()), DecoderContext.builder().build());

JSONAssert.assertEquals(stringWriter.toString(), "{\n" +
JSONAssert.assertEquals("{\n" +
" \"encodeNullsTrue\" : null,\n" +
" \"encodeNullHandlingStrategy_CODEC\" : [],\n" +
" \"encodeNullsShouldDecodeToNull\" : null\n" +
"}", true);
"}", stringWriter.toString(), true);


Assert.assertNull(readBasePojo.encodeNullsFalse);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package de.bild.codec.dividedclassmodel;

import com.mongodb.MongoClient;
import de.bild.codec.EnumCodecProvider;
import de.bild.codec.PojoCodecProvider;
import de.bild.codec.PolymorphicReflectionCodec;
import de.bild.codec.dividedclassmodel.basemodel.BasePojo;
import de.bild.codec.dividedclassmodel.nonmodel.NonModelPojo;
import de.bild.codec.dividedclassmodel.submodel.SubPojoWithinModel;
import org.bson.codecs.Codec;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DividedModelTest.class)
@ComponentScan(basePackages = "de.bild")
public class DividedModelTest {

static class Config {
@Bean()
public static CodecRegistry getCodecRegistry() {
return CodecRegistries.fromRegistries(
CodecRegistries.fromProviders(
new EnumCodecProvider(),
PojoCodecProvider.builder()
.register(BasePojo.class.getPackage().getName())
.register(SubPojoWithinModel.class.getPackage().getName())
.build()
),
MongoClient.getDefaultCodecRegistry());
}
}

@Autowired
CodecRegistry codecRegistry;

@Test
public void testModelClasses() {
Codec<SubPojoWithinModel> subPojoWithinModelCodec = codecRegistry.get(SubPojoWithinModel.class);
Assert.assertTrue(subPojoWithinModelCodec instanceof PolymorphicReflectionCodec);
Codec<NonModelPojo> nonModelPojoCodec = codecRegistry.get(NonModelPojo.class);
// interestingly nonModelPojoCodec will "upcast" NonModelPojo to BasePojo and a codec for BasePojo will be created
Assert.assertTrue(nonModelPojoCodec instanceof PolymorphicReflectionCodec);
//todo : What else do we need to check here?
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package de.bild.codec.dividedclassmodel.basemodel;

public interface BaseInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package de.bild.codec.dividedclassmodel.basemodel;

public class BasePojo implements BaseInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package de.bild.codec.dividedclassmodel.nonmodel;

public interface NonModelInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.bild.codec.dividedclassmodel.nonmodel;

import de.bild.codec.dividedclassmodel.basemodel.BasePojo;

public class NonModelPojo extends BasePojo implements NonModelInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package de.bild.codec.dividedclassmodel.nonmodel;

public class NonModelPojoExt extends NonModelPojo {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.bild.codec.dividedclassmodel.submodel;

import de.bild.codec.dividedclassmodel.nonmodel.NonModelPojoExt;

public class SubPojoWithinModel extends NonModelPojoExt {
}
Loading

0 comments on commit 82674f8

Please sign in to comment.