Skip to content

Commit 6b471c3

Browse files
committed
Fix #1045
1 parent 5e924b2 commit 6b471c3

File tree

5 files changed

+135
-52
lines changed

5 files changed

+135
-52
lines changed

src/main/java/com/fasterxml/jackson/databind/jsonFormatVisitors/JsonFormatVisitorWrapper.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,71 +75,71 @@ public static class Base implements JsonFormatVisitorWrapper {
7575
public Base() { }
7676

7777
public Base(SerializerProvider p) {
78-
_provider = p;
78+
_provider = p;
7979
}
8080

8181
@Override
8282
public SerializerProvider getProvider() {
83-
return _provider;
83+
return _provider;
8484
}
8585

8686
@Override
8787
public void setProvider(SerializerProvider p) {
88-
_provider = p;
88+
_provider = p;
8989
}
9090

9191
@Override
9292
public JsonObjectFormatVisitor expectObjectFormat(JavaType type)
93-
throws JsonMappingException {
94-
return null;
93+
throws JsonMappingException {
94+
return null;
9595
}
9696

9797
@Override
9898
public JsonArrayFormatVisitor expectArrayFormat(JavaType type)
9999
throws JsonMappingException {
100-
return null;
100+
return null;
101101
}
102102

103103
@Override
104104
public JsonStringFormatVisitor expectStringFormat(JavaType type)
105-
throws JsonMappingException {
106-
return null;
105+
throws JsonMappingException {
106+
return null;
107107
}
108108

109109
@Override
110110
public JsonNumberFormatVisitor expectNumberFormat(JavaType type)
111-
throws JsonMappingException {
112-
return null;
111+
throws JsonMappingException {
112+
return null;
113113
}
114114

115115
@Override
116116
public JsonIntegerFormatVisitor expectIntegerFormat(JavaType type)
117-
throws JsonMappingException {
118-
return null;
117+
throws JsonMappingException {
118+
return null;
119119
}
120120

121121
@Override
122122
public JsonBooleanFormatVisitor expectBooleanFormat(JavaType type)
123-
throws JsonMappingException {
124-
return null;
123+
throws JsonMappingException {
124+
return null;
125125
}
126126

127127
@Override
128128
public JsonNullFormatVisitor expectNullFormat(JavaType type)
129-
throws JsonMappingException {
130-
return null;
129+
throws JsonMappingException {
130+
return null;
131131
}
132132

133133
@Override
134134
public JsonAnyFormatVisitor expectAnyFormat(JavaType type)
135-
throws JsonMappingException {
136-
return null;
135+
throws JsonMappingException {
136+
return null;
137137
}
138138

139139
@Override
140140
public JsonMapFormatVisitor expectMapFormat(JavaType type)
141-
throws JsonMappingException {
142-
return null;
141+
throws JsonMappingException {
142+
return null;
143143
}
144144
}
145145
}

src/main/java/com/fasterxml/jackson/databind/ser/std/NumberSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
7878
} else {
7979
Class<?> h = handledType();
8080
if (h == BigDecimal.class) {
81-
visitFloatFormat(visitor, typeHint, JsonParser.NumberType.BIG_INTEGER);
81+
visitFloatFormat(visitor, typeHint, JsonParser.NumberType.BIG_DECIMAL);
8282
} else {
8383
// otherwise bit unclear what to call... but let's try:
8484
/*JsonNumberFormatVisitor v2 =*/ visitor.expectNumberFormat(typeHint);

src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,8 @@ protected byte[] utf8Bytes(String str) {
253253
protected static String aposToQuotes(String json) {
254254
return json.replace("'", "\"");
255255
}
256+
257+
protected static String quotesToApos(String json) {
258+
return json.replace("\"", "'");
259+
}
256260
}

src/test/java/com/fasterxml/jackson/databind/jsonschema/NewSchemaTest.java

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.fasterxml.jackson.databind.jsonschema;
22

3+
import java.math.BigDecimal;
4+
import java.math.BigInteger;
35
import java.util.*;
46

7+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
58
import com.fasterxml.jackson.annotation.JsonValue;
9+
import com.fasterxml.jackson.core.JsonParser.NumberType;
610
import com.fasterxml.jackson.databind.*;
7-
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
8-
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor;
9-
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat;
11+
import com.fasterxml.jackson.databind.jsonFormatVisitors.*;
12+
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
1013

1114
/**
1215
* Basic tests to exercise low-level support added for JSON Schema module and
@@ -42,6 +45,12 @@ static class POJO {
4245
public EnumMap<TestEnum,Double> weights;
4346
}
4447

48+
@JsonPropertyOrder({ "dec", "bigInt" })
49+
static class Numbers {
50+
public BigDecimal dec;
51+
public BigInteger bigInt;
52+
}
53+
4554
/*
4655
/**********************************************************
4756
/* Test methods
@@ -125,4 +134,74 @@ public void testJsonValueFormatHandling() throws Exception
125134
// and second, deserialize ok from that as well
126135
assertSame(JsonValueFormat.HOST_NAME, MAPPER.readValue(EXP, JsonValueFormat.class));
127136
}
137+
138+
// [databind#1045], regression wrt BigDecimal
139+
public void testSimpleNumbers() throws Exception
140+
{
141+
final StringBuilder sb = new StringBuilder();
142+
143+
MAPPER.acceptJsonFormatVisitor(Numbers.class,
144+
new JsonFormatVisitorWrapper.Base() {
145+
@Override
146+
public JsonObjectFormatVisitor expectObjectFormat(final JavaType type) {
147+
return new JsonObjectFormatVisitor.Base(getProvider()) {
148+
@Override
149+
public void optionalProperty(BeanProperty prop) throws JsonMappingException {
150+
sb.append("[optProp ").append(prop.getName()).append("(");
151+
JsonSerializer<Object> ser = null;
152+
if (prop instanceof BeanPropertyWriter) {
153+
BeanPropertyWriter bpw = (BeanPropertyWriter) prop;
154+
ser = bpw.getSerializer();
155+
}
156+
final SerializerProvider prov = getProvider();
157+
if (ser == null) {
158+
ser = prov.findValueSerializer(prop.getType(), prop);
159+
}
160+
ser.acceptJsonFormatVisitor(new JsonFormatVisitorWrapper.Base() {
161+
@Override
162+
public JsonNumberFormatVisitor expectNumberFormat(
163+
JavaType type) throws JsonMappingException {
164+
return new JsonNumberFormatVisitor() {
165+
@Override
166+
public void format(JsonValueFormat format) {
167+
sb.append("[numberFormat=").append(format).append("]");
168+
}
169+
170+
@Override
171+
public void enumTypes(Set<String> enums) { }
172+
173+
@Override
174+
public void numberType(NumberType numberType) {
175+
sb.append("[numberType=").append(numberType).append("]");
176+
}
177+
};
178+
}
179+
180+
@Override
181+
public JsonIntegerFormatVisitor expectIntegerFormat(JavaType type) throws JsonMappingException {
182+
return new JsonIntegerFormatVisitor() {
183+
@Override
184+
public void format(JsonValueFormat format) {
185+
sb.append("[integerFormat=").append(format).append("]");
186+
}
187+
188+
@Override
189+
public void enumTypes(Set<String> enums) { }
190+
191+
@Override
192+
public void numberType(NumberType numberType) {
193+
sb.append("[numberType=").append(numberType).append("]");
194+
}
195+
};
196+
}
197+
}, prop.getType());
198+
199+
sb.append(")]");
200+
}
201+
};
202+
}
203+
});
204+
assertEquals("[optProp dec([numberType=BIG_DECIMAL])][optProp bigInt([numberType=BIG_INTEGER])]",
205+
sb.toString());
206+
}
128207
}

src/test/java/com/fasterxml/jackson/databind/jsonschema/TestGenerateJsonSchema.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.fasterxml.jackson.databind.jsonschema;
22

3+
import java.math.BigDecimal;
4+
import java.math.BigInteger;
35
import java.util.Collection;
46
import java.util.Map;
57

68
import com.fasterxml.jackson.annotation.*;
7-
89
import com.fasterxml.jackson.databind.JsonNode;
910
import com.fasterxml.jackson.databind.ObjectMapper;
1011
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -105,6 +106,12 @@ static class UnwrappingRoot
105106
static class Name {
106107
public String first, last;
107108
}
109+
110+
@JsonPropertyOrder({ "dec", "bigInt" })
111+
static class Numbers {
112+
public BigDecimal dec;
113+
public BigInteger bigInt;
114+
}
108115

109116
/*
110117
/**********************************************************
@@ -117,8 +124,7 @@ static class Name {
117124
/**
118125
* tests generating json-schema stuff.
119126
*/
120-
public void testGeneratingJsonSchema()
121-
throws Exception
127+
public void testOldSchemaGeneration() throws Exception
122128
{
123129
JsonSchema jsonSchema = MAPPER.generateJsonSchema(SimpleBean.class);
124130

@@ -190,34 +196,18 @@ public void testGeneratingJsonSchemaWithFilters() throws Exception {
190196
* Additional unit test for verifying that schema object itself
191197
* can be properly serialized
192198
*/
193-
public void testSchemaSerialization()
194-
throws Exception
199+
public void testSchemaSerialization() throws Exception
195200
{
196201
JsonSchema jsonSchema = MAPPER.generateJsonSchema(SimpleBean.class);
197-
Map<String,Object> result = writeAndMap(MAPPER, jsonSchema);
198-
assertNotNull(result);
199-
// no need to check out full structure, just basics...
200-
assertEquals("object", result.get("type"));
201-
// only add 'required' if it is true...
202-
assertNull(result.get("required"));
203-
assertNotNull(result.get("properties"));
204-
}
205-
206-
public void testInvalidCall()
207-
throws Exception
208-
{
209-
// not ok to pass null
210-
try {
211-
MAPPER.generateJsonSchema(null);
212-
fail("Should have failed");
213-
} catch (IllegalArgumentException iae) {
214-
verifyException(iae, "class must be provided");
215-
}
202+
Map<String,Object> result = writeAndMap(MAPPER, jsonSchema);
203+
assertNotNull(result);
204+
// no need to check out full structure, just basics...
205+
assertEquals("object", result.get("type"));
206+
// only add 'required' if it is true...
207+
assertNull(result.get("required"));
208+
assertNotNull(result.get("properties"));
216209
}
217210

218-
/**
219-
* Test for [JACKSON-454]
220-
*/
221211
public void testThatObjectsHaveNoItems() throws Exception
222212
{
223213
JsonSchema jsonSchema = MAPPER.generateJsonSchema(TrivialBean.class);
@@ -236,7 +226,7 @@ public void testSchemaId() throws Exception
236226
json);
237227
}
238228

239-
// [Issue#271]
229+
// [databind#271]
240230
public void testUnwrapping() throws Exception
241231
{
242232
JsonSchema jsonSchema = MAPPER.generateJsonSchema(UnwrappingRoot.class);
@@ -247,4 +237,14 @@ public void testUnwrapping() throws Exception
247237
assertEquals(EXP, json);
248238
}
249239

240+
//
241+
public void testNumberTypes() throws Exception
242+
{
243+
JsonSchema jsonSchema = MAPPER.generateJsonSchema(Numbers.class);
244+
String json = quotesToApos(jsonSchema.toString());
245+
String EXP = "{'type':'object',"
246+
+"'properties':{'dec':{'type':'number'},"
247+
+"'bigInt':{'type':'integer'}}}";
248+
assertEquals(EXP, json);
249+
}
250250
}

0 commit comments

Comments
 (0)