-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for custom Annotator classes #86
Comments
From [email protected] on January 28, 2013 20:33:41 Yes the tool ignores the "links" schema property right now. At the moment I'm finding it difficult to see how jsonschema2pojo could support this in a general way. I think the best option for you right now might be to use jsonschema2pojo to generate the types as step 1, but make further manual changes to the sources produced (i.e. and keep the sources under normal version control from then on). You can create a property of type "string" with format "uri" to create a URI type property. You'll have to annotate the properties yourself. Another option would be to create your own clone and implement this as a custom change. If you have any neat ideas about how this might be included in jsonschema2pojo feel free to propose a solution. I wonder if the only general way to do this would be to add some way of providing your own custom Annotator, which is invokes whilst sources are generated and has the opportunity to add custom annotations. Would this work for you? |
From [email protected] on January 28, 2013 20:50:10 Beside forking the complete project, I was hoping for any listener/extension class I could extend to instruct jsonschema2pojo to generate beans differently. Modifying beans manually is error prone and expose risk of getting schemas and beans out of sync. Big deal in an enterprise environment... Exposing properties as URIs is different to me than exposing relationships. But I will try this and see if this brings me anywhere. Regards, |
From [email protected] on February 01, 2013 22:52:52 My team (@oraclecorp) is actually interrested at participating to generate JAXB compliant annotation out of the jsonschema in the generated POJOs. Let me know and contact me directly if you are. |
From [email protected] on February 01, 2013 23:11:35 If I understand you correctly, you'll be implementing this existing feature request: http://code.google.com/p/jsonschema2pojo/issues/detail?id=74 If you need any help navigating the code or advice on implementation then please feel free to send me some questions. Cheers! |
From [email protected] on February 05, 2013 17:42:57
I like this idea very much: allowing custom Annotater be registered through configuration and then invoking it during generating process would keep jsonschema decoupled and yet extensible for any technology specific use cases. E.g. supporting JAXB type of annotation, or @rel jax-rs type of annotation for each links. While generating classes, the configuration should allow to specify to pass the current file (java class being generated) or another file. Why another file? So it could actually generate an API class additional to the DTO class. Now that would be neat :))) >> having an API solely described by the json-schema, including (@get or @post) operation that returns DTOs (as defined per links schema semantic), so changing it ripples down to update API and DTOs in the code... no out-of-sync between the Java API and the json-schema definition... that would be great! Do you have a concrete idea how to allow customized annotation while parsing the schema (including properties AND link definitions)? Could we review together your ideas? |
From [email protected] on February 05, 2013 17:53:52 so becomes @path("clients/{id}")
} Neat... really neat... |
From [email protected] on February 05, 2013 20:35:36 Because of the way CodeModel[1] works, it would be trivial for a custom Annotator to add new types (as well as, of course, modify the existing ones). I expect the difficulty would come in trying to pass enough contextual information to the Annotator for it to sanely build the additional set of classes (I felt these pains when implementing the tool I've described above). No harm in trying though. |
From [email protected] on February 05, 2013 20:38:39 I'll tentatively peg this for 0.3.6. I reserve the right to change this though if something else becomes more urgent :) |
From [email protected] on February 20, 2013 15:58:51 @JacksonXmlElementWrapper allows specifying XML element to use for wrapping List and Map properties Should I enter a separate enhancement so this becomes part of the default build? (jsonschema2pojo already has jackson dependencies, it should be logical to support such annotations as well, e.g. by allowing some specific jsonschema property, similar to javaType you already have). Thanks! |
From [email protected] on February 22, 2013 20:44:46 This is quite specific to Jackson's JSON annotations - I think we may need to generalize this interface further so that it has the opportunity to operate on any generated construct. Could you try experimenting with your own implementation of the Annotator interface to see if you have enough power to apply the Jackson XML annotations where you need them? And when the Annotator is invoked, does it currently receive enough information to decide the correct XML annotation? |
From [email protected] on February 22, 2013 23:10:27 I think below 2 methods would be key:
As you will see, I added 2 new special properties in the schema The "id" which semantically represents the namespace of the JSON schema For collections (array) there is special handling:
For all other properties, the logic is as follows:
You would likely enhance the AnnotatorStyle with an additional JACKSON2+XML The advantage would be that compared to now, you would have the flexibility Let me know what you think and whether this would sound doable in your |
From [email protected] on February 22, 2013 23:15:33 https://code.google.com/p/jsonschema2pojo/issues/detail?id=86 ? |
From [email protected] on February 23, 2013 00:17:00 Feel free to email it to me, my gmail account name is joelittlejohn. |
From [email protected] on February 23, 2013 01:00:23 |
From [email protected] on February 23, 2013 21:49:26 You will see I did have to change/add a few operations to the Annotator interface as well as a few other minor changes. Please review and let me know what you think. One known limitation right now is that array having "$ref" to other schema are not working right now. But the proposal above is merely implemented. I still need to test the actual XML serialization using the generated POJOs. I only tested that the POJOs were looking how I wanted them to look like. This is work in progress, let me know what you think before I invest too much time in this. |
From [email protected] on February 23, 2013 22:11:20 I think it would be more flexible to keep the XML annotations in their own annotator (not mix with/extend the json annotator), but allow users to specify more than one annotator in their configuration (probably as a comma-separated list). This makes the annotators much more composable. So if you need jackson2 json annotations, and jackson2 xml annotations, you specify: jackson2,jackson2xml (or equivalent for Ant and CLI). We could even support custom annotator classes by allowing a these values to be fqcn's, e.g. jackson2,jackson2xml,com.mycompany.CustomAnnotator Please don't think too much about this - I'm happy to apply this change (switch from 'one' to 'one or more' annotators) and I think you should concentrate on your XML production. Once you have written a functioning annotator we can integrate it into the project any way we see fit. Oh and just for the record, could you comment here indicating that you are happy for contributions to be released, as part of this project, under Apache 2.0. |
From [email protected] on February 23, 2013 22:40:54 But for other types of annotations (e.g. JAXB which is not Jackson-xml) or for external coded annotator it does make sense. Could you help me understand why JSON properties that references other schemas are not dereferenced BEFORE starting the generation process? Right now, the property of an array contain the "$ref" reference, but not its content. My XML annotator needs the dereferenced content. Is there any utility I could call to get the dereferenced JsonNode instead of the one containing the reference? As I said, this is not final. I added an integrated test case for the simple properties and the root element (incl. namespace), and another for array which is lacking a few assertions in it (I validated visually in the generated class the generated code was good, but I need to add the validation in the JUnits). |
From [email protected] on February 23, 2013 23:00:29 Re deref'ing refs before starting, unfortunately the code is just not structured in this way. The rules simply navigate the schema as they find it. Depending on your situation, you may be able to solve your problem like: But I warn you that there is no limit to how deep the referencing may go, including (in some cases) circular references. Maybe you could explain the scenario in more detail or give an example? When you say 'the property of an array contain the "$ref" reference' are you talking about the 'items' property that is included as part of the array definition? I don't think I understand why you need to access this. Are you trying to jump down from an array property into its items schema and check the content of the properties on the array elements? |
From [email protected] on February 23, 2013 23:29:38 In a nutshell the namespace of child items (element wrapper I'll look into your sample code tomorrow. Thanks for the quick reply. |
From [email protected] on February 24, 2013 16:32:43 I have added integrated tests, including the serialization. What would miss would be proper documenting which property in the JSON schema would generate which annotation (along with an example of the serialized XML). Below is an overview of the addition to jsonschema2pojo that I hope you would integrate in the core project:
Let me know what you think and feel free to refactor/adapt as appropriate, as long as the integrated test cases continue to pass. |
From [email protected] on February 24, 2013 16:34:25 |
From [email protected] on March 05, 2013 21:42:10 |
From [email protected] on March 13, 2013 00:03:17 Could you try building a snapshot from the current HEAD and let me know how you get on? |
From [email protected] on March 15, 2013 20:51:07 |
From [email protected] on April 04, 2013 11:52:20 |
From [email protected] on April 04, 2013 11:56:32 iirc you added a new method for one of your annotations but I tried to include this in one of the existing, class-level annotation methods instead (because I think your case was a bit xml-specific). |
Guys, Can you please let me know how do I generate POJO for my json with annotation "@JacksonXmlProperty(isAttribute=true)"? |
Original author: [email protected] (January 28, 2013 19:05:34)
Hi,
It seems current logic only handles "properties" array to generate POJOs. Is there any programmatic way to extend this logic to additionally support link-relationships representation in POJOs?
Specifically, jersey-server-linking defines @Ref/@binding annotations that allows to specify resource and template parameter binding.
Sample that comes with the library is a representation bean (POJO) as follows:
public class ItemRepresentation {
}
Right now, the following schema:
{
"type":"object",
"properties": {
"foo": {
"type": "string"
},
"bar": {
"type": "integer"
},
"baz": {
"type": "boolean"
}
},
"links": [
{
"rel": "self",
"href": "clients/{id}"
}]
}
... generates same POJO as following schema (without links):
{
"type":"object",
"properties": {
"foo": {
"type": "string"
},
"bar": {
"type": "integer"
},
"baz": {
"type": "boolean"
}
}
}
And since POJO generation is dynamic, I would like to be able to either decorate the generated POJOS dynamically, or to influence the code generating the classes in the first place, to add additional annotations in the generated code.
Beside the obvious decoupling from Jersey, do you see why links are not generated (at all) in the POJOs? Should I specifically add a bag of properties to have the defined links listed?
Thanks for your help.
Original issue: http://code.google.com/p/jsonschema2pojo/issues/detail?id=86
The text was updated successfully, but these errors were encountered: