|
1 | 1 | package com.fasterxml.jackson.module.jsonSchema.failing;
|
2 | 2 |
|
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Set; |
| 7 | + |
3 | 8 | import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
4 | 9 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 10 | +import com.fasterxml.jackson.databind.ObjectWriter; |
5 | 11 | import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
|
6 | 12 | import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
|
7 | 13 | import com.fasterxml.jackson.module.jsonSchema.SchemaTestBase;
|
8 | 14 |
|
9 | 15 | public class TestUnwrapping extends SchemaTestBase
|
10 | 16 | {
|
11 |
| - static class UnwrappingRoot |
12 |
| - { |
| 17 | + static class UnwrappingRoot { |
| 18 | + @JsonUnwrapped(prefix="ignored.") |
13 | 19 | public int age;
|
14 | 20 |
|
15 |
| - @JsonUnwrapped |
16 |
| - public Name name; |
| 21 | + private Name _name; |
| 22 | + |
| 23 | + @JsonUnwrapped(prefix="name.") |
| 24 | + public Name getName() { |
| 25 | + return _name; |
| 26 | + } |
17 | 27 | }
|
18 | 28 |
|
19 | 29 | static class Name {
|
20 |
| - @JsonUnwrapped(prefix="name.") |
| 30 | + @JsonUnwrapped(prefix="ignoreNonObject.") // is ignored in serialization |
21 | 31 | public String first, last;
|
| 32 | + @JsonUnwrapped(prefix="deeper.") |
| 33 | + public More more; |
| 34 | + } |
| 35 | + |
| 36 | + static class More { |
| 37 | + public Double deepest; |
22 | 38 | }
|
23 | 39 |
|
24 | 40 | /*
|
25 | 41 | /**********************************************************
|
26 | 42 | /* Unit tests, success
|
27 | 43 | /**********************************************************
|
28 | 44 | */
|
29 |
| - |
| 45 | + |
30 | 46 | private final ObjectMapper MAPPER = objectMapper();
|
| 47 | + private final ObjectWriter WRITER = MAPPER.writerWithDefaultPrettyPrinter(); |
31 | 48 |
|
32 | 49 | public void testUnwrapping() throws Exception
|
33 | 50 | {
|
34 | 51 | JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER);
|
35 | 52 | JsonSchema schema = generator.generateSchema(UnwrappingRoot.class);
|
| 53 | + //System.out.println(WRITER.writeValueAsString(schema)); |
36 | 54 |
|
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()); |
38 | 61 |
|
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'}}}"; |
43 | 62 |
|
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()); |
48 | 69 | }
|
| 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 | + } |
49 | 90 | }
|
0 commit comments