Skip to content

Commit

Permalink
Fixes issue with allowable ranges e.g. infinity
Browse files Browse the repository at this point in the history
fixes #1504
  • Loading branch information
dilipkrish committed Oct 4, 2016
1 parent e29ed05 commit 77bb7bd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private AllowableValues createAllowableValuesFromMinMaxForNumbers(Optional<Min>
} else if (max.isPresent()) {
// use Min value until "infinity" works
LOG.debug("@Max detected: adding AllowableRangeValues to field ");
myvalues = new AllowableRangeValues(Double.toString(Double.MIN_VALUE), Double.toString(max.get().value()));
myvalues = new AllowableRangeValues(Double.toString(-Double.MAX_VALUE), Double.toString(max.get().value()));

}
return myvalues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MinMaxAnnotationPluginSpec extends Specification {
propertyName | expectedMin | expectedMax
"noAnnotation" | null | null
"onlyMin" | "10.0" | Double.MAX_VALUE.toString()
"onlyMax" | Double.MIN_VALUE.toString() | "20.0"
"onlyMax" | (-Double.MAX_VALUE).toString()| "20.0"
"both" | "10.0" | "20.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,17 @@ private Property mapProperty(ModelProperty source) {
AllowableValues allowableValues = source.getAllowableValues();
if (allowableValues instanceof AllowableRangeValues) {
AllowableRangeValues range = (AllowableRangeValues) allowableValues;
((AbstractNumericProperty) property).maximum(Double.valueOf(range.getMax()));
((AbstractNumericProperty) property).minimum(Double.valueOf(range.getMin()));
((AbstractNumericProperty) property).maximum(safeDouble(range.getMax()));
((AbstractNumericProperty) property).minimum(safeDouble(range.getMin()));
}
}

if (property instanceof StringProperty) {
AllowableValues allowableValues = source.getAllowableValues();
if (allowableValues instanceof AllowableRangeValues) {
AllowableRangeValues range = (AllowableRangeValues) allowableValues;
((StringProperty) property).maxLength(Integer.valueOf(range.getMax()));
((StringProperty) property).minLength(Integer.valueOf(range.getMin()));
((StringProperty) property).maxLength(safeInteger(range.getMax()));
((StringProperty) property).minLength(safeInteger(range.getMin()));
}
if(source.getPattern() != null) {
((StringProperty) property).setPattern(source.getPattern());
Expand All @@ -176,6 +176,13 @@ private Property mapProperty(ModelProperty source) {
return property;
}

static Integer safeInteger(String doubleString) {
try {
return Integer.valueOf(doubleString);
} catch (NumberFormatException e) {
return null;
}
}

static Property modelRefToProperty(ModelReference modelRef) {
if (modelRef == null || "void".equalsIgnoreCase(modelRef.getType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import io.swagger.models.properties.ObjectProperty
import io.swagger.models.properties.RefProperty
import io.swagger.models.properties.StringProperty
import org.mapstruct.factory.Mappers
import spock.lang.Unroll
import springfox.documentation.builders.ModelPropertyBuilder
import springfox.documentation.schema.*
import springfox.documentation.schema.mixins.TypesForTestingSupport
Expand All @@ -35,6 +36,7 @@ import static com.google.common.base.Suppliers.*
import static com.google.common.collect.Maps.*
import static springfox.documentation.schema.ResolvedTypes.*
import static springfox.documentation.spi.schema.contexts.ModelContext.*
import static springfox.documentation.swagger2.mappers.ModelMapper.safeInteger

@Mixin([TypesForTestingSupport, AlternateTypesSupport])
class ModelMapperSpec extends SchemaSpecification {
Expand Down Expand Up @@ -252,6 +254,21 @@ class ModelMapperSpec extends SchemaSpecification {
!valueClass.isPresent()
}

@Unroll
def "safe parses #stringValue" () {
when:
def safeParsed = safeInteger(stringValue)
then:
safeParsed == expected
where:
stringValue | expected
"0" | 0
"infinity" | null
"-infinity" | null
"1.0" | null

}



ModelProperty updatedIntObject(ModelProperty modelProperty) {
Expand Down

0 comments on commit 77bb7bd

Please sign in to comment.