joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

question mark as part of json property name means I'm unable to generate Java classes #1513

Closed ArtisanDejure closed 1 year ago

ArtisanDejure commented 1 year ago

I'm consuming a schema JSON from a vendor I don't control. The JSON contains some fields ending with a question mark (property-name?). this is of course not viewed as legal by the plugin. When the field is named the same as another property, but just with the question mark added, I get a message "trying to create the same field twice: . If it is just a property ending in the question mark the IllegalArgumentException is "Path not present: ".

I have a ticket open with the vendor to get them to correct their schema api, but I have no hope that they will actually do it.

Even trying to manually use the question mark in a Java field name is not legal, so I can't think of a "good " way for the plugin to handle it. I think it would be sufficient to be able to tell the plugin to skip certain properties though, such as ones containing the question mark. Another option would be for me to be able to specify a different name for them to generate with. That would be better than erroring out in my case. The alternative is for me to manually alter the vendor's schema, or to tediously write Java code to remove/change the offending properties.

unkish commented 1 year ago

Hi

It looks like it would be possible if https://github.com/joelittlejohn/jsonschema2pojo/blob/80d827db93e4cc3848c4f297ebbfe4b3fd936272/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonPointerUtils.java#L27-L32 would also have a mapping for ? eg.:

put("?", "~4");
ArtisanDejure commented 1 year ago

Thanks for replying. Is this something I need to override myself or something that will be done in the library?

joelittlejohn commented 1 year ago

It's a change required in the library I'm afraid. Very happy to accept a PR for this, with a change as per the suggestion from @unkish.