Skip to content

Commit 12d9fac

Browse files
authored
Merge branch 'eclipse-ee4j:master' into npe-custom-timestamp-formatter
2 parents 3e482ef + 66bef09 commit 12d9fac

15 files changed

+303
-38
lines changed

src/main/java/org/eclipse/yasson/internal/AnnotationIntrospector.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import jakarta.json.bind.annotation.JsonbTypeInfo;
6060
import jakarta.json.bind.annotation.JsonbTypeSerializer;
6161
import jakarta.json.bind.annotation.JsonbVisibility;
62+
import jakarta.json.bind.config.PropertyNamingStrategy;
6263
import jakarta.json.bind.config.PropertyVisibilityStrategy;
6364
import jakarta.json.bind.serializer.JsonbDeserializer;
6465
import jakarta.json.bind.serializer.JsonbSerializer;
@@ -152,10 +153,12 @@ private String getJsonbPropertyCustomizedName(Property property, JsonbAnnotatedE
152153
/**
153154
* Searches for JsonbCreator annotation on constructors and static methods.
154155
*
155-
* @param clazz class to search
156+
* @param clazz class to search
157+
* @param propertyNamingStrategy The naming strategy to use for the ${@code JsonbConstructor} annotation,
158+
* if set and no {@code JsonbProperty} annotations are present.
156159
* @return JsonbCreator metadata object
157160
*/
158-
public JsonbCreator getCreator(Class<?> clazz) {
161+
public JsonbCreator getCreator(Class<?> clazz, PropertyNamingStrategy propertyNamingStrategy) {
159162
JsonbCreator jsonbCreator = null;
160163
Constructor<?>[] declaredConstructors =
161164
AccessController.doPrivileged((PrivilegedAction<Constructor<?>[]>) clazz::getDeclaredConstructors);
@@ -164,7 +167,7 @@ public JsonbCreator getCreator(Class<?> clazz) {
164167
final jakarta.json.bind.annotation.JsonbCreator annot = findAnnotation(constructor.getDeclaredAnnotations(),
165168
jakarta.json.bind.annotation.JsonbCreator.class);
166169
if (annot != null) {
167-
jsonbCreator = createJsonbCreator(constructor, jsonbCreator, clazz);
170+
jsonbCreator = createJsonbCreator(constructor, jsonbCreator, clazz, propertyNamingStrategy);
168171
}
169172
}
170173

@@ -179,19 +182,19 @@ public JsonbCreator getCreator(Class<?> clazz) {
179182
method,
180183
clazz));
181184
}
182-
jsonbCreator = createJsonbCreator(method, jsonbCreator, clazz);
185+
jsonbCreator = createJsonbCreator(method, jsonbCreator, clazz, propertyNamingStrategy);
183186
}
184187
}
185188
if (jsonbCreator == null) {
186-
jsonbCreator = ClassMultiReleaseExtension.findCreator(clazz, declaredConstructors, this);
189+
jsonbCreator = ClassMultiReleaseExtension.findCreator(clazz, declaredConstructors, this, propertyNamingStrategy);
187190
if (jsonbCreator == null) {
188191
jsonbCreator = constructorPropertiesIntrospector.getCreator(declaredConstructors);
189192
}
190193
}
191194
return jsonbCreator;
192195
}
193196

194-
JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Class<?> clazz) {
197+
JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Class<?> clazz, PropertyNamingStrategy propertyNamingStrategy) {
195198
if (existing != null) {
196199
throw new JsonbException(Messages.getMessage(MessageKeys.MULTIPLE_JSONB_CREATORS, clazz));
197200
}
@@ -205,7 +208,8 @@ JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Cl
205208
if (jsonbPropertyAnnotation != null && !jsonbPropertyAnnotation.value().isEmpty()) {
206209
creatorModels[i] = new CreatorModel(jsonbPropertyAnnotation.value(), parameter, executable, jsonbContext);
207210
} else {
208-
creatorModels[i] = new CreatorModel(parameter.getName(), parameter, executable, jsonbContext);
211+
final String translatedParameterName = propertyNamingStrategy.translateName(parameter.getName());
212+
creatorModels[i] = new CreatorModel(translatedParameterName, parameter, executable, jsonbContext);
209213
}
210214
}
211215

