Skip to content

Commit

Permalink
Merge branch 'eclipse-ee4j:master' into npe-custom-timestamp-formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
greek1979 authored Feb 16, 2023
2 parents 3e482ef + 66bef09 commit 12d9fac
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import jakarta.json.bind.annotation.JsonbTypeInfo;
import jakarta.json.bind.annotation.JsonbTypeSerializer;
import jakarta.json.bind.annotation.JsonbVisibility;
import jakarta.json.bind.config.PropertyNamingStrategy;
import jakarta.json.bind.config.PropertyVisibilityStrategy;
import jakarta.json.bind.serializer.JsonbDeserializer;
import jakarta.json.bind.serializer.JsonbSerializer;
Expand Down Expand Up @@ -152,10 +153,12 @@ private String getJsonbPropertyCustomizedName(Property property, JsonbAnnotatedE
/**
* Searches for JsonbCreator annotation on constructors and static methods.
*
* @param clazz class to search
* @param clazz class to search
* @param propertyNamingStrategy The naming strategy to use for the ${@code JsonbConstructor} annotation,
* if set and no {@code JsonbProperty} annotations are present.
* @return JsonbCreator metadata object
*/
public JsonbCreator getCreator(Class<?> clazz) {
public JsonbCreator getCreator(Class<?> clazz, PropertyNamingStrategy propertyNamingStrategy) {
JsonbCreator jsonbCreator = null;
Constructor<?>[] declaredConstructors =
AccessController.doPrivileged((PrivilegedAction<Constructor<?>[]>) clazz::getDeclaredConstructors);
Expand All @@ -164,7 +167,7 @@ public JsonbCreator getCreator(Class<?> clazz) {
final jakarta.json.bind.annotation.JsonbCreator annot = findAnnotation(constructor.getDeclaredAnnotations(),
jakarta.json.bind.annotation.JsonbCreator.class);
if (annot != null) {
jsonbCreator = createJsonbCreator(constructor, jsonbCreator, clazz);
jsonbCreator = createJsonbCreator(constructor, jsonbCreator, clazz, propertyNamingStrategy);
}
}

Expand All @@ -179,19 +182,19 @@ public JsonbCreator getCreator(Class<?> clazz) {
method,
clazz));
}
jsonbCreator = createJsonbCreator(method, jsonbCreator, clazz);
jsonbCreator = createJsonbCreator(method, jsonbCreator, clazz, propertyNamingStrategy);
}
}
if (jsonbCreator == null) {
jsonbCreator = ClassMultiReleaseExtension.findCreator(clazz, declaredConstructors, this);
jsonbCreator = ClassMultiReleaseExtension.findCreator(clazz, declaredConstructors, this, propertyNamingStrategy);
if (jsonbCreator == null) {
jsonbCreator = constructorPropertiesIntrospector.getCreator(declaredConstructors);
}
}
return jsonbCreator;
}

JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Class<?> clazz) {
JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Class<?> clazz, PropertyNamingStrategy propertyNamingStrategy) {
if (existing != null) {
throw new JsonbException(Messages.getMessage(MessageKeys.MULTIPLE_JSONB_CREATORS, clazz));
}
Expand All @@ -205,7 +208,8 @@ JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Cl
if (jsonbPropertyAnnotation != null && !jsonbPropertyAnnotation.value().isEmpty()) {
creatorModels[i] = new CreatorModel(jsonbPropertyAnnotation.value(), parameter, executable, jsonbContext);
} else {
creatorModels[i] = new CreatorModel(parameter.getName(), parameter, executable, jsonbContext);
final String translatedParameterName = propertyNamingStrategy.translateName(parameter.getName());
creatorModels[i] = new CreatorModel(translatedParameterName, parameter, executable, jsonbContext);
}
}

