Skip to content
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
4 changes: 4 additions & 0 deletions johnzon-jsonb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@
<exclude>**/AnnotationTypeInfoTest</exclude>
</excludes>

<environmentVariables>
<LC_ALL>en_US.UTF-8</LC_ALL>
</environmentVariables>

<systemPropertyVariables>
<jimage.dir>${project.build.directory}/jimage</jimage.dir>
<signature.sigTestClasspath>${project.build.directory}/signaturedirectory/jakarta.json.bind-api.jar:${project.build.directory}/jimage/java.base:${project.build.directory}/jimage/java.rmi:${project.build.directory}/jimage/java.sql:${project.build.directory}/jimage/java.naming</signature.sigTestClasspath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.johnzon.mapper.SerializeValueFilter;
import org.apache.johnzon.mapper.access.AccessMode;
import org.apache.johnzon.mapper.access.FieldAndMethodAccessMode;
import org.apache.johnzon.mapper.access.MethodAccessMode;
import org.apache.johnzon.mapper.converter.BigDecimalConverter;
import org.apache.johnzon.mapper.converter.LocaleConverter;
import org.apache.johnzon.mapper.internal.AdapterKey;
Expand Down Expand Up @@ -211,24 +212,10 @@ public Jsonb build() {

final AccessMode accessMode = config.getProperty("johnzon.accessMode")
.map(this::toAccessMode)
.orElseGet(() -> new JsonbAccessMode(
.orElseGet(() -> newJsonbAccessMode(
propertyNamingStrategy, orderValue, visibilityStrategy,
!namingStrategyValue.orElse("").equals(PropertyNamingStrategy.CASE_INSENSITIVE),
builder.getAdapters(),
factory, jsonp, builderFactorySupplier, parserFactoryProvider,
config.getProperty("johnzon.accessModeDelegate")
.map(this::toAccessMode)
.orElseGet(() -> new FieldAndMethodAccessMode(true, true, false, false, true)),
// this changes in v3 of the spec so let's use this behavior which makes everyone happy by default
config.getProperty("johnzon.failOnMissingCreatorValues")
.map(this::toBool)
.orElseGet(() -> config.getProperty("jsonb.creator-parameters-required")
.map(this::toBool)
.orElse(false)),
isNillable,
config.getProperty("johnzon.supportsPrivateAccess")
.map(this::toBool)
.orElse(false), maxBigDecimalScale));
namingStrategyValue, factory, builderFactorySupplier, parserFactoryProvider,
isNillable, maxBigDecimalScale));
builder.setAccessMode(accessMode);

config.getProperty("johnzon.snippetMaxLength")
Expand All @@ -237,6 +224,12 @@ public Jsonb build() {
Integer.parseInt(it.toString()))
.ifPresent(builder::setSnippetMaxLength);

config.getProperty("johnzon.version")
.map(it -> Number.class.isInstance(it)?
Number.class.cast(it).intValue() :
Integer.parseInt(it.toString()))
.ifPresent(builder::setVersion);

config.getProperty("johnzon.use-biginteger-stringadapter")
.or(() -> Optional.ofNullable(System.getProperty("johnzon.use-biginteger-stringadapter")))
.map(Object::toString).map(Boolean::parseBoolean)
Expand Down Expand Up @@ -383,6 +376,76 @@ private Integer toInt(final Object v) {
return !Integer.class.isInstance(v) ? Integer.parseInt(v.toString()) : Integer.class.cast(v);
}

private Set<String> toExcludedMethods(final Object v) {
if (Set.class.isInstance(v)) {
return Set.class.cast(v);
}
final String str = v.toString().trim();
if (str.isEmpty()) {
return Set.of();
}
final Set<String> result = new HashSet<>();
for (final String s : str.split(",")) {
result.add(s.trim());
}
return result;
}

private JsonbAccessMode newJsonbAccessMode(final PropertyNamingStrategy propertyNamingStrategy,
final String orderValue,
final PropertyVisibilityStrategy visibilityStrategy,
final Optional<Object> namingStrategyValue,
final JohnzonAdapterFactory factory,
final Supplier<JsonBuilderFactory> builderFactorySupplier,
final Supplier<JsonParserFactory> parserFactoryProvider,
final boolean isNillable,
final int maxBigDecimalScale) {
final AccessMode delegateMode = config.getProperty("johnzon.accessModeDelegate")
.map(this::toAccessMode)
.orElseGet(() -> new FieldAndMethodAccessMode(true, true, false, false, true));
if (MethodAccessMode.class.isInstance(delegateMode)) {
ifExcludedMethods()
.ifPresent(MethodAccessMode.class.cast(delegateMode)::setExcludedMethods);
ifSupportAllRecordAttributes()
.ifPresent(v -> MethodAccessMode.class.cast(delegateMode).setSupportAllRecordAttributes(v.booleanValue()));
} else if (FieldAndMethodAccessMode.class.isInstance(delegateMode)) {
ifExcludedMethods()
.ifPresent(FieldAndMethodAccessMode.class.cast(delegateMode)::setExcludedMethods);
ifSupportAllRecordAttributes()
.ifPresent(v -> FieldAndMethodAccessMode.class.cast(delegateMode).setSupportAllRecordAttributes(v.booleanValue()));
}
return new JsonbAccessMode(
propertyNamingStrategy, orderValue, visibilityStrategy,
!namingStrategyValue.orElse("").equals(PropertyNamingStrategy.CASE_INSENSITIVE),
builder.getAdapters(),
factory, jsonp, builderFactorySupplier, parserFactoryProvider,
delegateMode,
config.getProperty("johnzon.failOnMissingCreatorValues")
.map(this::toBool)
.orElseGet(() -> config.getProperty("jsonb.creator-parameters-required")
.map(this::toBool)
.orElse(false)),
isNillable,
config.getProperty("johnzon.supportsPrivateAccess")
.map(this::toBool)
.orElse(false),
config.getProperty("johnzon.version")
.map(it -> Number.class.isInstance(it) ?
Number.class.cast(it).intValue() :
Integer.parseInt(it.toString()))
.orElse(-1), maxBigDecimalScale);
}

private Optional<Boolean> ifSupportAllRecordAttributes() {
return config.getProperty("johnzon.accessMode.supportAllRecordAttributes")
.map(this::toBool);
}

private Optional<Set<String>> ifExcludedMethods() {
return config.getProperty("johnzon.accessMode.excludedMethods")
.map(this::toExcludedMethods);
}

private AccessMode toAccessMode(final Object s) {
if (String.class.isInstance(s)) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.johnzon.mapper.Converter;
import org.apache.johnzon.mapper.JohnzonAny;
import org.apache.johnzon.mapper.JohnzonConverter;
import org.apache.johnzon.mapper.JohnzonIgnore;
import org.apache.johnzon.mapper.JohnzonRecord;
import org.apache.johnzon.mapper.MapperConverter;
import org.apache.johnzon.mapper.MappingGenerator;
Expand Down Expand Up @@ -157,6 +158,7 @@ protected Map<String, Writer> doFindWriters(Class<?> clazz) {
private final Types types = new Types();
private final boolean globalIsNillable;
private final boolean supportsPrivateAccess;
private final int version;
private final int maxBigDecimalScale;

// CHECKSTYLE:OFF
Expand Down Expand Up @@ -187,6 +189,25 @@ public JsonbAccessMode(final PropertyNamingStrategy propertyNamingStrategy, fina
final boolean supportsPrivateAccess,
final int maxBigDecimalScale) {
// CHECKSTYLE:ON
this(
propertyNamingStrategy, orderValue, visibilityStrategy, caseSensitive, defaultConverters, factory,
jsonProvider, builderFactory, parserFactory, delegate, failOnMissingCreatorValues, globalIsNillable,
supportsPrivateAccess, 0, maxBigDecimalScale);
}

// CHECKSTYLE:OFF
public JsonbAccessMode(final PropertyNamingStrategy propertyNamingStrategy, final String orderValue,
final PropertyVisibilityStrategy visibilityStrategy, final boolean caseSensitive,
final Map<AdapterKey, Adapter<?, ?>> defaultConverters, final JohnzonAdapterFactory factory,
final JsonProvider jsonProvider, final Supplier<JsonBuilderFactory> builderFactory,
final Supplier<JsonParserFactory> parserFactory,
final AccessMode delegate,
final boolean failOnMissingCreatorValues,
final boolean globalIsNillable,
final boolean supportsPrivateAccess,
final int version,
final int maxBigDecimalScale) {
// CHECKSTYLE:ON
this.globalIsNillable = globalIsNillable;
this.naming = propertyNamingStrategy;
this.order = orderValue;
Expand All @@ -200,6 +221,7 @@ public JsonbAccessMode(final PropertyNamingStrategy propertyNamingStrategy, fina
this.parserFactory = parserFactory;
this.failOnMissingCreatorValues = failOnMissingCreatorValues;
this.supportsPrivateAccess = supportsPrivateAccess;
this.version = version;
this.maxBigDecimalScale = maxBigDecimalScale;
}

Expand Down Expand Up @@ -973,6 +995,10 @@ private boolean isTransient(final DecoratedType t) {
if (t.getAnnotation(JsonbTransient.class) != null) {
return true;
}
final JohnzonIgnore johnzonIgnore = t.getAnnotation(JohnzonIgnore.class);
if (johnzonIgnore != null && johnzonIgnore.minVersion() >= 0 && version < johnzonIgnore.minVersion()) {
return true;
}
// TODO: spec requirement, this sounds wrong since you cant customize 2 kind of serializations on the same model
if (FieldAccessMode.FieldDecoratedType.class.isInstance(t)) {
final Field field = FieldAccessMode.FieldDecoratedType.class.cast(t).getField();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.apache.johnzon.core.JsonProviderImpl;
import org.apache.johnzon.jsonb.api.experimental.JsonbExtension;
import org.apache.johnzon.mapper.JohnzonIgnore;
import org.apache.johnzon.mapper.JohnzonRecord;
import org.junit.Test;

import jakarta.json.JsonNumber;
Expand Down Expand Up @@ -115,6 +117,38 @@ public void formattedNumberChecksConfiguredScaleBeforeFormatting() throws Except
}
}

@Test
public void configuredScaleLimitWithVersionedProperties() throws Exception {
for (final int version : new int[]{0, 2}) {
try (final Jsonb jsonb = JsonbBuilder.create(new JsonbConfig()
.setProperty("johnzon.version", version)
.setProperty("johnzon.max-big-decimal-scale", 3))) {
final VersionedFormattedHolder holder = new VersionedFormattedHolder();
holder.value = new BigDecimal("1e3");
assertEquals(version == 0 ? "{\"value\":\"1000\"}" :
"{\"extra\":\"visible\",\"value\":\"1000\"}", jsonb.toJson(holder));

holder.value = new BigDecimal("1e4");
assertScaleRejected(() -> jsonb.toJson(holder));
}
}
}

@Test
public void configuredScaleLimitWithRecordMethodExclusions() throws Exception {
try (final Jsonb jsonb = JsonbBuilder.create(new JsonbConfig()
.setProperty("johnzon.accessMode.supportAllRecordAttributes", true)
.setProperty("johnzon.accessMode.excludedMethods", "extra")
.setProperty("johnzon.max-big-decimal-scale", 3))) {
final FormattedRecord holder = new FormattedRecord();
holder.value = new BigDecimal("1e3");
assertEquals("{\"value\":\"1000\"}", jsonb.toJson(holder));

holder.value = new BigDecimal("1e4");
assertScaleRejected(() -> jsonb.toJson(holder));
}
}

private static void assertScaleRejected(final Jsonb jsonb, final String json) {
try {
jsonb.fromJson(json, Holder.class);
Expand Down Expand Up @@ -151,4 +185,32 @@ public static class FormattedHolder {
@JsonbNumberFormat("0")
public BigDecimal value;
}

public static class VersionedFormattedHolder {
private BigDecimal value;

@JsonbNumberFormat("0")
public BigDecimal getValue() {
return value;
}

@JohnzonIgnore(minVersion = 2)
public String getExtra() {
return "visible";
}
}

@JohnzonRecord
public static class FormattedRecord {
private BigDecimal value;

@JsonbNumberFormat("0")
public BigDecimal value() {
return value;
}

public String extra() {
return "excluded";
}
}
}
Loading
Loading