@@ -779,16 +783,19 @@ public Set<Class<?>> collectInterfaces(Class<?> cls) {
779783
/**
780784
* Processes customizations.
781785
*
782-
* @param clsElement Element to process.
786+
* @param clsElement Element to process.
787+
* @param propertyNamingStrategy The naming strategy to use for the ${@code JsonbConstructor} annotation,
788+
* if set and no {@code JsonbProperty} annotations are present.
783789
* @return Populated {@link ClassCustomization} instance.
784790
*/
785791
public ClassCustomization introspectCustomization(JsonbAnnotatedElement<Class<?>> clsElement,
786-
ClassCustomization parentCustomization) {
792+
ClassCustomization parentCustomization,
793+
PropertyNamingStrategy propertyNamingStrategy) {
787794
return ClassCustomization.builder()
788795
.nillable(isClassNillable(clsElement))
789796
.dateTimeFormatter(getJsonbDateFormat(clsElement))
790797
.numberFormatter(getJsonbNumberFormat(clsElement))
791-
.creator(getCreator(clsElement.getElement()))
798+
.creator(getCreator(clsElement.getElement(), propertyNamingStrategy))
792799
.propertyOrder(getPropertyOrder(clsElement))
793800
.adapterBinding(getAdapterBinding(clsElement))
794801
.serializerBinding(getSerializerBinding(clsElement))

src/main/java/org/eclipse/yasson/internal/ClassMultiReleaseExtension.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Optional;
1919

2020
import jakarta.json.bind.JsonbException;
21+
import jakarta.json.bind.config.PropertyNamingStrategy;
2122

2223
import org.eclipse.yasson.internal.model.JsonbCreator;
2324
import org.eclipse.yasson.internal.model.Property;
@@ -42,7 +43,8 @@ static boolean isSpecialAccessorMethod(Method method, Map<String, Property> clas
4243

4344
static JsonbCreator findCreator(Class<?> clazz,
4445
Constructor<?>[] declaredConstructors,
45-
AnnotationIntrospector introspector) {
46+
AnnotationIntrospector introspector,
47+
PropertyNamingStrategy propertyNamingStrategy) {
4648
return null;
4749
}
4850

src/main/java/org/eclipse/yasson/internal/MappingContext.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ private static Function<Class<?>, ClassModel> createParseClassModelFunction(Clas
8888
.introspectCustomization(clsElement,
8989
parentClassModel == null
9090
? ClassCustomization.empty()
91-
: parentClassModel.getClassCustomization());
91+
: parentClassModel.getClassCustomization(),
92+
jsonbContext.getConfigProperties().getPropertyNamingStrategy());
9293
// PolymorphismSupport configPolymorphism = jsonbContext.getConfigProperties().getPolymorphismSupport();
9394
// if (configPolymorphism != null) {
9495
// customization = mergeConfigAndAnnotationPolymorphism(configPolymorphism,

src/main/java/org/eclipse/yasson/internal/deserializer/DeserializationModelCreator.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -269,9 +269,10 @@ private ModelDeserializer<JsonParser> createCollectionDeserializer(CachedItem ca
269269
? ((ParameterizedType) type).getActualTypeArguments()[0]
270270
: Object.class;
271271
colType = ReflectionUtils.resolveType(chain, colType);
272+
ClassModel classModel = jsonbContext.getMappingContext().getOrCreateClassModel(ReflectionUtils.getRawType(colType));
272273
ModelDeserializer<JsonParser> typeProcessor = typeProcessor(chain,
273274
colType,
274-
propertyCustomization,
275+
classModel.getClassCustomization(),
275276
JustReturn.instance());
276277
CollectionDeserializer collectionDeserializer = new CollectionDeserializer(typeProcessor);
277278
CollectionInstanceCreator instanceDeserializer = new CollectionInstanceCreator(collectionDeserializer, type);

src/main/java/org/eclipse/yasson/internal/serializer/SerializationModelCreator.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -286,7 +286,10 @@ private ModelSerializer createCollectionSerializer(LinkedList<Type> chain,
286286
Type colType = type instanceof ParameterizedType
287287
? ((ParameterizedType) type).getActualTypeArguments()[0]
288288
: Object.class;
289-
ModelSerializer typeSerializer = memberSerializer(chain, colType, customization, false);
289+
Type resolvedKey = ReflectionUtils.resolveType(chain, colType);
290+
Class<?> rawClass = ReflectionUtils.getRawType(resolvedKey);
291+
ClassModel classModel = jsonbContext.getMappingContext().getOrCreateClassModel(rawClass);
292+
ModelSerializer typeSerializer = memberSerializer(chain, colType, classModel.getClassCustomization(), false);
290293
CollectionSerializer collectionSerializer = new CollectionSerializer(typeSerializer);
291294
KeyWriter keyWriter = new KeyWriter(collectionSerializer);
292295
NullVisibilitySwitcher nullVisibilitySwitcher = new NullVisibilitySwitcher(true, keyWriter);

src/main/java16/org/eclipse/yasson/internal/ClassMultiReleaseExtension.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Optional;
1919

2020
import jakarta.json.bind.JsonbException;
21+
import jakarta.json.bind.config.PropertyNamingStrategy;
2122

2223
import org.eclipse.yasson.internal.model.JsonbCreator;
2324
import org.eclipse.yasson.internal.model.Property;
@@ -47,10 +48,11 @@ static boolean isSpecialAccessorMethod(Method method, Map<String, Property> clas
4748

4849
static JsonbCreator findCreator(Class<?> clazz,
4950
Constructor<?>[] declaredConstructors,
50-
AnnotationIntrospector introspector) {
51+
AnnotationIntrospector introspector,
52+
PropertyNamingStrategy propertyNamingStrategy) {
5153
if (clazz.isRecord()) {
5254
if (declaredConstructors.length == 1) {
53-
return introspector.createJsonbCreator(declaredConstructors[0], null, clazz);
55+
return introspector.createJsonbCreator(declaredConstructors[0], null, clazz, propertyNamingStrategy);
5456
}
5557
}
5658
return null;

src/test/java/org/eclipse/yasson/internal/AnnotationIntrospectorTest.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -12,6 +12,7 @@
1212

1313
package org.eclipse.yasson.internal;
1414

15+
import jakarta.json.bind.config.PropertyNamingStrategy;
1516
import org.junit.jupiter.api.*;
1617
import static org.junit.jupiter.api.Assertions.*;
1718

@@ -40,6 +41,7 @@
4041
*/
4142
public class AnnotationIntrospectorTest {
4243
private final JsonbContext jsonbContext = new JsonbContext(new JsonbConfig(), JsonProvider.provider());
44+
private final PropertyNamingStrategy propertyNamingStrategy = jsonbContext.getConfigProperties().getPropertyNamingStrategy();
4345

4446
/**
4547
* class under test.
@@ -48,29 +50,29 @@ public class AnnotationIntrospectorTest {
4850

4951
@Test
5052
public void testObjectShouldBeCreateableFromJsonbAnnotatedConstructor() {
51-
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedConstructor.class);
53+
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedConstructor.class, propertyNamingStrategy);
5254
assertParameters(ObjectWithJsonbCreatorAnnotatedConstructor.parameters(), creator);
5355
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAnnotatedConstructor.example(), creator);
5456
}
5557

5658
@Test
5759
public void testObjectShouldBeCreateableFromJsonbAnnotatedStaticFactoryMethod() {
58-
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedFactoryMethod.class);
60+
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedFactoryMethod.class, propertyNamingStrategy);
5961
assertParameters(ObjectWithJsonbCreatorAnnotatedFactoryMethod.parameters(), creator);
6062
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAnnotatedFactoryMethod.example(), creator);
6163
}
6264

6365
@Test
6466
public void testObjectShouldBeCreateableFromJsonbAnnotatedStaticFactoryMethodIgnoringConstructorPorperties() {
65-
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAndConstructorPropertiesAnnotation.class);
67+
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAndConstructorPropertiesAnnotation.class, propertyNamingStrategy);
6668
assertParameters(ObjectWithJsonbCreatorAndConstructorPropertiesAnnotation.parameters(), creator);
6769
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAndConstructorPropertiesAnnotation.example(), creator);
6870
}
6971

7072
@Test
7173
public void testJsonbAnnotatedProtectedConstructorLeadsToAnException() {
7274
assertThrows(JsonbException.class, () -> {
73-
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedProtectedConstructor.class);
75+
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedProtectedConstructor.class, propertyNamingStrategy);
7476
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAnnotatedProtectedConstructor.example(), creator);
7577
});
7678
}
@@ -79,21 +81,21 @@ public void testJsonbAnnotatedProtectedConstructorLeadsToAnException() {
7981
@Disabled
8082
@Test
8183
public void testNoArgConstructorShouldBePreferredOverUnusableJsonbAnnotatedProtectedConstructor() {
82-
JsonbCreator creator = instrospector.getCreator(ObjectWithNoArgAndJsonbCreatorAnnotatedProtectedConstructor.class);
84+
JsonbCreator creator = instrospector.getCreator(ObjectWithNoArgAndJsonbCreatorAnnotatedProtectedConstructor.class, propertyNamingStrategy);
8385
assertParameters(ObjectWithNoArgAndJsonbCreatorAnnotatedProtectedConstructor.parameters(), creator);
8486
assertCreatedInstanceContainsAllParameters(ObjectWithNoArgAndJsonbCreatorAnnotatedProtectedConstructor.example(), creator);
8587
}
8688

8789
@Test
8890
public void testMoreThanOneAnnotatedCreatorMethodShouldLeadToAnException() {
8991
assertThrows(JsonbException.class,
90-
() -> instrospector.getCreator(ObjectWithTwoJsonbCreatorAnnotatedSpots.class),
92+
() -> instrospector.getCreator(ObjectWithTwoJsonbCreatorAnnotatedSpots.class, propertyNamingStrategy),
9193
() -> "More than one @" + JsonbCreator.class.getSimpleName());
9294
}
9395

9496
@Test
9597
public void testCreatorShouldBeNullOnMissingConstructorAnnotation() {
96-
assertNull(instrospector.getCreator(ObjectWithoutAnnotatedConstructor.class));
98+
assertNull(instrospector.getCreator(ObjectWithoutAnnotatedConstructor.class, propertyNamingStrategy));
9799
}
98100

99101
}

src/test/java/org/eclipse/yasson/internal/AnnotationIntrospectorWithoutOptionalModulesTest.java

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -12,19 +12,21 @@
1212

1313
package org.eclipse.yasson.internal;
1414

15-
import org.junit.jupiter.api.*;
16-
import static org.junit.jupiter.api.Assertions.*;
17-
1815
import static org.eclipse.yasson.internal.AnnotationIntrospectorTestAsserts.assertCreatedInstanceContainsAllParameters;
1916
import static org.eclipse.yasson.internal.AnnotationIntrospectorTestAsserts.assertParameters;
17+
import static org.junit.jupiter.api.Assertions.assertNull;
18+
import static org.junit.jupiter.api.Assertions.fail;
19+
20+
import jakarta.json.bind.JsonbConfig;
21+
import jakarta.json.bind.config.PropertyNamingStrategy;
22+
import jakarta.json.spi.JsonProvider;
2023

2124
import org.eclipse.yasson.internal.AnnotationIntrospectorTestFixtures.ObjectWithJsonbCreatorAnnotatedConstructor;
2225
import org.eclipse.yasson.internal.AnnotationIntrospectorTestFixtures.ObjectWithJsonbCreatorAnnotatedFactoryMethod;
2326
import org.eclipse.yasson.internal.AnnotationIntrospectorTestFixtures.ObjectWithoutAnnotatedConstructor;
2427
import org.eclipse.yasson.internal.model.JsonbCreator;
2528

26-
import jakarta.json.bind.JsonbConfig;
27-
import jakarta.json.spi.JsonProvider;
29+
import org.junit.jupiter.api.Test;
2830

2931
/**
3032
* Tests the {@link AnnotationIntrospector} with missing optional module "java.deskop", <br>
@@ -41,6 +43,7 @@ public class AnnotationIntrospectorWithoutOptionalModulesTest {
4143
* class under test.
4244
*/
4345
private static final AnnotationIntrospector instrospector = new AnnotationIntrospector(new JsonbContext(new JsonbConfig(), JsonProvider.provider()));
46+
private final PropertyNamingStrategy propertyNamingStrategy = propertyName -> propertyName;
4447

4548
@Test
4649
public void testNoConstructorPropertiesAnnotationWithoutOptionalModules() {
@@ -55,19 +58,19 @@ public void testNoConstructorPropertiesAnnotationWithoutOptionalModules() {
5558

5659
@Test
5760
public void testCreatorShouldBeNullOnMissingConstructorAnnotation() {
58-
assertNull(instrospector.getCreator(ObjectWithoutAnnotatedConstructor.class));
61+
assertNull(instrospector.getCreator(ObjectWithoutAnnotatedConstructor.class, propertyNamingStrategy));
5962
}
6063

6164
@Test
6265
public void testObjectShouldBeCreateableFromJsonbAnnotatedConstructorWithoutOptionalModules() {
63-
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedConstructor.class);
66+
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedConstructor.class, propertyNamingStrategy);
6467
assertParameters(ObjectWithJsonbCreatorAnnotatedConstructor.parameters(), creator);
6568
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAnnotatedConstructor.example(), creator);
6669
}
6770

6871
@Test
6972
public void testObjectShouldBeCreateableFromJsonbAnnotatedStaticFactoryMethodWithoutOptionalModules() {
70-
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedFactoryMethod.class);
73+
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedFactoryMethod.class, propertyNamingStrategy);
7174
assertParameters(ObjectWithJsonbCreatorAnnotatedFactoryMethod.parameters(), creator);
7275
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAnnotatedFactoryMethod.example(), creator);
7376
}

0 commit comments

Comments
 (0)