FasterXML / jackson-module-jsonSchema

Module for generating JSON Schema (v3) definitions from POJOs
370 stars 136 forks source link

JsonSchema from string definition #106

Closed ben-manes closed 8 years ago

ben-manes commented 8 years ago

I'd like to take a json schema definition from a String and deserialize it into an ObjectSchema instance. This would allow me to walk the properties to determine the referenced schemas and perform server-side logic based on the property's metadata.

I'm currently using json schema for defining custom forms, with components like capturing an e-signature on mobile and displaying on the web. This includes additional metadata, like computed fields (excel-style), currently handled by the UI. I'm starting to need server-side metadata and need to walk the schema to perform various post-processing tasks.

I think ObjectSchema would work nicely for my current needs, but I don't see how to construct it from a String or JsonNode. I'm using jsonschema2pojo for RPCs, but since these schemas are stored in the db using a form builder, I don't have a class to provide for the readme's example.

Any tips for how to obtain a schema definition to walk it?

cowtowncoder commented 8 years ago

In theory it should be just:

ObjectSchema schema = mapper.readValue(source, ObjectSchema.class);

shouldnt it?

ben-manes commented 8 years ago

I honestly didn't expect it to be that intuitive.

That appears to work well for simple schemas, but unfortunately fails because I'm using draft4. Then I run into #9 where required changed from a boolean to an array, causing it to fail. In my case I'd be fine ignoring required. Is the simplest approach to parse to JsonNode and filter it out prior to deserialization?

cowtowncoder commented 8 years ago

@ben-manes if you mean JSON Schema v4, note that this module does not support v4 (can not support both v3 and v4, so it has to remain v3), and it is quite likely it never will. There is one v4 schema generator (I'll have to dig reference up, add link to from README):

https://github.com/mbknor/mbknor-jackson-jsonSchema

but another possibility is that this project could become multi-project and have separate modules for v3 and v4. Anyway, that is unfortunately some ways off.

So I think you are right that unfortunately right now you probably are best off doing manual filtering. With JsonNode, you can then just use ObjectMapper.treeToValue().

Then again, if there was a compatible way of limited support for v4 sources wrt individual changed properties, I would be supportive of adding work-arounds. I just don't know if this is doable regarding major changes to semantic of required.

ben-manes commented 8 years ago

Unfortunately your right that the workarounds may be an endless creep. I used a mixin to ignored required and then found a $ref was not traversed, resulting in the parse error missing property 'type' that is to contain type id.

I think for my particular use-case, I'll eventually use server-side JS for schema evaluation and user-defined functions. So hacking this beyond its intended use is probably a poor approach.

Thanks for the help!