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 re default keyword on objects #1558

Open andye2004 opened 1 year ago

andye2004 commented 1 year ago

I'm wondering if there would be any appetite for adding a recognised value as a marker to instantiate an empty instance of an object, if the object is marked with the default keyword?

This is a fairly contrived example but consider the folowing foo-bar schema snippet:

{
  "title": "Foo Bar Schema",
  "type": "object",
  "properties": {
    "foo": {
      "title": "foo",
      "type": "object",
      "default": "instance",
      "properties": {
        "title": "bar",
        "type": "string",
        "default": "foobar"
      }
    },
    "bar": {
      "title": "bar",
      "type": "string",
      "default": "bar"
    }
  }
}

When the generate target is run we end up with two pojos, Foo.java and FooBar.java, with FooBar.java containing a foo property as follows:-

...
private Foo foo = null;
...

It would be good if, based on a known value being used as the default, e.g. "default": "instance", the following code could be generated instead:

...
private Foo foo = new Foo();
...

I've used the word instance in the example but the actual value could be made configurable to fit with different organisational requirements.

I'd be happy to provide a PR if it's something that would be accepted into the project.