jackson-module-jsonSchema icon indicating copy to clipboard operation
jackson-module-jsonSchema copied to clipboard

Support polymorphism on schema generation

Open cowtowncoder opened this issue 12 years ago • 14 comments

(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 { }

cowtowncoder avatar Dec 04 '12 05:12 cowtowncoder

This would probably be an appropriate value for the "title" property in a value type schema

bytesandwich avatar Dec 04 '12 16:12 bytesandwich

Hmmh. JSON Schema's support for OO features like inheritance are pretty lame. Anyone know what would be the best thing to populate?

cowtowncoder avatar Sep 20 '13 04:09 cowtowncoder

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" ] },
                . . . 
            }        
        }
    }
}

greyfairer avatar Sep 03 '14 12:09 greyfairer

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.

cowtowncoder avatar Sep 03 '14 19:09 cowtowncoder

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.

marcuslange avatar Jan 30 '16 01:01 marcuslange

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.

DanielSchaffer avatar Jan 30 '16 01:01 DanielSchaffer

this would be pretty useful! +1

ejoncas avatar Mar 01 '16 05:03 ejoncas

+1

cmungall avatar Mar 07 '16 01:03 cmungall

+1

javaguruen avatar May 09 '16 12:05 javaguruen

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 avatar Jun 16 '16 19:06 mbknor

@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 avatar Jun 16 '16 19:06 cowtowncoder

@cowtowncoder FYI: Version 1.0.0 of https://github.com/mbknor/mbknor-jackson-jsonSchema is now released to Maven Central.

mbknor avatar Jun 21 '16 09:06 mbknor

Thank you!

cowtowncoder avatar Jun 22 '16 03:06 cowtowncoder

@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.

bitdivine avatar Oct 05 '18 18:10 bitdivine