Skip to content

Commit

Permalink
@JsonbTransient does not work on abstract classes #454 (#572)
Browse files Browse the repository at this point in the history
@JsonbTransient does not work on abstract classes #454

Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Aug 26, 2022
1 parent 5fb9f86 commit a7fb7a4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main/java/org/eclipse/yasson/internal/model/PropertyModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public PropertyModel(ClassModel classModel, Property property, JsonbContext json
this.setValueHandle = createWriteHandle(field, setter, setterVisible, strategy);
this.getterMethodType = getterVisible ? property.getGetterType() : null;
this.setterMethodType = setterVisible ? property.getSetterType() : null;
this.customization = introspectCustomization(property, jsonbContext);
this.customization = introspectCustomization(property, jsonbContext, classModel);
this.readName = calculateReadWriteName(customization.getJsonReadName(), propertyName,
jsonbContext.getConfigProperties().getPropertyNamingStrategy());
this.writeName = calculateReadWriteName(customization.getJsonWriteName(), propertyName,
Expand Down Expand Up @@ -191,11 +191,24 @@ private SerializerBinding<?> getUserSerializerBinding(Property property, JsonbCo
return jsonbContext.getComponentMatcher().getSerializerBinding(getPropertySerializationType(), null).orElse(null);
}

private PropertyCustomization introspectCustomization(Property property, JsonbContext jsonbContext) {
private PropertyCustomization introspectCustomization(Property property, JsonbContext jsonbContext, ClassModel classModel) {
final AnnotationIntrospector introspector = jsonbContext.getAnnotationIntrospector();
final PropertyCustomization.Builder builder = PropertyCustomization.builder();
//drop all other annotations for transient properties
EnumSet<AnnotationTarget> transientInfo = introspector.getJsonbTransientCategorized(property);
ClassModel parent = classModel;
// Check parent classes for transient annotations
while ((parent = parent.getParentClassModel()) != null) {
PropertyModel parentProperty = parent.getPropertyModel(property.getName());
if (parentProperty != null) {
if (parentProperty.customization.isReadTransient()) {
transientInfo.add(AnnotationTarget.GETTER);
}
if (parentProperty.customization.isWriteTransient()) {
transientInfo.add(AnnotationTarget.SETTER);
}
}
}
if (transientInfo.size() != 0) {
builder.readTransient(transientInfo.contains(AnnotationTarget.GETTER));
builder.writeTransient(transientInfo.contains(AnnotationTarget.SETTER));
Expand Down
80 changes: 80 additions & 0 deletions src/test/java/org/eclipse/yasson/Issue454Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.JsonbConfig;
import jakarta.json.bind.annotation.JsonbTransient;

public class Issue454Test {

@Test
public void test() {
final String EXPECTED = "{\"field2\":\"bbb\"}";
Jsonb jsonb = JsonbBuilder.create(new JsonbConfig());
assertEquals(EXPECTED, jsonb.toJson(new TheInterface() {

@Override
public String getField1() {
return "aaa";
}

@Override
public String getField2() {
return "bbb";
}}));
assertEquals(EXPECTED, jsonb.toJson(new TheClass() {
@Override
public String getField1() {
return "aaa";
}
@Override
public String getField2() {
return "bbb";
}}));
assertEquals(EXPECTED, jsonb.toJson(new TheClass2()));
assertEquals(EXPECTED, jsonb.toJson(new TheClass2() {}));
}

public static abstract class TheClass {
@JsonbTransient
public abstract String getField1();

public abstract String getField2();
}

public static class TheClass2 extends TheClass {
@Override
public String getField1() {
return "aaa";
}
@Override
public String getField2() {
return "bbb";
}
}

public static interface TheInterface {

@JsonbTransient
String getField1();

String getField2();
}

}

0 comments on commit a7fb7a4

Please sign in to comment.