Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #276: Added special handling for Optional<> JsonbCreator arguments #278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -115,11 +116,16 @@ private T createInstance(Class<T> rawType, JsonbCreator creator) {
final List<Object> paramValues = new ArrayList<>();
for(CreatorModel param : creator.getParams()) {
final ValueWrapper valueWrapper = values.get(param.getName());
//required by spec
if (valueWrapper == null){
throw new JsonbException(Messages.getMessage(MessageKeys.JSONB_CREATOR_MISSING_PROPERTY, param.getName()));
if ( valueWrapper == null ) {
if (ReflectionUtils.getRawType(param.getType()).isAssignableFrom( Optional.class )) {
paramValues.add( Optional.empty() );
} else {
//required by spec
throw new JsonbException( Messages.getMessage( MessageKeys.JSONB_CREATOR_MISSING_PROPERTY, param.getName() ) );
}
} else {
paramValues.add( valueWrapper.getValue() );
}
paramValues.add(valueWrapper.getValue());
}
instance = creator.call(paramValues.toArray(), rawType);
return instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public void testRootConstructor() {
assertEquals(new BigDecimal("25"), pojo.bigDec);
}

@Test
public void testRootConstructorOptional() {
String json = "{\"str1\":\"abc\",\"bigDec\":25}";
final Jsonb jsonb = JsonbBuilder.create();
CreatorConstructorPojoOptional pojo = jsonb.fromJson(json, CreatorConstructorPojoOptional.class);
assertEquals("abc", pojo.str1);
assertEquals(false, pojo.str2.isPresent());
assertEquals(new BigDecimal("25"), pojo.bigDec);
}

@Test
public void testRootFactoryMethod() {
String json = "{\"par1\":\"abc\",\"par2\":\"def\",\"bigDec\":25}";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.eclipse.yasson.customization.model;

import javax.json.bind.annotation.JsonbCreator;
import javax.json.bind.annotation.JsonbProperty;
import java.math.BigDecimal;
import java.util.Optional;

/**
* @author misl
*/
public class CreatorConstructorPojoOptional {

public String str1;

public Optional<String> str2;

public BigDecimal bigDec;

public CreatorFactoryMethodPojo innerFactoryCreator;

@JsonbCreator
public CreatorConstructorPojoOptional( @JsonbProperty("str1") String str1, @JsonbProperty("str2") Optional<String> str2) {
this.str1 = str1;
this.str2 = str2;
}

}