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

977 add digits rule #978

Merged
merged 3 commits into from
May 13, 2019
Merged
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
@@ -0,0 +1,50 @@
/**
* Copyright © 2010-2017 Nokia
* <p>
* Licensed 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.jsonschema2pojo.rules;

import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JFieldVar;
import org.jsonschema2pojo.Schema;

import javax.validation.constraints.Digits;
import java.util.NoSuchElementException;

public class DigitsRule implements Rule<JFieldVar, JFieldVar> {

private final RuleFactory ruleFactory;

protected DigitsRule(RuleFactory ruleFactory) {
this.ruleFactory = ruleFactory;
}

@Override
public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {

if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()
&& node.has("integerDigits") && node.has("fractionalDigits")) {

JAnnotationUse annotation = field.annotate(Digits.class);

annotation.param("integer", node.get("integerDigits").asInt());
annotation.param("fraction", node.get("fractionalDigits").asInt());
}

return field;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public JDefinedClass apply(String nodeName, JsonNode node, JsonNode parent, JDef

ruleFactory.getMinLengthMaxLengthRule().apply(nodeName, node, parent, field, schema);

ruleFactory.getDigitsRule().apply(nodeName, node, parent, field, schema);

if (isObject(node) || isArray(node)) {
ruleFactory.getValidRule().apply(nodeName, node, parent, field, schema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,17 @@ public Rule<JFieldVar, JFieldVar> getMinLengthMaxLengthRule() {
return new MinLengthMaxLengthRule(this);
}

/**
* Provides a rule instance that should be applied when a property
* declaration is found in the schema, to assign he digits validation
* on that property.
*
* @return a schema rule that can handle the "digits" declaration.
*/
public Rule<JFieldVar, JFieldVar> getDigitsRule() {
return new DigitsRule(this);
}

/**
* Provides a rule instance that should be applied when a "pattern"
* declaration is found in the schema for a property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public void factoryMethodsCreateRules() {

assertThat(ruleFactory.getValidRule(), notNullValue());

assertThat(ruleFactory.getDigitsRule(), notNullValue());

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -223,6 +224,46 @@ public void jsr303SizeValidationIsAddedForSchemaRuleMaxLength() throws ClassNotF
assertNumberOfConstraintViolationsOn(invalidInstance, is(1));
}

@Test
public void jsr303DigitsValidationIsAddedForSchemaRuleDigits() throws ClassNotFoundException {

ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/jsr303/digits.json", "com.example",
config("includeJsr303Annotations", true, "useBigDecimals", true));

Class generatedType = resultsClassLoader.loadClass("com.example.Digits");

// positive value
Object validInstance = createInstanceWithPropertyValue(generatedType, "decimal", new BigDecimal("12345.1234567890"));

assertNumberOfConstraintViolationsOn(validInstance, is(0));

// negative value
validInstance = createInstanceWithPropertyValue(generatedType, "decimal", new BigDecimal("-12345.0123456789"));

assertNumberOfConstraintViolationsOn(validInstance, is(0));

// zero value
validInstance = createInstanceWithPropertyValue(generatedType, "decimal", new BigDecimal("0.0"));

assertNumberOfConstraintViolationsOn(validInstance, is(0));

// too many integer digits
Object invalidInstance = createInstanceWithPropertyValue(generatedType, "decimal", new BigDecimal("123456.0123456789"));

assertNumberOfConstraintViolationsOn(invalidInstance, is(1));

// too many fractional digits
invalidInstance = createInstanceWithPropertyValue(generatedType, "decimal", new BigDecimal("12345.12345678901"));

assertNumberOfConstraintViolationsOn(invalidInstance, is(1));

// too many integer & fractional digits
invalidInstance = createInstanceWithPropertyValue(generatedType, "decimal", new BigDecimal("123456.12345678901"));

assertNumberOfConstraintViolationsOn(invalidInstance, is(1));

}

@Test
public void jsr303ValidAnnotationIsAddedForObject() throws ClassNotFoundException {
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/jsr303/validObject.json", "com.example",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "object",
"properties": {
"decimal": {
"type": "number",
"integerDigits": 5,
"fractionalDigits": 10
}
}
}