reinert / JJSchema

A generator from Java Types to JSON-Schema using Jackson.
Other
117 stars 56 forks source link

Instant expected as String in 1.2 #64

Closed devabdicant closed 6 years ago

devabdicant commented 7 years ago

Hi! I'm checking the new 1.2. I have an object with an Instant param and when I try to validate the result is expected["string"]: This is the param I create the schema from @Attributes(required = true, description = "expected time for activation") private Instant activation;

This is the result I get: com.github.fge.jsonschema.core.exceptions.ProcessingException: fatal: instance type (object) does not match any allowed primitive type (allowed: ["string"]) level: "fatal" schema: {"loadingURI":"#","pointer":"/properties/activation"} instance: {"pointer":"/activation"} domain: "validation" keyword: "type" found: "object" expected: ["string"]

The release says this type is specifically supported, so I guess I am doing something wrong. Could you upgrade the example in readme.txt to include a Instant parameter and see what I'm doing wrong?

Thanks!!

reinert commented 7 years ago

Yeah. It is supported. Instant is converted to string. I don't know why it's converting to object in your case. Could you post your whole class so I can reproduce the error?

devabdicant commented 7 years ago

Sure! I am creating a WebService. The client parses this class into json using jackson public class InstantTest {

private Instant aDate;

public InstantTest(){}

public Instant getaDate() {
    return aDate;
}
public void setaDate(Instant aDate) {
    this.aDate = aDate;
}

}

the web service receives this json: {"aDate":{"epochSecond":1488886747,"nano":997000000}}

the WS has this class import java.time.Instant;

import com.github.reinert.jjschema.Attributes;

public class InstantTest {

@Attributes(required = false, description = "Instant testing")
private Instant aDate;

public InstantTest(){}

public Instant getaDate() {
    return aDate;
}
public void setaDate(Instant aDate) {
    this.aDate = aDate;
}

}

and using JJSCHEMA 1.2 generates this: instantSchema: {"type":"object","properties":{"aDate":{"type":"string","description":"Instant testing"}},"$schema":"http ://json-schema.org/draft-04/schema#"}

with this method //TEST public static String loadInstantSchema() { JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory(); schemaFactory.setAutoPutDollarSchema(true); return schemaFactory.createSchema(InstantTest.class).toString(); }

the result of the validation is what I wrote before, type error, because the schema says it is a string

hope it helps!!

thanks!

reinert commented 7 years ago

The problem there is that Jackson is converting the aDate field to an object with two fields epochSecond and nano. Json Schema has no "date" type, so we must fallback to one type of the schema, may it be a 'string', a 'number', or even an 'object'. Your Jackson lib is set to convert java Instant fields to an object as stated before. You can configure Jackson to convert Instant as a String in the date-time format. JJSchema converts "date" related fields to string because is the recommended approach of Json Schema spec, followed by a 'format = date-time' attribute.