Skip to content

Commit a7fb7a4

Browse files
authored
@JsonbTransient does not work on abstract classes #454 (#572)
@JsonbTransient does not work on abstract classes #454 Signed-off-by: Jorge Bescos Gascon <[email protected]>
1 parent 5fb9f86 commit a7fb7a4

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

src/main/java/org/eclipse/yasson/internal/model/PropertyModel.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public PropertyModel(ClassModel classModel, Property property, JsonbContext json
158158
this.setValueHandle = createWriteHandle(field, setter, setterVisible, strategy);
159159
this.getterMethodType = getterVisible ? property.getGetterType() : null;
160160
this.setterMethodType = setterVisible ? property.getSetterType() : null;
161-
this.customization = introspectCustomization(property, jsonbContext);
161+
this.customization = introspectCustomization(property, jsonbContext, classModel);
162162
this.readName = calculateReadWriteName(customization.getJsonReadName(), propertyName,
163163
jsonbContext.getConfigProperties().getPropertyNamingStrategy());
164164
this.writeName = calculateReadWriteName(customization.getJsonWriteName(), propertyName,
@@ -191,11 +191,24 @@ private SerializerBinding<?> getUserSerializerBinding(Property property, JsonbCo
191191
return jsonbContext.getComponentMatcher().getSerializerBinding(getPropertySerializationType(), null).orElse(null);
192192
}
193193

194-
private PropertyCustomization introspectCustomization(Property property, JsonbContext jsonbContext) {
194+
private PropertyCustomization introspectCustomization(Property property, JsonbContext jsonbContext, ClassModel classModel) {
195195
final AnnotationIntrospector introspector = jsonbContext.getAnnotationIntrospector();
196196
final PropertyCustomization.Builder builder = PropertyCustomization.builder();
197197
//drop all other annotations for transient properties
198198
EnumSet<AnnotationTarget> transientInfo = introspector.getJsonbTransientCategorized(property);
199+
ClassModel parent = classModel;
200+
// Check parent classes for transient annotations
201+
while ((parent = parent.getParentClassModel()) != null) {
202+
PropertyModel parentProperty = parent.getPropertyModel(property.getName());
203+
if (parentProperty != null) {
204+
if (parentProperty.customization.isReadTransient()) {
205+
transientInfo.add(AnnotationTarget.GETTER);
206+
}
207+
if (parentProperty.customization.isWriteTransient()) {
208+
transientInfo.add(AnnotationTarget.SETTER);
209+
}
210+
}
211+
}
199212
if (transientInfo.size() != 0) {
200213
builder.readTransient(transientInfo.contains(AnnotationTarget.GETTER));
201214
builder.writeTransient(transientInfo.contains(AnnotationTarget.SETTER));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
package org.eclipse.yasson;
14+
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
import jakarta.json.bind.Jsonb;
20+
import jakarta.json.bind.JsonbBuilder;
21+
import jakarta.json.bind.JsonbConfig;
22+
import jakarta.json.bind.annotation.JsonbTransient;
23+
24+
public class Issue454Test {
25+
26+
@Test
27+
public void test() {
28+
final String EXPECTED = "{\"field2\":\"bbb\"}";
29+
Jsonb jsonb = JsonbBuilder.create(new JsonbConfig());
30+
assertEquals(EXPECTED, jsonb.toJson(new TheInterface() {
31+
32+
@Override
33+
public String getField1() {
34+
return "aaa";
35+
}
36+
37+
@Override
38+
public String getField2() {
39+
return "bbb";
40+
}}));
41+
assertEquals(EXPECTED, jsonb.toJson(new TheClass() {
42+
@Override
43+
public String getField1() {
44+
return "aaa";
45+
}
46+
@Override
47+
public String getField2() {
48+
return "bbb";
49+
}}));
50+
assertEquals(EXPECTED, jsonb.toJson(new TheClass2()));
51+
assertEquals(EXPECTED, jsonb.toJson(new TheClass2() {}));
52+
}
53+
54+
public static abstract class TheClass {
55+
@JsonbTransient
56+
public abstract String getField1();
57+
58+
public abstract String getField2();
59+
}
60+
61+
public static class TheClass2 extends TheClass {
62+
@Override
63+
public String getField1() {
64+
return "aaa";
65+
}
66+
@Override
67+
public String getField2() {
68+
return "bbb";
69+
}
70+
}
71+
72+
public static interface TheInterface {
73+
74+
@JsonbTransient
75+
String getField1();
76+
77+
String getField2();
78+
}
79+
80+
}

0 commit comments

Comments
 (0)