java-json-tools / json-schema-validator

A JSON Schema validation implementation in pure Java, which aims for correctness and performance, in that order
http://json-schema-validator.herokuapp.com/
Other
1.63k stars 399 forks source link

Using both JAXB and Jackson annotations to serialize/deserialize data #268

Closed sushmayellampally closed 6 years ago

sushmayellampally commented 6 years ago

My POJO element is:

@XmlElement(name = "TrackDt", required = true)
@XmlSchemaType(name = "date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "GMT")
protected XMLGregorianCalendar trackDt;

During generating Json schema, I am using both JAXB and Jackson annotaions by adding introspectors like below:

JacksonAnnotationIntrospector primary = new JacksonAnnotationIntrospector();
JaxbAnnotationIntrospector secondary = new 
JaxbAnnotationIntrospector(typeFactory);
mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(primary, secondary));

The generated Json schema looks like:

..
 "TrackDt": {
        "type": "string",
        "format": "date-time"
    }
..
"required": ["TrackDt"]

A Json request example: { .... "TrackDt": "2017-06-13", .... } While validating this JSON instance against the schema, the date is validated and the response contains the dates according to the pattern specified in @JsonFormat annotation.

Now , I want to add JAXB annotation introspector and use JAXB annotations also. But the validator throws following error:

Exception while reading input message; nested exception is 
com.github.fge.jsonschema.core.exceptions.ProcessingException: fatal: string 
"2017-06-13" is invalid against requested date format(s) [yyyy-MM- 
dd'T'HH:mm:ssZ, yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z] level: "fatal" schema: 
{"loadingURI":"#","pointer":"../properties/TrackDt"} 
instance: {"pointer":"/TrackDt"} domain: "validation" keyword: "format" 
attribute: "date-time" value: "2017-06-13" expected: ["yyyy-MM- 
dd'T'HH:mm:ssZ","yyyy-MM-dd'T'HH:mm:ss.[0-9]{1,12}Z"]
sushmayellampally commented 6 years ago

Why would the validation fails if I add Jaxb annotation introspector?

sushmayellampally commented 6 years ago

Closing this issue. This error is related to https://github.com/java-json-tools/json-schema-validator/issues/103. Adding the date and time attributes fixed the error.