Expand Down Expand Up @@ -779,16 +783,19 @@ public Set<Class<?>> collectInterfaces(Class<?> cls) {
/**
* Processes customizations.
*
* @param clsElement Element to process.
* @param clsElement Element to process.
* @param propertyNamingStrategy The naming strategy to use for the ${@code JsonbConstructor} annotation,
* if set and no {@code JsonbProperty} annotations are present.
* @return Populated {@link ClassCustomization} instance.
*/
public ClassCustomization introspectCustomization(JsonbAnnotatedElement<Class<?>> clsElement,
ClassCustomization parentCustomization) {
ClassCustomization parentCustomization,
PropertyNamingStrategy propertyNamingStrategy) {
return ClassCustomization.builder()
.nillable(isClassNillable(clsElement))
.dateTimeFormatter(getJsonbDateFormat(clsElement))
.numberFormatter(getJsonbNumberFormat(clsElement))
.creator(getCreator(clsElement.getElement()))
.creator(getCreator(clsElement.getElement(), propertyNamingStrategy))
.propertyOrder(getPropertyOrder(clsElement))
.adapterBinding(getAdapterBinding(clsElement))
.serializerBinding(getSerializerBinding(clsElement))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Optional;

import jakarta.json.bind.JsonbException;
import jakarta.json.bind.config.PropertyNamingStrategy;

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

static JsonbCreator findCreator(Class<?> clazz,
Constructor<?>[] declaredConstructors,
AnnotationIntrospector introspector) {
AnnotationIntrospector introspector,
PropertyNamingStrategy propertyNamingStrategy) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ private static Function<Class<?>, ClassModel> createParseClassModelFunction(Clas
.introspectCustomization(clsElement,
parentClassModel == null
? ClassCustomization.empty()
: parentClassModel.getClassCustomization());
: parentClassModel.getClassCustomization(),
jsonbContext.getConfigProperties().getPropertyNamingStrategy());
// PolymorphismSupport configPolymorphism = jsonbContext.getConfigProperties().getPolymorphismSupport();
// if (configPolymorphism != null) {
// customization = mergeConfigAndAnnotationPolymorphism(configPolymorphism,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023 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
Expand Down Expand Up @@ -269,9 +269,10 @@ private ModelDeserializer<JsonParser> createCollectionDeserializer(CachedItem ca
? ((ParameterizedType) type).getActualTypeArguments()[0]
: Object.class;
colType = ReflectionUtils.resolveType(chain, colType);
ClassModel classModel = jsonbContext.getMappingContext().getOrCreateClassModel(ReflectionUtils.getRawType(colType));
ModelDeserializer<JsonParser> typeProcessor = typeProcessor(chain,
colType,
propertyCustomization,
classModel.getClassCustomization(),
JustReturn.instance());
CollectionDeserializer collectionDeserializer = new CollectionDeserializer(typeProcessor);
CollectionInstanceCreator instanceDeserializer = new CollectionInstanceCreator(collectionDeserializer, type);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023 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
Expand Down Expand Up @@ -286,7 +286,10 @@ private ModelSerializer createCollectionSerializer(LinkedList<Type> chain,
Type colType = type instanceof ParameterizedType
? ((ParameterizedType) type).getActualTypeArguments()[0]
: Object.class;
ModelSerializer typeSerializer = memberSerializer(chain, colType, customization, false);
Type resolvedKey = ReflectionUtils.resolveType(chain, colType);
Class<?> rawClass = ReflectionUtils.getRawType(resolvedKey);
ClassModel classModel = jsonbContext.getMappingContext().getOrCreateClassModel(rawClass);
ModelSerializer typeSerializer = memberSerializer(chain, colType, classModel.getClassCustomization(), false);
CollectionSerializer collectionSerializer = new CollectionSerializer(typeSerializer);
KeyWriter keyWriter = new KeyWriter(collectionSerializer);
NullVisibilitySwitcher nullVisibilitySwitcher = new NullVisibilitySwitcher(true, keyWriter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Optional;

import jakarta.json.bind.JsonbException;
import jakarta.json.bind.config.PropertyNamingStrategy;

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

static JsonbCreator findCreator(Class<?> clazz,
Constructor<?>[] declaredConstructors,
AnnotationIntrospector introspector) {
AnnotationIntrospector introspector,
PropertyNamingStrategy propertyNamingStrategy) {
if (clazz.isRecord()) {
if (declaredConstructors.length == 1) {
return introspector.createJsonbCreator(declaredConstructors[0], null, clazz);
return introspector.createJsonbCreator(declaredConstructors[0], null, clazz, propertyNamingStrategy);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 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
Expand All @@ -12,6 +12,7 @@

package org.eclipse.yasson.internal;

import jakarta.json.bind.config.PropertyNamingStrategy;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -40,6 +41,7 @@
*/
public class AnnotationIntrospectorTest {
private final JsonbContext jsonbContext = new JsonbContext(new JsonbConfig(), JsonProvider.provider());
private final PropertyNamingStrategy propertyNamingStrategy = jsonbContext.getConfigProperties().getPropertyNamingStrategy();

/**
* class under test.
Expand All @@ -48,29 +50,29 @@ public class AnnotationIntrospectorTest {

@Test
public void testObjectShouldBeCreateableFromJsonbAnnotatedConstructor() {
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedConstructor.class);
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedConstructor.class, propertyNamingStrategy);
assertParameters(ObjectWithJsonbCreatorAnnotatedConstructor.parameters(), creator);
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAnnotatedConstructor.example(), creator);
}

@Test
public void testObjectShouldBeCreateableFromJsonbAnnotatedStaticFactoryMethod() {
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedFactoryMethod.class);
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedFactoryMethod.class, propertyNamingStrategy);
assertParameters(ObjectWithJsonbCreatorAnnotatedFactoryMethod.parameters(), creator);
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAnnotatedFactoryMethod.example(), creator);
}

@Test
public void testObjectShouldBeCreateableFromJsonbAnnotatedStaticFactoryMethodIgnoringConstructorPorperties() {
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAndConstructorPropertiesAnnotation.class);
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAndConstructorPropertiesAnnotation.class, propertyNamingStrategy);
assertParameters(ObjectWithJsonbCreatorAndConstructorPropertiesAnnotation.parameters(), creator);
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAndConstructorPropertiesAnnotation.example(), creator);
}

@Test
public void testJsonbAnnotatedProtectedConstructorLeadsToAnException() {
assertThrows(JsonbException.class, () -> {
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedProtectedConstructor.class);
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedProtectedConstructor.class, propertyNamingStrategy);
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAnnotatedProtectedConstructor.example(), creator);
});
}
Expand All @@ -79,21 +81,21 @@ public void testJsonbAnnotatedProtectedConstructorLeadsToAnException() {
@Disabled
@Test
public void testNoArgConstructorShouldBePreferredOverUnusableJsonbAnnotatedProtectedConstructor() {
JsonbCreator creator = instrospector.getCreator(ObjectWithNoArgAndJsonbCreatorAnnotatedProtectedConstructor.class);
JsonbCreator creator = instrospector.getCreator(ObjectWithNoArgAndJsonbCreatorAnnotatedProtectedConstructor.class, propertyNamingStrategy);
assertParameters(ObjectWithNoArgAndJsonbCreatorAnnotatedProtectedConstructor.parameters(), creator);
assertCreatedInstanceContainsAllParameters(ObjectWithNoArgAndJsonbCreatorAnnotatedProtectedConstructor.example(), creator);
}

@Test
public void testMoreThanOneAnnotatedCreatorMethodShouldLeadToAnException() {
assertThrows(JsonbException.class,
() -> instrospector.getCreator(ObjectWithTwoJsonbCreatorAnnotatedSpots.class),
() -> instrospector.getCreator(ObjectWithTwoJsonbCreatorAnnotatedSpots.class, propertyNamingStrategy),
() -> "More than one @" + JsonbCreator.class.getSimpleName());
}

@Test
public void testCreatorShouldBeNullOnMissingConstructorAnnotation() {
assertNull(instrospector.getCreator(ObjectWithoutAnnotatedConstructor.class));
assertNull(instrospector.getCreator(ObjectWithoutAnnotatedConstructor.class, propertyNamingStrategy));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 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
Expand All @@ -12,19 +12,21 @@

package org.eclipse.yasson.internal;

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

import static org.eclipse.yasson.internal.AnnotationIntrospectorTestAsserts.assertCreatedInstanceContainsAllParameters;
import static org.eclipse.yasson.internal.AnnotationIntrospectorTestAsserts.assertParameters;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;

import jakarta.json.bind.JsonbConfig;
import jakarta.json.bind.config.PropertyNamingStrategy;
import jakarta.json.spi.JsonProvider;

import org.eclipse.yasson.internal.AnnotationIntrospectorTestFixtures.ObjectWithJsonbCreatorAnnotatedConstructor;
import org.eclipse.yasson.internal.AnnotationIntrospectorTestFixtures.ObjectWithJsonbCreatorAnnotatedFactoryMethod;
import org.eclipse.yasson.internal.AnnotationIntrospectorTestFixtures.ObjectWithoutAnnotatedConstructor;
import org.eclipse.yasson.internal.model.JsonbCreator;

import jakarta.json.bind.JsonbConfig;
import jakarta.json.spi.JsonProvider;
import org.junit.jupiter.api.Test;

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

@Test
public void testNoConstructorPropertiesAnnotationWithoutOptionalModules() {
Expand All @@ -55,19 +58,19 @@ public void testNoConstructorPropertiesAnnotationWithoutOptionalModules() {

@Test
public void testCreatorShouldBeNullOnMissingConstructorAnnotation() {
assertNull(instrospector.getCreator(ObjectWithoutAnnotatedConstructor.class));
assertNull(instrospector.getCreator(ObjectWithoutAnnotatedConstructor.class, propertyNamingStrategy));
}

@Test
public void testObjectShouldBeCreateableFromJsonbAnnotatedConstructorWithoutOptionalModules() {
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedConstructor.class);
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedConstructor.class, propertyNamingStrategy);
assertParameters(ObjectWithJsonbCreatorAnnotatedConstructor.parameters(), creator);
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAnnotatedConstructor.example(), creator);
}

@Test
public void testObjectShouldBeCreateableFromJsonbAnnotatedStaticFactoryMethodWithoutOptionalModules() {
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedFactoryMethod.class);
JsonbCreator creator = instrospector.getCreator(ObjectWithJsonbCreatorAnnotatedFactoryMethod.class, propertyNamingStrategy);
assertParameters(ObjectWithJsonbCreatorAnnotatedFactoryMethod.parameters(), creator);
assertCreatedInstanceContainsAllParameters(ObjectWithJsonbCreatorAnnotatedFactoryMethod.example(), creator);
}
Expand Down
Loading

0 comments on commit 12d9fac

Please sign in to comment.