jackson-module-jsonSchema
jackson-module-jsonSchema copied to clipboard
Support polymorphism on schema generation
(moved from: https://github.com/FasterXML/jackson-databind/issues/41 )
t would be nice to support polymorphism on schema generation. Currently, the generated schema doesn't provides information on subtypes with the above case .
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@Type(value = subtypeA.class, name = "A"),
@Type(value = subtypeB.class, name = "B"),
@Type(value = subtypeC.class, name = "C")
})
public abstract class ParentType { }
This would probably be an appropriate value for the "title" property in a value type schema
Hmmh. JSON Schema's support for OO features like inheritance are pretty lame. Anyone know what would be the best thing to populate?
You could use the approach described in http://stackoverflow.com/questions/18375506/how-to-use-dependencies-in-json-schema-draft-04:
{
"type": "object",
"required": [ "results" ],
"properties": {
"results": {
"type": "array",
"oneOf": [
{ "$ref": "#/definitions/person" },
{ "$ref": "#/definitions/company" }
]
}
},
"definitions": {
"person": {
"properties": {
"type": { "enum": [ "person" ] },
"name": {"type": "string" },
"dateOfBirth": {"type":"string"}
},
"required": [ "type", "name", "dateOfBirth" ],
"additionalProperties": false
},
"company": {
"properties": {
"type": { "enum": [ "company" ] },
. . .
}
}
}
}
While theoretically possible, this might not be practical since it requires ability to navigate the logical tree structure. Although maybe just producing this structure (as there's no need to read in JSON Schema) would not need that; there's some work to keep track of references, but if structure is kept flat (no nested definitions) it might not be too bad.
Is anyone working on this? I wouldn't mind taking a shot at it with some guidance. It's something I need in several projects i'm working on.
I've done some work on my own around adding better support for oneOf/anyOf, but I've been temporarily diverted to another project. I'll see if I can't extract the relevant files sometime next week and put them in a fork or something.
this would be pretty useful! +1
+1
+1
I'm currently writing a new jsonSchema-generator using Jackson (https://github.com/mbknor/mbknor-jackson-jsonSchema). It writes v4 schemas and supports Polymorphism used like this:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Child1.class, name = "child1"),
@JsonSubTypes.Type(value = Child2.class, name = "child2") })
public abstract class Parent {
public String parentString;
}
It is not done yet, but works for us. Maybe someone would check it out and contribute.
@mbknor Excellent. If so, we could eventually deprecate this module since there is no active maintainer here. Also, schema generation itself is not really something core to Jackson and works best as separate add-on anyway. So I'd be happy to see the support move to other projects that we can link to.
This would provide #9
@cowtowncoder FYI: Version 1.0.0 of https://github.com/mbknor/mbknor-jackson-jsonSchema is now released to Maven Central.
Thank you!
@mbknor How would you generate the JSON schema for subtypes if some subtypes are simple strings, others objects or arrays?
trait Poly
case class SimpleSimon() extends Poly // serialises as "simon"
case class Complex(i:Int, j:Int) extends Poly // serialises as {"i":5, "j":9}
I guess that for the JSON schema, JsonTypeInfo is not essential. Just giving SubTypes should work:
@JsonSubTypes(Array(
Type(classOf[SimpleSimon]),
Type(classOf[Complex])
))
trait Poly
case class SimpleSimon() extends Poly // serialises as "simon"
case class Complex(i:Int, j:Int) extends Poly // serialises as {"i":5, "j":9}
But it doesn't seem to. Is there some other way through? I would definitely offer beer money for the above.