This is an automated email from the ASF dual-hosted git repository. rmannibucau pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/johnzon.git
The following commit(s) were added to refs/heads/master by this push: new b25243b2 [JOHNZON-390] ensure jsonb creator/constructor parameters respect naming strategy b25243b2 is described below commit b25243b23f36f257e47d8fce9b914bb5c882889c Author: Romain Manni-Bucau <rmannibu...@gmail.com> AuthorDate: Thu Dec 8 11:39:58 2022 +0100 [JOHNZON-390] ensure jsonb creator/constructor parameters respect naming strategy --- .../org/apache/johnzon/jsonb/JsonbAccessMode.java | 6 +-- .../java/org/apache/johnzon/jsonb/NamingTest.java | 61 ++++++++++++++++++++++ .../org/apache/johnzon/jsonb/test/JsonbRule.java | 5 ++ .../jsonschema/JsonSchemaValidatorFactory.java | 15 +++--- .../jsonschema/JsonSchemaValidatorTest.java | 29 ++++++++++ pom.xml | 1 + 6 files changed, 108 insertions(+), 9 deletions(-) diff --git a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java index a0d0d8c6..49411f56 100644 --- a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java +++ b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java @@ -252,13 +252,13 @@ public class JsonbAccessMode implements AccessMode, Closeable, Cleanable<Class<? int i = 0; for (final Parameter parameter : (finalConstructor == null ? finalFactory : finalConstructor).getParameters()) { final JsonbProperty property = getAnnotation(parameter, JsonbProperty.class); - params[i] = property != null ? + params[i] = property != null && !property.value().isEmpty() ? property.value() : (record ? ofNullable(parameter.getAnnotation(JohnzonRecord.Name.class)) .map(JohnzonRecord.Name::value) - .orElseGet(parameter::getName) : - parameter.getName()); + .orElseGet(() -> naming.translateName(parameter.getName())) : + naming.translateName(parameter.getName())); final JsonbTypeAdapter adapter = getAnnotation(parameter, JsonbTypeAdapter.class); final JsonbDateFormat dateFormat = getAnnotation(parameter, JsonbDateFormat.class); diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/NamingTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/NamingTest.java new file mode 100644 index 00000000..715523af --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/NamingTest.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.johnzon.jsonb; + +import org.apache.johnzon.jsonb.test.JsonbRule; +import org.junit.Rule; +import org.junit.Test; + +import javax.json.bind.annotation.JsonbCreator; +import javax.json.bind.config.PropertyNamingStrategy; +import java.util.StringJoiner; + +import static org.junit.Assert.assertEquals; + +public class NamingTest { + @Rule + public final JsonbRule jsonb = new JsonbRule() + .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES); + + @Test + public void lower() { + assertEquals("{\"first_name\":\"test\"}", jsonb.toJson(new Model("test"))); + assertEquals("Model[firstName='test']", jsonb.fromJson("{\"first_name\":\"test\"}", Model.class).toString()); + } + + public static class Model { + public final String firstName; + + private Model(final String firstName) { + this.firstName = firstName; + } + + @Override + public String toString() { + return new StringJoiner(", ", Model.class.getSimpleName() + "[", "]") + .add("firstName='" + firstName + "'") + .toString(); + } + + @JsonbCreator + public static Model create(final String firstName) { + return new Model(firstName); + } + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/test/JsonbRule.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/test/JsonbRule.java index 62779863..f617fe1e 100644 --- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/test/JsonbRule.java +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/test/JsonbRule.java @@ -47,6 +47,11 @@ public class JsonbRule implements TestRule, Jsonb, JsonbExtension { return this; } + public JsonbRule withPropertyNamingStrategy(final String propertyorderstrategy) { + config.withPropertyNamingStrategy(propertyorderstrategy); + return this; + } + public JsonbRule withFormatting(final boolean format) { config.withFormatting(format); return this; diff --git a/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorFactory.java b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorFactory.java index 14103bc6..d62752fc 100644 --- a/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorFactory.java +++ b/johnzon-jsonschema/src/main/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorFactory.java @@ -197,17 +197,20 @@ public class JsonSchemaValidatorFactory implements AutoCloseable { final Predicate<CharSequence> pattern = regexFactory.get().apply(obj.getKey()); final JsonObject currentSchema = obj.getValue().asJsonObject(); // no cache cause otherwise it could be in properties - return (Function<JsonValue, Stream<ValidationResult.ValidationError>>) validable -> { + return (Function<JsonValue, Stream<ValidationResult.ValidationError>>) root -> { + final JsonValue validable = Optional.ofNullable(valueProvider) + .map(provider -> provider.apply(root)) + .orElse(root); if (validable.getValueType() != JsonValue.ValueType.OBJECT) { return Stream.empty(); } return validable.asJsonObject().entrySet().stream() .filter(e -> pattern.test(e.getKey())) - .flatMap(e -> { - final String[] subPath = Stream.concat(Stream.of(path), Stream.of(e.getKey())).toArray(String[]::new); - final Function<JsonValue, JsonValue> provider = new ChainedValueAccessor(valueProvider, e.getKey()); - return buildValidator(subPath, currentSchema, provider).apply(validable); - }); + .flatMap(e -> buildValidator( + Stream.concat(Stream.of(path), Stream.of(e.getKey())).toArray(String[]::new), + currentSchema, + o -> o.asJsonObject().get(e.getKey())) + .apply(validable)); }; }) .collect(toList())) diff --git a/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorTest.java b/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorTest.java index 3331ed38..adae7ae8 100644 --- a/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorTest.java +++ b/johnzon-jsonschema/src/test/java/org/apache/johnzon/jsonschema/JsonSchemaValidatorTest.java @@ -49,6 +49,35 @@ public class JsonSchemaValidatorTest { factory.close(); } + @Test + public void patternPropertiesNested() { + try (final JsonSchemaValidator validator = factory.newInstance(jsonFactory.createObjectBuilder() + .add("type", "object") + .add("properties", jsonFactory.createObjectBuilder() + .add("nested", jsonFactory.createObjectBuilder() + .add("type", "object") + .add("patternProperties", jsonFactory.createObjectBuilder() + .add("[0-9]+", jsonFactory.createObjectBuilder().add("type", "number")) + .build()))) + .build())) { + + assertTrue(validator.apply(jsonFactory.createObjectBuilder().build()).isSuccess()); + assertTrue(validator.apply(jsonFactory.createObjectBuilder() + .add("nested", jsonFactory.createObjectBuilder() + .add("1", 1) + .build()) + .build()) + .isSuccess()); + + final ValidationResult result = validator.apply(jsonFactory.createObjectBuilder() + .add("nested", jsonFactory.createObjectBuilder() + .add("1", "test") + .build()) + .build()); + assertFalse(result.toString(), result.isSuccess()); + } + } + @Test public void rootRequired() { final JsonSchemaValidator validator = factory.newInstance(jsonFactory.createObjectBuilder() diff --git a/pom.xml b/pom.xml index 13dfe016..1a43bed4 100644 --- a/pom.xml +++ b/pom.xml @@ -277,6 +277,7 @@ <encoding>${project.build.sourceEncoding}</encoding> <showDeprecation>true</showDeprecation> <showWarnings>true</showWarnings> + <parameters>true</parameters> </configuration> </plugin>