Skip to content

Commit 9e6d736

Browse files
committed
Merge pull request #61 from ciderale/jsonschema-support-jsonunwrapped
support json schema generation for @JsonUnwrapped (#20)
2 parents 10baae5 + 6d47fdc commit 9e6d736

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed
Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,90 @@
11
package com.fasterxml.jackson.module.jsonSchema.failing;
22

3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
7+
38
import com.fasterxml.jackson.annotation.JsonUnwrapped;
49
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.ObjectWriter;
511
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
612
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
713
import com.fasterxml.jackson.module.jsonSchema.SchemaTestBase;
814

915
public class TestUnwrapping extends SchemaTestBase
1016
{
11-
static class UnwrappingRoot
12-
{
17+
static class UnwrappingRoot {
18+
@JsonUnwrapped(prefix="ignored.")
1319
public int age;
1420

15-
@JsonUnwrapped
16-
public Name name;
21+
private Name _name;
22+
23+
@JsonUnwrapped(prefix="name.")
24+
public Name getName() {
25+
return _name;
26+
}
1727
}
1828

1929
static class Name {
20-
@JsonUnwrapped(prefix="name.")
30+
@JsonUnwrapped(prefix="ignoreNonObject.") // is ignored in serialization
2131
public String first, last;
32+
@JsonUnwrapped(prefix="deeper.")
33+
public More more;
34+
}
35+
36+
static class More {
37+
public Double deepest;
2238
}
2339

2440
/*
2541
/**********************************************************
2642
/* Unit tests, success
2743
/**********************************************************
2844
*/
29-
45+
3046
private final ObjectMapper MAPPER = objectMapper();
47+
private final ObjectWriter WRITER = MAPPER.writerWithDefaultPrettyPrinter();
3148

3249
public void testUnwrapping() throws Exception
3350
{
3451
JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER);
3552
JsonSchema schema = generator.generateSchema(UnwrappingRoot.class);
53+
//System.out.println(WRITER.writeValueAsString(schema));
3654

37-
String json = MAPPER.writeValueAsString(schema).replace('"', '\'');
55+
Map<String, JsonSchema> properties = schema.asObjectSchema().getProperties();
56+
assertEquals(4, properties.keySet().size());
57+
assertNotNull(properties.get("age").asIntegerSchema());
58+
assertNotNull(properties.get("name.first").asStringSchema());
59+
assertNotNull(properties.get("name.last").asStringSchema());
60+
assertNotNull(properties.get("name.deeper.deepest").asNumberSchema());
3861

39-
//System.err.println("JSON -> "+MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
40-
String EXP = "{'type':'object','properties':{"
41-
+"'name.last':{'type':'string'},'name.first':{'type':'string'},"
42-
+"'age':{'type':'number','type':'integer'}}}";
4362

44-
System.err.println("EXP: "+EXP);
45-
System.err.println("ACT: "+json);
46-
47-
assertEquals(EXP, json);
63+
UnwrappingRoot example = makeExample();
64+
String json = WRITER.writeValueAsString(example);
65+
//System.out.println(json); //helpful debugging output
66+
Set<String> expectedKeySet = extractKeysFromJson(json);
67+
//System.out.println(expectedKeySet);
68+
assertEquals(expectedKeySet, properties.keySet());
4869
}
70+
71+
private HashSet<String> extractKeysFromJson(String json) {
72+
String[] keys = json
73+
.replaceAll("[{}]","") //remove begin/end of json object
74+
.replaceAll("\"","") //remove quotes
75+
.replaceAll(":[^,]*,?", "") //get rid of value part
76+
.trim().split("\\s+"); //split by whitespace
77+
return new HashSet<String>(Arrays.asList(keys));
78+
}
79+
80+
private UnwrappingRoot makeExample() {
81+
UnwrappingRoot value = new UnwrappingRoot();
82+
value.age = 33;
83+
value._name = new Name();
84+
value._name.first = "firstName";
85+
value._name.last = "lastName";
86+
value._name.more = new More();
87+
value._name.more.deepest = 3.14;
88+
return value;
89+
}
4990
}

0 commit comments

Comments
 (0)