jackson jsonSchema: How to set type object for property (JsonRawValue)
<p>I use <a href="https://github.com/FasterXML/jackson-databind" rel="nofollow">jackson</a> and <a href="https://github.com/FasterXML/jackson-module-jsonSchema" rel="nofollow">jackson-module-jsonSchema</a> to deserialize json and generate json schema (on fly) to validate json by <a href="https://github.com/daveclayton/json-schema-validator" rel="nofollow">json-schema-validator</a>.</p>

<p>I have a class with field "payload". This field should contain raw json, because there can be any properties, which client needs. For example:</p>

<pre><code>{
    "author": "test",
    "payload": {
        "title": "Test title"
    }
}   
</code></pre>

<p>I expect that field payload will have type "object" in schema, but it's type "string". How should I tell to scheme generator to make it object???</p>

<p>Class:</p>

<pre><code>import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.databind.JsonNode;

public class Book {
    private String author;
    private Object payload;

    @JsonRawValue
    public Object getPayload() {
        return payload;
    }

    public void setPayload(JsonNode node) {
        this.payload = node;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
            "author='" + author + '\'' +
            ", payload=" + payload +
            '}';
    }
}
</code></pre>

<p>My test:</p>

<pre><code>@Test
public void generateSchemaBook() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new SimpleModule());
    JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
    final JsonSchema jsonSchema = schemaGen.generateSchema(Book.class);
    jsonSchema.set$schema("http://json-schema.org/draft-03/schema#");
    final String schema = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
    /*
        {
          "type" : "object",
          "id" : "urn:jsonschema:ru:infon:mas:protocol:Book",
          "$schema" : "http://json-schema.org/draft-03/schema#",
          "properties" : {
            "author" : {
              "type" : "string",
              "required" : true
            },
            "payload" : {
              "type" : "string",
              "required" : true
            }
          }
        }
     */
    System.out.println(schema);
    String testJson = "{\"author\":\"test\",\"payload\":{\"title\":\"Test title\"}}";
    Book book = mapper.readValue(testJson, Book.class);
    System.out.println(book);
    assertEquals("{\"title\":\"Test title\"}", book.getPayload().toString());

    ProcessingReport validate = JsonSchemaFactory.byDefault().getJsonSchema(JsonLoader.fromString(schema)).validate(JsonLoader.fromString(testJson));
    assertTrue(validate.isSuccess());
}
</code></pre>