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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.swagger.v3.oas.models.media;

import java.text.NumberFormat;
import java.util.Objects;

/**
Expand Down Expand Up @@ -34,13 +33,14 @@ public IntegerSchema _default(Number _default) {
protected Number cast(Object value) {
if (value != null) {
try {
Number casted = NumberFormat.getInstance().parse(value.toString());
if (Integer.MIN_VALUE <= casted.longValue() && casted.longValue() <= Integer.MAX_VALUE) {
return Integer.parseInt(value.toString());
String stringValue = value.toString();
long casted = Long.parseLong(stringValue);
if (withinIntegerBounds(casted)) {
return Integer.parseInt(stringValue);
} else {
return Long.parseLong(value.toString());
return casted;
}
} catch (Exception e) {
} catch (Exception ignored) {
}
}
return null;
Expand Down Expand Up @@ -75,4 +75,8 @@ public String toString() {
sb.append("}");
return sb.toString();
}

private boolean withinIntegerBounds(long value) {
return Integer.MIN_VALUE <= value && value <= Integer.MAX_VALUE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.swagger.v3.oas.models.media;

import org.testng.annotations.Test;

import java.util.Locale;

import static org.testng.Assert.*;

public class IntegerSchemaTest {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is of interest I could add the tests first in a PR to showcase the current behavior, and then do a PR afterwards that changes the implementation (but also showcases that the behavior is the same).


private static final IntegerSchema INTEGER_SCHEMA = new IntegerSchema();
private static final Locale SWEDISH_LOCALE = new Locale("se");
private static final String INTEGER_STRING = "123";
private static final String LONG_STRING = "1111111111111111111";
private static final String NEGATIVE_INTEGER = "-123";
private static final String INTEGER_WITH_SPACE_THOUSAND_SEPARATOR = "2 000";
private static final String INTEGER_WITH_DECIMAL_THOUSAND_SEPARATOR = "2.000";
private static final String INTEGER_WITH_COMMA_THOUSAND_SEPARATOR = "2,000";
private static final Integer NUMBER = 123;
private static final Integer NEGATIVE_NUMBER = -123;
private static final Long LONG = 1111111111111111111L;

@Test
public void testCastInteger() {
assertEquals(INTEGER_SCHEMA.cast(INTEGER_STRING), NUMBER);
}

@Test
public void testCastLong() {
assertEquals(INTEGER_SCHEMA.cast(LONG_STRING), LONG);
}

@Test
public void testCastNegativeInteger() {
assertEquals(INTEGER_SCHEMA.cast(NEGATIVE_INTEGER), NEGATIVE_NUMBER);
}

@Test
public void testCastIntegerWithSpaceThousandSeparatorFailsWithNull() {
assertNull(INTEGER_SCHEMA.cast(INTEGER_WITH_SPACE_THOUSAND_SEPARATOR));
}

@Test
public void testCastIntegerWithDecimalThousandSeparatorFailsWithNull() {
assertNull(INTEGER_SCHEMA.cast(INTEGER_WITH_DECIMAL_THOUSAND_SEPARATOR));
}

@Test
public void testCastIntegerWithCommaThousandSeparatorFailsWithNull() {
assertNull(INTEGER_SCHEMA.cast(INTEGER_WITH_COMMA_THOUSAND_SEPARATOR));
}

@Test
public void testCastIntegerWithSwedishLocale() {
Locale defaultLocale = Locale.getDefault();
try {
Locale.setDefault(SWEDISH_LOCALE);
assertEquals(new IntegerSchema().cast(INTEGER_STRING), NUMBER);
} finally {
Locale.setDefault(defaultLocale);
}
}

@Test
public void testCastLongWithSwedishLocale() {
Locale defaultLocale = Locale.getDefault();
try {
Locale.setDefault(SWEDISH_LOCALE);
assertEquals(new IntegerSchema().cast(LONG_STRING), LONG);
} finally {
Locale.setDefault(defaultLocale);
}
}

@Test
public void testCastNegativeIntegerWithSwedishLocale() {
Locale defaultLocale = Locale.getDefault();
try {
Locale.setDefault(SWEDISH_LOCALE);
assertEquals(new IntegerSchema().cast(NEGATIVE_INTEGER), NEGATIVE_NUMBER);
} finally {
Locale.setDefault(defaultLocale);
}
}

}