mulesoft-labs / raml-for-jax-rs

This project is all about two way transformation of JAX-RS-annotated Java code to RAML API description and back.
Other
296 stars 181 forks source link

How can we set List to null by default in models? #390

Closed harikrishnanmurthy closed 5 years ago

harikrishnanmurthy commented 5 years ago

While generating Java models from raml using raml-to-jaxrs-maven-plugin, all the Lists in java class are initialized by default to new ArrayList<>(). How do we prevent this?

Description We have an attribute defined as an array in our raml spec. While trying to generate the Java model classes, the resulting List is initialized by default to new ArrayList<>(). This is causing problems in our application since an empty arraylist and null are treated differently.

"references": {"type" : "array", "items" : {
    "type": "object",
    "properties" : {
        "type": {"type" : "string"},
        "source": {"type" : "string"},
        "value": {"type" : "string"}
    },
    "required": ["type", "value"],
    "additionalProperties": false
}}

When the maven plugin is run, the resulting java code is

private List references= new ArrayList<>();

The reference field is not mandatory(optional)

What we want is:

private List references = null;

jaxrs-code-generator version raml-to-jaxrs-maven-plugin version : 3.0.1

jpbelang commented 5 years ago

I'm at work right now, so I can't get into anything really precise, just a first draft.

jsonschemastuff is delegated to the http://www.jsonschema2pojo.org/, and the maven plugin can push arguments to thelibrary. We have examples of this.

https://github.com/mulesoft-labs/raml-for-jax-rs/tree/master/raml-to-jaxrs/examples/maven-examples/simple-json-example

harikrishnanmurthy commented 5 years ago

Fixed it by adding attribute "default" : null

Thanks a lot @jpbelang for sending useful links