Skip to content

Use BigDecimal instead of Double in NumberFormat #82

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

Open
wants to merge 3 commits into
base: 2.x
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 @@ -14,6 +14,8 @@
import com.fasterxml.jackson.module.jsonSchema.validation.AnnotationConstraintResolver;
import com.fasterxml.jackson.module.jsonSchema.validation.ValidationConstraintResolver;

import java.math.BigDecimal;

/**
* @author cponomaryov
*/
Expand Down Expand Up @@ -75,8 +77,10 @@ private JsonSchema addValidationConstraints(JsonSchema schema, BeanProperty prop
arraySchema.setMinItems(constraintResolver.getArrayMinItems(prop));
} else if (schema.isNumberSchema()) {
NumberSchema numberSchema = schema.asNumberSchema();
numberSchema.setMaximum(constraintResolver.getNumberMaximum(prop));
numberSchema.setMinimum(constraintResolver.getNumberMinimum(prop));
Double max = constraintResolver.getNumberMaximum(prop);
numberSchema.setMaximum(maybeBigDecimal(max));
Double min = constraintResolver.getNumberMinimum(prop);
numberSchema.setMinimum(maybeBigDecimal(min));
} else if (schema.isStringSchema()) {
StringSchema stringSchema = schema.asStringSchema();
stringSchema.setMaxLength(constraintResolver.getStringMaxLength(prop));
Expand All @@ -86,4 +90,11 @@ private JsonSchema addValidationConstraints(JsonSchema schema, BeanProperty prop
return schema;
}

private BigDecimal maybeBigDecimal(Double value) {
if (value == null)
return null;
else
return new BigDecimal(value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;

import java.math.BigDecimal;

/**
* This class represents a {@link JsonSchema} as a number type
* @author jphelan
Expand All @@ -28,11 +30,15 @@ public class NumberSchema extends ValueTypeSchema

/**This attribute defines the maximum value of the instance property*/
@JsonProperty
private Double maximum = null;
private BigDecimal maximum = null;

/**This attribute defines the minimum value of the instance property*/
@JsonProperty
private Double minimum = null;
private BigDecimal minimum = null;

/** The value of the instance needs to be a multiple of this attribute */
@JsonProperty
private BigDecimal multipleOf = null;

@Override
public NumberSchema asNumberSchema() { return this; }
Expand All @@ -45,13 +51,17 @@ public Boolean getExclusiveMinimum() {
return exclusiveMinimum;
}

public Double getMaximum() {
public BigDecimal getMaximum() {
return maximum;
}

public Double getMinimum() {
public BigDecimal getMinimum() {
return minimum;
}

public BigDecimal getMultipleOf() {
return multipleOf;
}

/* (non-Javadoc)
* @see com.fasterxml.jackson.databind.jsonSchema.types.JsonSchema#getType()
Expand All @@ -73,14 +83,18 @@ public void setExclusiveMinimum(Boolean exclusiveMinimum) {
this.exclusiveMinimum = exclusiveMinimum;
}

public void setMaximum(Double maximum) {
public void setMaximum(BigDecimal maximum) {
this.maximum = maximum;
}

public void setMinimum(Double minimum) {
public void setMinimum(BigDecimal minimum) {
this.minimum = minimum;
}

public void setMultipleOf(BigDecimal multipleOf) {
this.multipleOf = multipleOf;
}

@Override
public boolean equals(Object obj)
{
Expand All @@ -96,7 +110,7 @@ protected boolean _equals(NumberSchema that)
equals(getExclusiveMinimum(), that.getExclusiveMinimum()) &&
equals(getMaximum(), that.getMaximum()) &&
equals(getMinimum(), that.getMinimum()) &&
equals(getMultipleOf(), that.getMultipleOf()) &&
super._equals(that);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.module.jsonSchema.types.StringSchema;

import javax.validation.constraints.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -250,14 +251,14 @@ private Object[][] listTestData() {

private Object[][] numberTestData() {
return new Object[][] {{"numberWithoutConstraints", null, null},
{"numberWithMin", 5d, null},
{"numberWithDecimalMin", 5.5, null},
{"numberWithMax", null, 6d},
{"numberWithDecimalMax", null, 6.5},
{"numberWithMinAndMax", 7d, 8d},
{"numberWithMinAndDecimalMax", 9d, 9.5},
{"numberWithDecimalMinAndMax", 10.5, 11d},
{"numberWithDecimalMinAndDecimalMax", 11.5, 12.5}};
{"numberWithMin", new BigDecimal(5d), null},
{"numberWithDecimalMin", new BigDecimal(5.5), null},
{"numberWithMax", null, new BigDecimal(6d)},
{"numberWithDecimalMax", null, new BigDecimal(6.5)},
{"numberWithMinAndMax", new BigDecimal(7d), new BigDecimal(8d)},
{"numberWithMinAndDecimalMax", new BigDecimal(9d), new BigDecimal(9.5)},
{"numberWithDecimalMinAndMax", new BigDecimal(10.5), new BigDecimal(11d)},
{"numberWithDecimalMinAndDecimalMax", new BigDecimal(11.5), new BigDecimal(12.5)}};
}

private Object[][] stringTestData() {
Expand Down