diff --git a/src/test/java/com/fasterxml/jackson/module/jsonSchema/failing/TestUnwrapping.java b/src/test/java/com/fasterxml/jackson/module/jsonSchema/failing/TestUnwrapping.java index 356ce573..c3032f16 100644 --- a/src/test/java/com/fasterxml/jackson/module/jsonSchema/failing/TestUnwrapping.java +++ b/src/test/java/com/fasterxml/jackson/module/jsonSchema/failing/TestUnwrapping.java @@ -1,24 +1,40 @@ package com.fasterxml.jackson.module.jsonSchema.failing; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.module.jsonSchema.JsonSchema; import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator; import com.fasterxml.jackson.module.jsonSchema.SchemaTestBase; public class TestUnwrapping extends SchemaTestBase { - static class UnwrappingRoot - { + static class UnwrappingRoot { + @JsonUnwrapped(prefix="ignored.") public int age; - @JsonUnwrapped - public Name name; + private Name _name; + + @JsonUnwrapped(prefix="name.") + public Name getName() { + return _name; + } } static class Name { - @JsonUnwrapped(prefix="name.") + @JsonUnwrapped(prefix="ignoreNonObject.") // is ignored in serialization public String first, last; + @JsonUnwrapped(prefix="deeper.") + public More more; + } + + static class More { + public Double deepest; } /* @@ -26,24 +42,49 @@ static class Name { /* Unit tests, success /********************************************************** */ - + private final ObjectMapper MAPPER = objectMapper(); + private final ObjectWriter WRITER = MAPPER.writerWithDefaultPrettyPrinter(); public void testUnwrapping() throws Exception { JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER); JsonSchema schema = generator.generateSchema(UnwrappingRoot.class); + //System.out.println(WRITER.writeValueAsString(schema)); - String json = MAPPER.writeValueAsString(schema).replace('"', '\''); + Map properties = schema.asObjectSchema().getProperties(); + assertEquals(4, properties.keySet().size()); + assertNotNull(properties.get("age").asIntegerSchema()); + assertNotNull(properties.get("name.first").asStringSchema()); + assertNotNull(properties.get("name.last").asStringSchema()); + assertNotNull(properties.get("name.deeper.deepest").asNumberSchema()); -//System.err.println("JSON -> "+MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(schema)); - String EXP = "{'type':'object','properties':{" - +"'name.last':{'type':'string'},'name.first':{'type':'string'}," - +"'age':{'type':'number','type':'integer'}}}"; -System.err.println("EXP: "+EXP); -System.err.println("ACT: "+json); - - assertEquals(EXP, json); + UnwrappingRoot example = makeExample(); + String json = WRITER.writeValueAsString(example); + //System.out.println(json); //helpful debugging output + Set expectedKeySet = extractKeysFromJson(json); + //System.out.println(expectedKeySet); + assertEquals(expectedKeySet, properties.keySet()); } + + private HashSet extractKeysFromJson(String json) { + String[] keys = json + .replaceAll("[{}]","") //remove begin/end of json object + .replaceAll("\"","") //remove quotes + .replaceAll(":[^,]*,?", "") //get rid of value part + .trim().split("\\s+"); //split by whitespace + return new HashSet(Arrays.asList(keys)); + } + + private UnwrappingRoot makeExample() { + UnwrappingRoot value = new UnwrappingRoot(); + value.age = 33; + value._name = new Name(); + value._name.first = "firstName"; + value._name.last = "lastName"; + value._name.more = new More(); + value._name.more.deepest = 3.14; + return value; + } }