简体   繁体   中英

JSON Schema validation - Java POJO vs separate .json files

I am working on a project that has been using Java POJOs from which JSON Schemas are generated and validated against given REST endpoint response.

This is working fine most of the time but has some drawbacks, for instance when a new key-value pair is introduced in a given endpoint, I have no means to see in Jenkins build's automated test that something was added in a response (restassured' matchesJsonSchema method does not account for extra items that were not defined in POJO class beforehand - meaning if there is no "x" field defined in class and it's retrieved in the endpoint response, test will still pass).

There is a "maxProperties" key available in JSON standard against which the schema can be validated, however this would mean that I'd need to add ~40 .json schema files to my repository and define the current no of returned key-value pairs.

In general, I wonder what's the better approach when it comes to maintenance - keeping the schemas separate in .json files or create them programmatically in Java POJO classes for validation? I think that ideally it would be better to validate the responses against the swagger documentation that's available online for the API, although since I've never had a chance to touch upon the topic, I wonder what are the potential drawbacks.

在POJO类Use JSR 303 Validation中,这使您的应用程序易于使用@size(min =“”,max =“”),并且您还必须在rest控制器类中使用@valid注释

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM