This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git


The following commit(s) were added to refs/heads/master by this push:
     new 904c0810 [JOHNZON-401] fix serialization of nested polymorphic objects
904c0810 is described below

commit 904c08102d82e4fb5ca443e13d3a9251787f4899
Author: Markus Jung <m...@markus-jung.dev>
AuthorDate: Fri Jan 26 08:15:49 2024 +0100

    [JOHNZON-401] fix serialization of nested polymorphic objects
---
 .../johnzon/jsonb/polymorphism/JsonbPolymorphismTest.java | 15 +++++++++++++++
 .../org/apache/johnzon/mapper/MappingGeneratorImpl.java   | 12 ++++++------
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git 
a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTest.java
 
b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTest.java
index 87d83899..52351430 100644
--- 
a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTest.java
+++ 
b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTest.java
@@ -41,6 +41,17 @@ public class JsonbPolymorphismTest {
 
     @Rule public JsonbRule jsonb = new JsonbRule();
 
+    @Test
+    public void testNestedSerialization() {
+        Labrador labrador = new Labrador();
+        labrador.dogAge = 3;
+        labrador.labradorName = "john";
+        AnimalWrapper wrapper = new AnimalWrapper();
+        wrapper.animal = labrador;
+
+        
assertEquals("{\"animal\":{\"@animal\":\"dog\",\"@dog\":\"labrador\",\"dogAge\":3,\"labradorName\":\"john\"}}",
 jsonb.toJson(wrapper));
+    }
+
     @Test
     public void testSerialization() {
         Labrador labrador = new Labrador();
@@ -100,6 +111,10 @@ public class JsonbPolymorphismTest {
 
     }
 
+    public static class AnimalWrapper {
+        public Animal animal;
+    }
+
     @JsonbTypeInfo(key = "@animal", value = @JsonbSubtype(alias = "dog", type 
= Dog.class))
     public interface Animal {
     }
diff --git 
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
index cf44c5c7..c06f38df 100644
--- 
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
+++ 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
@@ -192,12 +192,6 @@ public class MappingGeneratorImpl implements 
MappingGenerator {
                     generator.writeStartObject();
                 }
 
-                if (classMapping.serializedPolymorphicProperties != null) {
-                    for (Map.Entry<String, String> polymorphicProperty : 
classMapping.serializedPolymorphicProperties) {
-                        generator.write(polymorphicProperty.getKey(), 
polymorphicProperty.getValue());
-                    }
-                }
-
                 final boolean writeEnd = doWriteObjectBody(object, 
ignoredProperties, jsonPointer, generator);
                 if (writeEnd && writeBody) {
                     generator.writeEnd();
@@ -376,6 +370,12 @@ public class MappingGeneratorImpl implements 
MappingGenerator {
             return true;
         }
 
+        if (classMapping.serializedPolymorphicProperties != null) {
+            for (Map.Entry<String, String> polymorphicProperty : 
classMapping.serializedPolymorphicProperties) {
+                generator.write(polymorphicProperty.getKey(), 
polymorphicProperty.getValue());
+            }
+        }
+
         for (final Map.Entry<String, Mappings.Getter> getterEntry : 
classMapping.getters.entrySet()) {
             final Mappings.Getter getter = getterEntry.getValue();
             if (ignored != null && ignored.contains(getterEntry.getKey())) {

Reply via email to