Skip to content

support json schema generation for @JsonUnwrapped (#20) #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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,49 +1,90 @@
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;
}

/*
/**********************************************************
/* 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<String, JsonSchema> 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<String> expectedKeySet = extractKeysFromJson(json);
//System.out.println(expectedKeySet);
assertEquals(expectedKeySet, properties.keySet());
}

private HashSet<String> 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<String>(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;
}
}